Skip to content

Commit

Permalink
supports nette/utils 2.5
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Oct 15, 2021
1 parent a6c575f commit 058a72a
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
],
"require": {
"php": ">=7.1 <8.2",
"nette/utils": "^3.1.5 || ^4.0"
"nette/utils": "^2.5.7 || ^3.1.5 || ^4.0"
},
"require-dev": {
"nette/tester": "^2.3 || ^2.4",
Expand Down
6 changes: 5 additions & 1 deletion src/Schema/Elements/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,11 @@ private function doFinalize($value, Context $context)
if (Nette\Utils\Reflection::isBuiltinType($this->castTo)) {
settype($value, $this->castTo);
} else {
$value = Nette\Utils\Arrays::toObject($value, new $this->castTo);
$object = new $this->castTo;
foreach ($value as $k => $v) {
$object->$k = $v;
}
$value = $object;
}
}

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 @@ -164,7 +164,7 @@ public function complete($value, Context $context)
} else {
$keys = array_map('strval', array_keys($items));
foreach ($extraKeys as $key) {
$hint = Nette\Utils\Helpers::getSuggestion($keys, (string) $key);
$hint = Nette\Utils\ObjectHelpers::getSuggestion($keys, (string) $key);
$context->addError(
'Unexpected item %path%' . ($hint ? ", did you mean '%hint%'?" : '.'),
Nette\Schema\Message::UNEXPECTED_ITEM,
Expand Down
4 changes: 3 additions & 1 deletion src/Schema/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ public static function merge($value, $base)

public static function getPropertyType(\ReflectionProperty $prop): ?string
{
if ($type = Nette\Utils\Type::fromReflection($prop)) {
if (!class_exists(Nette\Utils\Type::class)) {
throw new Nette\NotSupportedException('Expect::from() requires nette/utils 3.x');
} elseif ($type = Nette\Utils\Type::fromReflection($prop)) {
return (string) $type;
} elseif ($type = preg_replace('#\s.*#', '', (string) self::parseAnnotation($prop, 'var'))) {
$class = Reflection::getPropertyDeclaringClass($prop);
Expand Down
4 changes: 4 additions & 0 deletions tests/Schema/Expect.from.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ use Tester\Assert;

require __DIR__ . '/../bootstrap.php';

if (!class_exists(Nette\Utils\Type::class)) {
Tester\Environment::skip('Expect::from() requires nette/utils 3.x');
}


Assert::with(Structure::class, function () {
$schema = Expect::from(new stdClass);
Expand Down
3 changes: 3 additions & 0 deletions tests/Schema/Helpers.getPropertyType.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use Tester\Assert;
require __DIR__ . '/../bootstrap.php';
require __DIR__ . '/fixtures/Helpers.getPropertyType.php';

if (!class_exists(Nette\Utils\Type::class)) {
Tester\Environment::skip('Expect::from() requires nette/utils 3.x');
}

Assert::null(Helpers::getPropertyType(new \ReflectionProperty(NS\A::class, 'noType')));

Expand Down

0 comments on commit 058a72a

Please sign in to comment.