Skip to content

Commit

Permalink
castTo() is realized via transform()
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Oct 5, 2023
1 parent 6144611 commit 71c34f1
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 25 deletions.
18 changes: 1 addition & 17 deletions src/Schema/Elements/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ trait Base
/** @var callable[] */
private $transforms = [];

/** @var string|null */
private $castTo;

/** @var string|null */
private $deprecated;

Expand Down Expand Up @@ -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));
}


Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Schema/Elements/Structure.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
19 changes: 19 additions & 0 deletions src/Schema/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}
}
}
5 changes: 3 additions & 2 deletions tests/Schema/Expect.from.php74.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

declare(strict_types=1);

use Nette\Schema\Context;
use Nette\Schema\Elements\Structure;
use Nette\Schema\Expect;
use Tester\Assert;
Expand All @@ -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;
Expand All @@ -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));
});
5 changes: 3 additions & 2 deletions tests/Schema/Expect.from.php80.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

declare(strict_types=1);

use Nette\Schema\Context;
use Nette\Schema\Elements\Structure;
use Nette\Schema\Expect;
use Tester\Assert;
Expand All @@ -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;
Expand All @@ -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));
});
7 changes: 4 additions & 3 deletions tests/Schema/Expect.from.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

declare(strict_types=1);

use Nette\Schema\Context;
use Nette\Schema\Elements\Structure;
use Nette\Schema\Expect;
use Tester\Assert;
Expand All @@ -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';

Expand Down Expand Up @@ -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));
});


Expand Down

0 comments on commit 71c34f1

Please sign in to comment.