-
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: create cast resolver and cast contract
- Loading branch information
1 parent
00dfb78
commit 5332992
Showing
10 changed files
with
128 additions
and
46 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,12 @@ | ||
<?php | ||
|
||
namespace Mongolid\Model\Casts; | ||
|
||
use MongoDB\BSON\UTCDateTime; | ||
|
||
interface CastInterface | ||
{ | ||
public static function get(mixed $value): mixed; | ||
|
||
public static function set(mixed $value): mixed; | ||
} |
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,35 @@ | ||
<?php | ||
|
||
namespace Mongolid\Model\Casts; | ||
|
||
use Mongolid\Model\Casts\DateTime\DateTimeCast; | ||
use Mongolid\Model\Casts\DateTime\DateTimeImmutableCast; | ||
use Mongolid\Model\Casts\Exceptions\InvalidCastException; | ||
|
||
class CastResolver | ||
{ | ||
private const DATE_TIME = 'datetime'; | ||
private const IMMUTABLE_DATE_TIME = 'immutable_datetime'; | ||
|
||
public static array $validCasts = [ | ||
self::DATE_TIME, | ||
self::IMMUTABLE_DATE_TIME, | ||
]; | ||
|
||
public static function resolve(?string $cast): ?object | ||
{ | ||
if (is_null($cast)) { | ||
return null; | ||
} | ||
|
||
if (!in_array($cast, self::$validCasts)) { | ||
throw new InvalidCastException($cast); | ||
} | ||
|
||
return match($cast) { | ||
self::DATE_TIME => new DateTimeCast(), | ||
self::IMMUTABLE_DATE_TIME => new DateTimeImmutableCast(), | ||
default => null, | ||
}; | ||
} | ||
} |
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
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,17 @@ | ||
<?php | ||
|
||
namespace Mongolid\Model\Casts\Exceptions; | ||
|
||
use InvalidArgumentException; | ||
use Mongolid\Model\Casts\CastResolver; | ||
|
||
class InvalidCastException extends InvalidArgumentException | ||
{ | ||
public function __construct(string $cast) | ||
{ | ||
$available = implode(',', CastResolver::$validCasts); | ||
$message = "Invalid cast attribute: $cast. Use a valid one like $available"; | ||
|
||
parent::__construct($message); | ||
} | ||
} |
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,32 @@ | ||
<?php | ||
|
||
namespace Mongolid\Model\Casts; | ||
|
||
use Mongolid\Model\Casts\DateTime\DateTimeCast; | ||
use Mongolid\Model\Casts\DateTime\DateTimeImmutableCast; | ||
use Mongolid\Model\Casts\Exceptions\InvalidCastException; | ||
use Mongolid\TestCase; | ||
|
||
class CastResolverTest extends TestCase | ||
{ | ||
public function testShouldResolveCast(): void | ||
{ | ||
// Actions | ||
$dateTimeCast = CastResolver::resolve('datetime'); | ||
$dateTimeImmutableCast = CastResolver::resolve('immutable_datetime'); | ||
|
||
// Assertions | ||
$this->assertInstanceOf(DateTimeCast::class, $dateTimeCast); | ||
$this->assertInstanceOf(DateTimeImmutableCast::class, $dateTimeImmutableCast); | ||
} | ||
|
||
public function testShouldThrowExceptionWhenGivenInvalidCastToBeResolved(): void | ||
{ | ||
// Expectations | ||
$this->expectException(InvalidCastException::class); | ||
$this->expectExceptionMessage('Invalid cast attribute: invalid. Use a valid one like datetime,immutable_datetime'); | ||
|
||
// Actions | ||
CastResolver::resolve('invalid'); | ||
} | ||
} |
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