From 74414aa56ae3b674fa15fda71f0a40222d9da235 Mon Sep 17 00:00:00 2001 From: David Date: Fri, 27 Aug 2021 20:14:27 +0200 Subject: [PATCH] castTo datetime WIP --- src/Schema/Elements/Base.php | 2 ++ tests/Schema/Expect.castTo.phpt | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/Schema/Elements/Base.php b/src/Schema/Elements/Base.php index cd99955..1051883 100644 --- a/src/Schema/Elements/Base.php +++ b/src/Schema/Elements/Base.php @@ -166,6 +166,8 @@ private function doFinalize($value, Context $context) if ($this->castTo) { if (Nette\Utils\Reflection::isBuiltinType($this->castTo)) { settype($value, $this->castTo); + } elseif (in_array($this->castTo, [\DateTime::class, \DateTimeImmutable::class], true)) { + $value = new ($this->castTo)($value); } else { $value = Nette\Utils\Arrays::toObject($value, new $this->castTo); } diff --git a/tests/Schema/Expect.castTo.phpt b/tests/Schema/Expect.castTo.phpt index 0d0d5bb..4b99ca5 100644 --- a/tests/Schema/Expect.castTo.phpt +++ b/tests/Schema/Expect.castTo.phpt @@ -29,3 +29,20 @@ test('', function () { Assert::equal((object) ['a' => 1, 'b' => 2], (new Processor)->process($schema, ['a' => 1, 'b' => 2])); }); + + +// wip +class Foo +{ + public \DateTime $bar; +} + +$processor = new \Nette\Schema\Processor; +$processor->process( + \Nette\Schema\Expect::structure([ + 'bar' => \Nette\Schema\Expect::string()->castTo('DateTime'), + ])->castTo(Foo::class), + [ + 'bar' => '2021-01-01', + ] +);