-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: split datetimecast into multiple classes
- Loading branch information
1 parent
5f782da
commit a5e6a07
Showing
10 changed files
with
211 additions
and
96 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Mongolid\Model\Casts; | ||
|
||
use MongoDB\BSON\UTCDateTime; | ||
use Mongolid\Model\Casts\DateTime\BaseDateTimeCast; | ||
use Mongolid\Model\Casts\DateTime\DateTimeCast; | ||
use Mongolid\Model\Casts\DateTime\DateTimeImmutableCast; | ||
|
||
class Caster | ||
{ | ||
public static function castToDateTime( | ||
string $cast, | ||
mixed $value | ||
) | ||
{ | ||
if (!$value instanceof UTCDateTime) { | ||
return null; | ||
} | ||
|
||
if ($cast === 'datetime') { | ||
return DateTimeCast::castToDateTime($value); | ||
} | ||
|
||
if ($cast === 'immutable_datetime') { | ||
return DateTimeImmutableCast::castToDateTime($value); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public static function castToMongoUTCDateTime(mixed $value): ?UTCDateTime | ||
{ | ||
return BaseDateTimeCast::castToMongoUTCDateTime($value); | ||
} | ||
} |
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,25 @@ | ||
<?php | ||
|
||
namespace Mongolid\Model\Casts\DateTime; | ||
|
||
use DateTimeInterface; | ||
use MongoDB\BSON\UTCDateTime; | ||
|
||
abstract class BaseDateTimeCast | ||
{ | ||
public static array $validCasts = [ | ||
'datetime', | ||
'immutable_datetime', | ||
]; | ||
|
||
abstract public static function castToDateTime(?UTCDateTime $value): ?DateTimeInterface; | ||
|
||
public static function castToMongoUTCDateTime(?DateTimeInterface $value): ?UTCDateTime | ||
{ | ||
if (is_null($value)) { | ||
return null; | ||
} | ||
|
||
return new UTCDateTime($value); | ||
} | ||
} |
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,19 @@ | ||
<?php | ||
|
||
namespace Mongolid\Model\Casts\DateTime; | ||
|
||
use DateTime; | ||
use MongoDB\BSON\UTCDateTime; | ||
use Mongolid\Util\LocalDateTime; | ||
|
||
class DateTimeCast extends BaseDateTimeCast | ||
{ | ||
public static function castToDateTime(?UTCDateTime $value): ?DateTime | ||
{ | ||
if (is_null($value)) { | ||
return null; | ||
} | ||
|
||
return LocalDateTime::get($value); | ||
} | ||
} |
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,25 @@ | ||
<?php | ||
|
||
namespace Mongolid\Model\Casts\DateTime; | ||
|
||
use DateTimeImmutable; | ||
use MongoDB\BSON\UTCDateTime; | ||
use Mongolid\Util\LocalDateTime; | ||
|
||
class DateTimeImmutableCast extends BaseDateTimeCast | ||
{ | ||
/** | ||
* @param UTCDateTime|null $value | ||
* @return DateTimeImmutable|null | ||
*/ | ||
public static function castToDateTime(?UTCDateTime $value): ?DateTimeImmutable | ||
{ | ||
if (is_null($value)) { | ||
return null; | ||
} | ||
|
||
return DateTimeImmutable::createFromMutable( | ||
LocalDateTime::get($value) | ||
); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
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,23 @@ | ||
<?php | ||
|
||
namespace Mongolid\Model\Casts\DateTime; | ||
|
||
use MongoDB\BSON\UTCDateTime; | ||
use Mongolid\TestCase; | ||
|
||
class BaseDateTimeCast extends TestCase | ||
{ | ||
public function testCastToMongoUTCDateTime(): void | ||
{ | ||
// Set | ||
$dateInDateTime = DateTime::createFromFormat('d/m/Y H:i:s', '08/10/2025 12:30:45'); | ||
|
||
// Actions | ||
$expires_at = DateTimeCast::castToMongoUTCDateTime($dateInDateTime); | ||
$nulled_at = DateTimeCast::castToMongoUTCDateTime(null); | ||
|
||
// Assertions | ||
$this->assertInstanceOf(UTCDateTime::class, $expires_at); | ||
$this->assertNull($nulled_at); | ||
} | ||
} |
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,28 @@ | ||
<?php | ||
|
||
namespace Mongolid\Model\Casts\DateTime; | ||
|
||
use DateTime; | ||
use MongoDB\BSON\UTCDateTime; | ||
use Mongolid\TestCase; | ||
|
||
class DateTimeCastTest extends TestCase | ||
{ | ||
public function testCastToDateTime(): void | ||
{ | ||
// Set | ||
$timestamp = new UTCDateTime( | ||
DateTime::createFromFormat('d/m/Y H:i:s', '08/10/2025 12:30:45') | ||
); | ||
|
||
// Actions | ||
$revoked_at = DateTimeCast::castToDateTime(null); | ||
$expires_at = DateTimeCast::castToDateTime($timestamp); | ||
$validated_at = DateTimeCast::castToDateTime($timestamp); | ||
|
||
// Assertions | ||
$this->assertNull($revoked_at); | ||
$this->assertInstanceOf(DateTime::class, $expires_at); | ||
$this->assertInstanceOf(DateTime::class, $validated_at); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
tests/Unit/Model/Casts/DateTime/DateTimeImmutableCastTest.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,28 @@ | ||
<?php | ||
|
||
namespace Mongolid\Model\Casts\DateTime; | ||
|
||
use DateTimeImmutable; | ||
use MongoDB\BSON\UTCDateTime; | ||
use Mongolid\TestCase; | ||
|
||
class DateTimeImmutableCastTest extends TestCase | ||
{ | ||
public function testCastToDateTime(): void | ||
{ | ||
// Set | ||
$timestamp = new UTCDateTime( | ||
DateTimeImmutable::createFromFormat('d/m/Y H:i:s', '08/10/2025 12:30:45') | ||
); | ||
|
||
// Actions | ||
$revoked_at = DateTimeImmutableCast::castToDateTime(null); | ||
$birthdate = DateTimeImmutableCast::castToDateTime($timestamp); | ||
$created_at = DateTimeImmutableCast::castToDateTime($timestamp); | ||
|
||
// Assertions | ||
$this->assertNull($revoked_at); | ||
$this->assertInstanceOf(DateTimeImmutable::class, $birthdate); | ||
$this->assertInstanceOf(DateTimeImmutable::class, $created_at); | ||
} | ||
} |