Skip to content

Commit

Permalink
castTo datetime WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Oct 4, 2022
1 parent 81438a8 commit e7ef564
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Schema/Elements/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,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);
}
Expand Down
17 changes: 17 additions & 0 deletions tests/Schema/Expect.castTo.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
);

0 comments on commit e7ef564

Please sign in to comment.