Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Oct 3, 2023
1 parent 4eda8ba commit 8d82bb3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
7 changes: 5 additions & 2 deletions src/Schema/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,12 @@ public static function getCastStrategy(string $type): \Closure
};
} elseif (method_exists($type, '__construct')) {
return static function ($value) use ($type) {
if (PHP_VERSION_ID < 80000 && is_array($value)) {
throw new Nette\NotSupportedException("Creating $type objects is supported since PHP 8.0");
}
return is_array($value)
? new ($type)(...$value)
: new ($type)($value);
? new $type(...$value)
: new $type($value);
};
} else {
return static function ($value) use ($type) {
Expand Down
15 changes: 6 additions & 9 deletions tests/Schema/Expect.castTo.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,14 @@ test('simple object', function () {


test('object with constructor', function () {
if (PHP_VERSION_ID < 80000) {
return;
}

class Foo2
{
private int $a;

private int $b;
private $a;
private $b;


public function __construct(int $a, int $b)
Expand All @@ -62,12 +65,6 @@ test('object with constructor', function () {


test('DateTime', function () {
$schema = Expect::array()->castTo(DateTime::class);
Assert::equal(
new DateTime('2021-01-01'),
(new Processor)->process($schema, ['datetime' => '2021-01-01']),
);

$schema = Expect::string()->castTo(DateTime::class);
Assert::equal(
new DateTime('2021-01-01'),
Expand Down

0 comments on commit 8d82bb3

Please sign in to comment.