Skip to content

Commit

Permalink
castTo() allows you to create objects [Closes #44][Closes #47][Closes #…
Browse files Browse the repository at this point in the history
…58][Closes #46]
  • Loading branch information
dg committed Oct 4, 2023
1 parent 9e4d58b commit bc4e370
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 10 deletions.
37 changes: 35 additions & 2 deletions readme.md
Expand Up @@ -422,10 +422,43 @@ Successfully validated data can be cast:
Expect::scalar()->castTo('string');
```

In addition to native PHP types, you can also cast to classes:
In addition to native PHP types, you can also cast to classes. It distinguishes whether it is a simple class without a constructor or a class with a constructor. If the class has no constructor, an instance of it is created and all elements of the structure are written to its properties:

```php
Expect::scalar()->castTo('AddressEntity');
class Info
{
public bool $processRefund;
public int $refundAmount;
}

Expect::structure([
'processRefund' => Expect::bool(),
'refundAmount' => Expect::int(),
])->castTo(Info::class);

// creates '$obj = new Info' and writes to $obj->processRefund and $obj->refundAmount
```

If the class has a constructor, the elements of the structure are passed as named parameters to the constructor (requires PHP 8):

```php
class Info
{
public function __construct(
public bool $processRefund,
public int $refundAmount,
) {
}
}

// creates $obj = new Info(processRefund: ..., refundAmount: ...)
```

Casting combined with a scalar parameter creates an object and passes the value as the sole parameter to the constructor:

```php
Expect::string()->castTo(DateTime::class);
// creates new DateTime(...)
```


Expand Down
9 changes: 9 additions & 0 deletions src/Schema/Helpers.php
Expand Up @@ -176,6 +176,15 @@ public static function getCastStrategy(string $type): \Closure
settype($value, $type);
return $value;
};
} 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);
};
} else {
return static function ($value) use ($type) {
$object = new $type;
Expand Down
58 changes: 50 additions & 8 deletions tests/Schema/Expect.castTo.phpt
Expand Up @@ -10,22 +10,64 @@ use Tester\Assert;
require __DIR__ . '/../bootstrap.php';


test('', function () {
test('built-in', function () {
$schema = Expect::int()->castTo('string');

Assert::same('10', (new Processor)->process($schema, 10));

$schema = Expect::string()->castTo('array');
Assert::same(['foo'], (new Processor)->process($schema, 'foo'));
});


test('', function () {
$schema = Expect::string()->castTo('array');
test('simple object', function () {
class Foo1
{
public $a;
public $b;
}

Assert::same(['foo'], (new Processor)->process($schema, 'foo'));
$foo = new Foo1;
$foo->a = 1;
$foo->b = 2;

$schema = Expect::array()->castTo(Foo1::class);
Assert::equal(
$foo,
(new Processor)->process($schema, ['a' => 1, 'b' => 2])
);
});


test('', function () {
$schema = Expect::array()->castTo('stdClass');
test('object with constructor', function () {
if (PHP_VERSION_ID < 80000) {
return;
}

class Foo2
{
private $a;
private $b;


public function __construct(int $a, int $b)
{
$this->b = $b;
$this->a = $a;
}
}

$schema = Expect::array()->castTo(Foo2::class);
Assert::equal(
new Foo2(1, 2),
(new Processor)->process($schema, ['b' => 2, 'a' => 1])
);
});


Assert::equal((object) ['a' => 1, 'b' => 2], (new Processor)->process($schema, ['a' => 1, 'b' => 2]));
test('DateTime', function () {
$schema = Expect::string()->castTo(DateTime::class);
Assert::equal(
new DateTime('2021-01-01'),
(new Processor)->process($schema, '2021-01-01')
);
});

0 comments on commit bc4e370

Please sign in to comment.