-
-
Notifications
You must be signed in to change notification settings - Fork 505
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update custom mapping example (#2654)
The previous example was a simplified copy of the date type. In order to present something more useful, the new example is inspired by MongoDB's codec tutorial.
- Loading branch information
Showing
4 changed files
with
146 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Documentation\CustomMapping; | ||
|
||
use DateTimeImmutable; | ||
use DateTimeZone; | ||
use Doctrine\ODM\MongoDB\Tests\BaseTestCase; | ||
use Doctrine\ODM\MongoDB\Types\Type; | ||
|
||
class CustomMappingTest extends BaseTestCase | ||
{ | ||
public function testTest(): void | ||
{ | ||
Type::addType('date_with_timezone', DateTimeWithTimezoneType::class); | ||
Type::overrideType('date_immutable', DateTimeWithTimezoneType::class); | ||
|
||
$thing = new Thing(); | ||
$thing->date = new DateTimeImmutable('2021-01-01 00:00:00', new DateTimeZone('Africa/Tripoli')); | ||
|
||
$this->dm->persist($thing); | ||
$this->dm->flush(); | ||
$this->dm->clear(); | ||
|
||
$result = $this->dm->find(Thing::class, $thing->id); | ||
$this->assertEquals($thing->date, $result->date); | ||
$this->assertEquals('Africa/Tripoli', $result->date->getTimezone()->getName()); | ||
|
||
// Ensure we don't need to handle null values | ||
$nothing = new Thing(); | ||
|
||
$this->dm->persist($nothing); | ||
$this->dm->flush(); | ||
$this->dm->clear(); | ||
|
||
$result = $this->dm->find(Thing::class, $nothing->id); | ||
$this->assertNull($result->date); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
tests/Documentation/CustomMapping/DateTimeWithTimezoneType.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Documentation\CustomMapping; | ||
|
||
use DateTimeImmutable; | ||
use DateTimeInterface; | ||
use DateTimeZone; | ||
use Doctrine\ODM\MongoDB\Types\ClosureToPHP; | ||
use Doctrine\ODM\MongoDB\Types\Type; | ||
use MongoDB\BSON\UTCDateTime; | ||
use RuntimeException; | ||
|
||
class DateTimeWithTimezoneType extends Type | ||
{ | ||
// This trait provides default closureToPHP used during data hydration | ||
use ClosureToPHP; | ||
|
||
/** @param array{utc: UTCDateTime, tz: string} $value */ | ||
public function convertToPHPValue($value): DateTimeImmutable | ||
{ | ||
if (! isset($value['utc'], $value['tz'])) { | ||
throw new RuntimeException('Database value cannot be converted to date with timezone. Expected array with "utc" and "tz" keys.'); | ||
} | ||
|
||
$timeZone = new DateTimeZone($value['tz']); | ||
$dateTime = $value['utc'] | ||
->toDateTime() | ||
->setTimeZone($timeZone); | ||
|
||
return DateTimeImmutable::createFromMutable($dateTime); | ||
} | ||
|
||
/** | ||
* @param DateTimeInterface $value | ||
* | ||
* @return array{utc: UTCDateTime, tz: string} | ||
*/ | ||
public function convertToDatabaseValue($value): array | ||
{ | ||
return [ | ||
'utc' => new UTCDateTime($value), | ||
'tz' => $value->getTimezone()->getName(), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Documentation\CustomMapping; | ||
|
||
use DateTimeImmutable; | ||
use Doctrine\ODM\MongoDB\Mapping\Annotations\Document; | ||
use Doctrine\ODM\MongoDB\Mapping\Annotations\Field; | ||
use Doctrine\ODM\MongoDB\Mapping\Annotations\Id; | ||
|
||
#[Document] | ||
class Thing | ||
{ | ||
#[Id] | ||
public string $id; | ||
|
||
#[Field(type: 'date_with_timezone')] | ||
public ?DateTimeImmutable $date = null; | ||
} |