-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update project structure * Foundation for writing custom casters * Custom casters logic * Anonymous casters * Update readme + refactor callable casters
- Loading branch information
1 parent
778df23
commit 3c890f6
Showing
13 changed files
with
323 additions
and
15 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,81 @@ | ||
<?php | ||
|
||
namespace AntoninMasek\SimpleHydrator\Casters; | ||
|
||
use AntoninMasek\SimpleHydrator\Exceptions\InvalidCasterException; | ||
use AntoninMasek\SimpleHydrator\Exceptions\UnknownCasterException; | ||
use ReflectionProperty; | ||
|
||
abstract class Caster | ||
{ | ||
private const CASTERS_NAMESPACE = 'AntoninMasek\SimpleHydrator\Casters'; | ||
private const CASTERS_SUFFIX = 'Caster'; | ||
|
||
private static array $casters = []; | ||
|
||
public function __construct(protected ReflectionProperty $property) | ||
{ | ||
} | ||
|
||
public static function setCasters(array $map): array | ||
{ | ||
return self::$casters = $map; | ||
} | ||
|
||
public static function clearCasters(): array | ||
{ | ||
return self::$casters = []; | ||
} | ||
|
||
public static function registerCaster(string $className, string|callable $caster): array | ||
{ | ||
return self::setCasters(array_merge(self::$casters, [ | ||
$className => $caster, | ||
])); | ||
} | ||
|
||
/** | ||
* @throws InvalidCasterException | ||
* @throws UnknownCasterException | ||
*/ | ||
public static function make(ReflectionProperty $property): Caster | ||
{ | ||
$propertyClassName = $property->getType()->getName(); | ||
|
||
$casterClassNameOrCallable = ! array_key_exists($propertyClassName, self::$casters) | ||
? self::CASTERS_NAMESPACE . "\\$propertyClassName" . self::CASTERS_SUFFIX | ||
: self::$casters[$propertyClassName]; | ||
|
||
if (is_callable($casterClassNameOrCallable)) { | ||
return self::handleCallableCaster($casterClassNameOrCallable); | ||
} | ||
|
||
if (! class_exists($casterClassNameOrCallable)) { | ||
throw new UnknownCasterException($casterClassNameOrCallable); | ||
} | ||
|
||
$caster = new $casterClassNameOrCallable($property); | ||
|
||
if (! ($caster instanceof Caster)) { | ||
throw new InvalidCasterException(); | ||
} | ||
|
||
return $caster; | ||
} | ||
|
||
private static function handleCallableCaster($callable): Caster | ||
{ | ||
return new class($callable) extends Caster { | ||
public function __construct(private mixed $callable) | ||
{ | ||
} | ||
|
||
public function cast(mixed $value): mixed | ||
{ | ||
return ($this->callable)($value); | ||
} | ||
}; | ||
} | ||
|
||
abstract public function cast(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,17 @@ | ||
<?php | ||
|
||
namespace AntoninMasek\SimpleHydrator\Casters; | ||
|
||
use DateTime; | ||
|
||
class DateTimeCaster extends Caster | ||
{ | ||
public function cast(mixed $value): ?DateTime | ||
{ | ||
if (is_null($value)) { | ||
return null; | ||
} | ||
|
||
return new DateTime($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
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,13 @@ | ||
<?php | ||
|
||
namespace AntoninMasek\SimpleHydrator\Exceptions; | ||
|
||
use Exception; | ||
|
||
class CasterException extends Exception | ||
{ | ||
public static function invalidValue(string $className, mixed $value): static | ||
{ | ||
return new static("Array expected. Got $value. Cannot tell how to build $className from $value. To solve this you can write your own caster. To find out how, take a look at 'Casters' section in the readme."); | ||
} | ||
} |
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,14 @@ | ||
<?php | ||
|
||
namespace AntoninMasek\SimpleHydrator\Exceptions; | ||
|
||
use AntoninMasek\SimpleHydrator\Casters\Caster; | ||
use Exception; | ||
|
||
class InvalidCasterException extends Exception | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct('All casters have to extend ' . Caster::class); | ||
} | ||
} |
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,13 @@ | ||
<?php | ||
|
||
namespace AntoninMasek\SimpleHydrator\Exceptions; | ||
|
||
use Exception; | ||
|
||
class UnknownCasterException extends Exception | ||
{ | ||
public function __construct(string $className) | ||
{ | ||
parent::__construct("Unknown caster $className"); | ||
} | ||
} |
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,19 @@ | ||
<?php | ||
|
||
namespace AntoninMasek\SimpleHydrator\Tests\Casters; | ||
|
||
use AntoninMasek\SimpleHydrator\Casters\Caster; | ||
use AntoninMasek\SimpleHydrator\Tests\Models\ClassThatNeedsCustomCaster; | ||
use DateTime; | ||
|
||
class TestingCaster extends Caster | ||
{ | ||
public function cast(mixed $value): ClassThatNeedsCustomCaster | ||
{ | ||
$class = new ClassThatNeedsCustomCaster(); | ||
|
||
$class->value = floatval((new DateTime())->format('n')) + $value; | ||
|
||
return $class; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -6,4 +6,5 @@ class Car | |
{ | ||
public string $type; | ||
public string $brand; | ||
public ?ClassThatNeedsCustomCaster $customCaster; | ||
} |
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 AntoninMasek\SimpleHydrator\Tests\Models; | ||
|
||
class ClassThatNeedsCustomCaster | ||
{ | ||
public float $value; | ||
|
||
public function __construct() | ||
{ | ||
} | ||
} |
Oops, something went wrong.