diff --git a/src/Schema/Elements/Base.php b/src/Schema/Elements/Base.php index 55c28ad..4b44514 100644 --- a/src/Schema/Elements/Base.php +++ b/src/Schema/Elements/Base.php @@ -31,9 +31,6 @@ trait Base /** @var callable[] */ private $transforms = []; - /** @var string|null */ - private $castTo; - /** @var string|null */ private $deprecated; @@ -61,8 +58,7 @@ public function before(callable $handler): self public function castTo(string $type): self { - $this->castTo = $type; - return $this; + return $this->transform(Helpers::getCastStrategy($type)); } @@ -134,18 +130,6 @@ private function doDeprecation(Context $context): void private function doTransform($value, Context $context) { - if ($this->castTo) { - if (Nette\Utils\Reflection::isBuiltinType($this->castTo)) { - settype($value, $this->castTo); - } else { - $object = new $this->castTo; - foreach ($value as $k => $v) { - $object->$k = $v; - } - $value = $object; - } - } - $isOk = $context->createChecker(); foreach ($this->transforms as $handler) { $value = $handler($value, $context); diff --git a/src/Schema/Elements/Structure.php b/src/Schema/Elements/Structure.php index b5f8203..02ca77b 100644 --- a/src/Schema/Elements/Structure.php +++ b/src/Schema/Elements/Structure.php @@ -40,7 +40,7 @@ public function __construct(array $items) { (function (Schema ...$items) {})(...array_values($items)); $this->items = $items; - $this->castTo = 'object'; + $this->castTo('object'); $this->required = true; } diff --git a/src/Schema/Helpers.php b/src/Schema/Helpers.php index dd8cd0b..c0432db 100644 --- a/src/Schema/Helpers.php +++ b/src/Schema/Helpers.php @@ -167,4 +167,23 @@ public static function validatePattern(string $value, string $pattern, Context $ ); } } + + + public static function getCastStrategy(string $type): \Closure + { + if (Nette\Utils\Reflection::isBuiltinType($type)) { + return static function ($value) use ($type) { + settype($value, $type); + return $value; + }; + } else { + return static function ($value) use ($type) { + $object = new $type; + foreach ($value as $k => $v) { + $object->$k = $v; + } + return $object; + }; + } + } } diff --git a/tests/Schema/Expect.from.php74.phpt b/tests/Schema/Expect.from.php74.phpt index 32a51ac..6726880 100644 --- a/tests/Schema/Expect.from.php74.phpt +++ b/tests/Schema/Expect.from.php74.phpt @@ -6,6 +6,7 @@ declare(strict_types=1); +use Nette\Schema\Context; use Nette\Schema\Elements\Structure; use Nette\Schema\Expect; use Tester\Assert; @@ -15,7 +16,7 @@ require __DIR__ . '/../bootstrap.php'; Assert::with(Structure::class, function () { - $schema = Expect::from(new class { + $schema = Expect::from($obj = new class { public string $dsn = 'mysql'; public ?string $user; public ?string $password = null; @@ -35,5 +36,5 @@ Assert::with(Structure::class, function () { 'mixed' => Expect::mixed(), 'arr' => Expect::type('array')->default([1]), ], $schema->items); - Assert::type('string', $schema->castTo); + Assert::type($obj, $schema->doTransform([], new Context)); }); diff --git a/tests/Schema/Expect.from.php80.phpt b/tests/Schema/Expect.from.php80.phpt index 9a6c0a4..db0e1b1 100644 --- a/tests/Schema/Expect.from.php80.phpt +++ b/tests/Schema/Expect.from.php80.phpt @@ -6,6 +6,7 @@ declare(strict_types=1); +use Nette\Schema\Context; use Nette\Schema\Elements\Structure; use Nette\Schema\Expect; use Tester\Assert; @@ -15,7 +16,7 @@ require __DIR__ . '/../bootstrap.php'; Assert::with(Structure::class, function () { - $schema = Expect::from(new class { + $schema = Expect::from($obj = new class { public string $dsn = 'mysql'; public ?string $user; public ?string $password = null; @@ -35,5 +36,5 @@ Assert::with(Structure::class, function () { 'mixed' => Expect::mixed()->required(), 'arr' => Expect::type('array')->default([1]), ], $schema->items); - Assert::type('string', $schema->castTo); + Assert::type($obj, $schema->doTransform([], new Context)); }); diff --git a/tests/Schema/Expect.from.phpt b/tests/Schema/Expect.from.phpt index 4c24218..9623957 100644 --- a/tests/Schema/Expect.from.phpt +++ b/tests/Schema/Expect.from.phpt @@ -2,6 +2,7 @@ declare(strict_types=1); +use Nette\Schema\Context; use Nette\Schema\Elements\Structure; use Nette\Schema\Expect; use Tester\Assert; @@ -19,12 +20,12 @@ Assert::with(Structure::class, function () { Assert::type(Structure::class, $schema); Assert::same([], $schema->items); - Assert::same(stdClass::class, $schema->castTo); + Assert::type(stdClass::class, $schema->doTransform([], new Context)); }); Assert::with(Structure::class, function () { - $schema = Expect::from(new class { + $schema = Expect::from($obj = new class { /** @var string */ public $dsn = 'mysql'; @@ -59,7 +60,7 @@ Assert::with(Structure::class, function () { 'arr' => Expect::type('array|null')->default(null), 'required' => Expect::type('string')->required(), ], $schema->items); - Assert::type('string', $schema->castTo); + Assert::type($obj, $schema->doTransform([], new Context)); });