v1.2.2
This commit is contained in:
parent
3a25585090
commit
80a2202d6e
14 changed files with 305 additions and 122 deletions
14
vendor/composer/installed.json
vendored
14
vendor/composer/installed.json
vendored
|
|
@ -275,17 +275,17 @@
|
|||
},
|
||||
{
|
||||
"name": "nette/schema",
|
||||
"version": "v1.3.5",
|
||||
"version_normalized": "1.3.5.0",
|
||||
"version": "v1.3.6",
|
||||
"version_normalized": "1.3.6.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/nette/schema.git",
|
||||
"reference": "f0ab1a3cda782dbc5da270d28545236aa80c4002"
|
||||
"reference": "c54350438cd6914616f790a49cb424605f421562"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/nette/schema/zipball/f0ab1a3cda782dbc5da270d28545236aa80c4002",
|
||||
"reference": "f0ab1a3cda782dbc5da270d28545236aa80c4002",
|
||||
"url": "https://api.github.com/repos/nette/schema/zipball/c54350438cd6914616f790a49cb424605f421562",
|
||||
"reference": "c54350438cd6914616f790a49cb424605f421562",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
|
|
@ -299,7 +299,7 @@
|
|||
"phpstan/phpstan": "^2.1.39@stable",
|
||||
"tracy/tracy": "^2.8"
|
||||
},
|
||||
"time": "2026-02-23T03:47:12+00:00",
|
||||
"time": "2026-08-16T21:58:41+00:00",
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
|
|
@ -339,7 +339,7 @@
|
|||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/nette/schema/issues",
|
||||
"source": "https://github.com/nette/schema/tree/v1.3.5"
|
||||
"source": "https://github.com/nette/schema/tree/v1.3.6"
|
||||
},
|
||||
"install-path": "../nette/schema"
|
||||
},
|
||||
|
|
|
|||
6
vendor/composer/installed.php
vendored
6
vendor/composer/installed.php
vendored
|
|
@ -38,9 +38,9 @@
|
|||
'dev_requirement' => false,
|
||||
),
|
||||
'nette/schema' => array(
|
||||
'pretty_version' => 'v1.3.5',
|
||||
'version' => '1.3.5.0',
|
||||
'reference' => 'f0ab1a3cda782dbc5da270d28545236aa80c4002',
|
||||
'pretty_version' => 'v1.3.6',
|
||||
'version' => '1.3.6.0',
|
||||
'reference' => 'c54350438cd6914616f790a49cb424605f421562',
|
||||
'type' => 'library',
|
||||
'install_path' => __DIR__ . '/../nette/schema',
|
||||
'aliases' => array(),
|
||||
|
|
|
|||
2
vendor/nette/schema/readme.md
vendored
2
vendor/nette/schema/readme.md
vendored
|
|
@ -126,7 +126,7 @@ Expect::null()
|
|||
Expect::array($default = [])
|
||||
```
|
||||
|
||||
And then all types [supported by the Validators](https://doc.nette.org/validators#toc-validation-rules) via `Expect::type('scalar')` or abbreviated `Expect::scalar()`. Also class or interface names are accepted, e.g. `Expect::type('AddressEntity')`.
|
||||
And then all types [supported by the Validators](https://doc.nette.org/validators#toc-expected-types) via `Expect::type('scalar')` or abbreviated `Expect::scalar()`. Also class or interface names are accepted, e.g. `Expect::type('AddressEntity')`.
|
||||
|
||||
You can also use union notation:
|
||||
|
||||
|
|
|
|||
8
vendor/nette/schema/src/Schema/Context.php
vendored
8
vendor/nette/schema/src/Schema/Context.php
vendored
|
|
@ -10,6 +10,9 @@ namespace Nette\Schema;
|
|||
use function count;
|
||||
|
||||
|
||||
/**
|
||||
* Accumulates errors and warnings during schema validation and tracks the current path.
|
||||
*/
|
||||
final class Context
|
||||
{
|
||||
public bool $skipDefaults = false;
|
||||
|
|
@ -44,7 +47,10 @@ final class Context
|
|||
}
|
||||
|
||||
|
||||
/** @return \Closure(): bool */
|
||||
/**
|
||||
* Returns a closure that returns true as long as no new errors have been added since the call.
|
||||
* @return \Closure(): bool
|
||||
*/
|
||||
public function createChecker(): \Closure
|
||||
{
|
||||
$count = count($this->errors);
|
||||
|
|
|
|||
|
|
@ -14,6 +14,9 @@ use Nette\Schema\Schema;
|
|||
use function array_merge, array_unique, implode, is_array;
|
||||
|
||||
|
||||
/**
|
||||
* Schema that accepts any of a fixed set of values or sub-schemas (union type / enumeration).
|
||||
*/
|
||||
final class AnyOf implements Schema
|
||||
{
|
||||
use Base;
|
||||
|
|
@ -32,6 +35,9 @@ final class AnyOf implements Schema
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets the first variant as the default value (instead of null).
|
||||
*/
|
||||
public function firstIsDefault(): self
|
||||
{
|
||||
$this->default = $this->set[0];
|
||||
|
|
@ -39,6 +45,9 @@ final class AnyOf implements Schema
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Allows null as an accepted value in addition to the defined variants.
|
||||
*/
|
||||
public function nullable(): self
|
||||
{
|
||||
$this->set[] = null;
|
||||
|
|
@ -46,6 +55,9 @@ final class AnyOf implements Schema
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Allows the value to be a DynamicParameter as an accepted variant.
|
||||
*/
|
||||
public function dynamic(): self
|
||||
{
|
||||
$this->set[] = new Type(Nette\Schema\DynamicParameter::class);
|
||||
|
|
@ -88,10 +100,13 @@ final class AnyOf implements Schema
|
|||
foreach ($this->set as $item) {
|
||||
if ($item instanceof Schema) {
|
||||
$dolly = new Context;
|
||||
$dolly->skipDefaults = $context->skipDefaults;
|
||||
$dolly->isKey = $context->isKey;
|
||||
$dolly->path = $context->path;
|
||||
$res = $item->complete($item->normalize($value, $dolly), $dolly);
|
||||
if (!$dolly->errors) {
|
||||
$context->warnings = array_merge($context->warnings, $dolly->warnings);
|
||||
$context->dynamics = array_merge($context->dynamics, $dolly->dynamics);
|
||||
return $res;
|
||||
}
|
||||
|
||||
|
|
|
|||
27
vendor/nette/schema/src/Schema/Elements/Base.php
vendored
27
vendor/nette/schema/src/Schema/Elements/Base.php
vendored
|
|
@ -43,7 +43,10 @@ trait Base
|
|||
}
|
||||
|
||||
|
||||
/** @param callable(mixed): mixed $handler */
|
||||
/**
|
||||
* Sets a pre-normalization callback applied to the raw input value before any validation.
|
||||
* @param callable(mixed): mixed $handler
|
||||
*/
|
||||
public function before(callable $handler): self
|
||||
{
|
||||
$this->before = $handler(...);
|
||||
|
|
@ -51,13 +54,19 @@ trait Base
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Casts the validated value to a built-in type or instantiates the given class.
|
||||
*/
|
||||
public function castTo(string $type): self
|
||||
{
|
||||
return $this->transform(Helpers::getCastStrategy($type));
|
||||
}
|
||||
|
||||
|
||||
/** @param callable(mixed, Context): mixed $handler */
|
||||
/**
|
||||
* Adds a post-validation transformation callback. The handler may also report errors via Context.
|
||||
* @param callable(mixed, Context): mixed $handler
|
||||
*/
|
||||
public function transform(callable $handler): self
|
||||
{
|
||||
$this->transforms[] = $handler(...);
|
||||
|
|
@ -65,7 +74,10 @@ trait Base
|
|||
}
|
||||
|
||||
|
||||
/** @param callable(mixed): bool $handler */
|
||||
/**
|
||||
* Adds a custom validation assertion; optionally describe it for error messages.
|
||||
* @param callable(mixed): bool $handler
|
||||
*/
|
||||
public function assert(callable $handler, ?string $description = null): self
|
||||
{
|
||||
$expected = $description ?? (is_string($handler) ? "$handler()" : '#' . count($this->transforms));
|
||||
|
|
@ -78,11 +90,14 @@ trait Base
|
|||
Nette\Schema\Message::FailedAssertion,
|
||||
['value' => $value, 'assertion' => $expected],
|
||||
);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/** Marks as deprecated */
|
||||
/**
|
||||
* Marks the item as deprecated; emits a warning with the given message when the item is used.
|
||||
*/
|
||||
public function deprecated(string $message = 'The item %path% is deprecated.'): self
|
||||
{
|
||||
$this->deprecated = $message;
|
||||
|
|
@ -138,7 +153,7 @@ trait Base
|
|||
}
|
||||
|
||||
|
||||
/** @deprecated use Nette\Schema\Validators::validateType() */
|
||||
/** @deprecated use Nette\Schema\Helpers::validateType() */
|
||||
private function doValidate(mixed $value, string $expected, Context $context): bool
|
||||
{
|
||||
$isOk = $context->createChecker();
|
||||
|
|
@ -148,7 +163,7 @@ trait Base
|
|||
|
||||
|
||||
/**
|
||||
* @deprecated use Nette\Schema\Validators::validateRange()
|
||||
* @deprecated use Nette\Schema\Helpers::validateRange()
|
||||
* @param array{?float, ?float} $range
|
||||
*/
|
||||
private static function doValidateRange(mixed $value, array $range, Context $context, string $types = ''): bool
|
||||
|
|
|
|||
|
|
@ -39,6 +39,9 @@ final class Structure implements Schema
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Not supported for structures; always throws.
|
||||
*/
|
||||
public function default(mixed $value): self
|
||||
{
|
||||
throw new Nette\InvalidStateException('Structure cannot have default value.');
|
||||
|
|
@ -59,6 +62,9 @@ final class Structure implements Schema
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Allows extra keys not defined in the shape, validating their values against the given type.
|
||||
*/
|
||||
public function otherItems(string|Schema $type = 'mixed'): self
|
||||
{
|
||||
$this->otherItems = $type instanceof Schema ? $type : new Type($type);
|
||||
|
|
@ -66,6 +72,9 @@ final class Structure implements Schema
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* When enabled, properties whose value equals the default are omitted from the output.
|
||||
*/
|
||||
public function skipDefaults(bool $state = true): self
|
||||
{
|
||||
$this->skipDefaults = $state;
|
||||
|
|
@ -73,7 +82,10 @@ final class Structure implements Schema
|
|||
}
|
||||
|
||||
|
||||
/** @param Schema[]|self $shape */
|
||||
/**
|
||||
* Creates a new structure by merging this shape with additional properties.
|
||||
* @param Schema[]|self $shape
|
||||
*/
|
||||
public function extend(array|self $shape): self
|
||||
{
|
||||
$shape = $shape instanceof self ? $shape->items : $shape;
|
||||
|
|
@ -203,8 +215,17 @@ final class Structure implements Schema
|
|||
|
||||
public function completeDefault(Context $context): mixed
|
||||
{
|
||||
return $this->required
|
||||
? $this->complete([], $context)
|
||||
: null;
|
||||
if (!$this->required) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// the item is missing in the input, do not report it as used deprecated
|
||||
$deprecated = $this->deprecated;
|
||||
$this->deprecated = null;
|
||||
try {
|
||||
return $this->complete([], $context);
|
||||
} finally {
|
||||
$this->deprecated = $deprecated;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
29
vendor/nette/schema/src/Schema/Elements/Type.php
vendored
29
vendor/nette/schema/src/Schema/Elements/Type.php
vendored
|
|
@ -11,6 +11,7 @@ use Nette\Schema\Context;
|
|||
use Nette\Schema\DynamicParameter;
|
||||
use Nette\Schema\Helpers;
|
||||
use Nette\Schema\Schema;
|
||||
use Nette\Utils\Validators;
|
||||
use function array_key_exists, array_pop, implode, is_array, str_replace, strpos;
|
||||
|
||||
|
||||
|
|
@ -36,6 +37,9 @@ final class Type implements Schema
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Allows the value to be null in addition to the declared type.
|
||||
*/
|
||||
public function nullable(): self
|
||||
{
|
||||
$this->type = 'null|' . $this->type;
|
||||
|
|
@ -43,6 +47,9 @@ final class Type implements Schema
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Controls whether the default value is merged with the input array (enabled by default).
|
||||
*/
|
||||
public function mergeDefaults(bool $state = true): self
|
||||
{
|
||||
$this->merge = $state;
|
||||
|
|
@ -50,6 +57,9 @@ final class Type implements Schema
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Allows the value to be a DynamicParameter, which is recorded for deferred validation.
|
||||
*/
|
||||
public function dynamic(): self
|
||||
{
|
||||
$this->type = DynamicParameter::class . '|' . $this->type;
|
||||
|
|
@ -86,6 +96,9 @@ final class Type implements Schema
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets a regex pattern the string value must match entirely (anchored to start and end).
|
||||
*/
|
||||
public function pattern(?string $pattern): self
|
||||
{
|
||||
$this->pattern = $pattern;
|
||||
|
|
@ -162,7 +175,7 @@ final class Type implements Schema
|
|||
$merge = false;
|
||||
}
|
||||
|
||||
if ($value === null && is_array($this->default)) {
|
||||
if ($value === null && is_array($this->default) && !Validators::is(null, $this->type)) {
|
||||
$value = []; // is unable to distinguish null from array in NEON
|
||||
}
|
||||
|
||||
|
|
@ -173,13 +186,13 @@ final class Type implements Schema
|
|||
$isOk() && Helpers::validateRange($value, $this->range, $context, $this->type);
|
||||
$isOk() && $value !== null && $this->pattern !== null && Helpers::validatePattern($value, $this->pattern, $context);
|
||||
$isOk() && is_array($value) && $this->validateItems($value, $context);
|
||||
$isOk() && $merge && $value = Helpers::merge($value, $this->default);
|
||||
$isOk() && $merge && $value !== null && $value = Helpers::merge($value, $this->default);
|
||||
$isOk() && $value = $this->doTransform($value, $context);
|
||||
if (!$isOk()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($value instanceof DynamicParameter) {
|
||||
if ($value instanceof DynamicParameter && $this->type !== DynamicParameter::class) {
|
||||
$expected = $this->type . ($this->range === [null, null] ? '' : ':' . implode('..', $this->range));
|
||||
$context->dynamics[] = [$value, str_replace(DynamicParameter::class . '|', '', $expected), $context->path];
|
||||
}
|
||||
|
|
@ -190,7 +203,7 @@ final class Type implements Schema
|
|||
/** @param array<mixed> $value */
|
||||
private function validateItems(array &$value, Context $context): void
|
||||
{
|
||||
if (!$this->itemsValue) {
|
||||
if (!($itemsValue = $this->itemsValue)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -198,9 +211,15 @@ final class Type implements Schema
|
|||
foreach ($value as $key => $val) {
|
||||
$context->path[] = $key;
|
||||
$context->isKey = true;
|
||||
$isKeyOk = $context->createChecker();
|
||||
$key = $this->itemsKey ? $this->itemsKey->complete($key, $context) : $key;
|
||||
$context->isKey = false;
|
||||
$res[$key ?? ''] = $this->itemsValue->complete($val, $context);
|
||||
$keyOk = $isKeyOk();
|
||||
$val = $itemsValue->complete($val, $context);
|
||||
if ($keyOk) {
|
||||
$res[$key] = $val;
|
||||
}
|
||||
|
||||
array_pop($context->path);
|
||||
}
|
||||
$value = $res;
|
||||
|
|
|
|||
24
vendor/nette/schema/src/Schema/Expect.php
vendored
24
vendor/nette/schema/src/Schema/Expect.php
vendored
|
|
@ -42,26 +42,38 @@ final class Expect
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a schema for a custom type expression (e.g., 'int|string', 'null|float').
|
||||
*/
|
||||
public static function type(string $type): Type
|
||||
{
|
||||
return new Type($type);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a union schema that accepts any of the given values or sub-schemas.
|
||||
*/
|
||||
public static function anyOf(mixed ...$set): AnyOf
|
||||
{
|
||||
return new AnyOf(...$set);
|
||||
}
|
||||
|
||||
|
||||
/** @param Schema[] $shape */
|
||||
/**
|
||||
* Creates a structure schema with defined properties; output is stdClass.
|
||||
* @param Schema[] $shape
|
||||
*/
|
||||
public static function structure(array $shape): Structure
|
||||
{
|
||||
return new Structure($shape);
|
||||
}
|
||||
|
||||
|
||||
/** @param array<string, Schema> $items */
|
||||
/**
|
||||
* Generates a structure schema from a class instance by reflecting its properties or constructor parameters.
|
||||
* @param array<string, Schema> $items Optional overrides for specific properties.
|
||||
*/
|
||||
public static function from(object $object, array $items = []): Structure
|
||||
{
|
||||
$ro = new \ReflectionObject($object);
|
||||
|
|
@ -95,6 +107,8 @@ final class Expect
|
|||
|
||||
|
||||
/**
|
||||
* Creates an array schema. When passed Schema elements, behaves like structure() but outputs an array.
|
||||
* Without Schema elements, creates a plain array type with the given default value.
|
||||
* @param mixed[] $shape
|
||||
*/
|
||||
public static function array(?array $shape = []): Structure|Type
|
||||
|
|
@ -106,12 +120,18 @@ final class Expect
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates an associative or indexed array schema where every value matches the given type.
|
||||
*/
|
||||
public static function arrayOf(string|Schema $valueType, string|Schema|null $keyType = null): Type
|
||||
{
|
||||
return (new Type('array'))->items($valueType, $keyType);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a list schema (sequentially indexed from 0) where every element matches the given type.
|
||||
*/
|
||||
public static function listOf(string|Schema $type): Type
|
||||
{
|
||||
return (new Type('list'))->items($type);
|
||||
|
|
|
|||
66
vendor/nette/schema/src/Schema/Helpers.php
vendored
66
vendor/nette/schema/src/Schema/Helpers.php
vendored
|
|
@ -9,7 +9,7 @@ namespace Nette\Schema;
|
|||
|
||||
use Nette;
|
||||
use Nette\Utils\Reflection;
|
||||
use function count, explode, get_debug_type, implode, in_array, is_array, is_float, is_int, is_object, is_scalar, is_string, method_exists, preg_match, preg_quote, preg_replace, preg_replace_callback, settype, str_replace, strlen, trim, var_export;
|
||||
use function array_map, count, explode, get_debug_type, implode, in_array, is_array, is_float, is_int, is_object, is_scalar, is_string, is_subclass_of, method_exists, preg_match, preg_quote, preg_replace, preg_replace_callback, settype, str_replace, strlen, trim, var_export;
|
||||
|
||||
|
||||
/**
|
||||
|
|
@ -54,6 +54,9 @@ final class Helpers
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the type of a property or parameter as a string, or null if not determinable.
|
||||
*/
|
||||
public static function getPropertyType(\ReflectionProperty|\ReflectionParameter $prop): ?string
|
||||
{
|
||||
if ($type = Nette\Utils\Type::fromReflection($prop)) {
|
||||
|
|
@ -89,6 +92,9 @@ final class Helpers
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Formats a value for use in error messages (e.g., 'hello', true, object stdClass).
|
||||
*/
|
||||
public static function formatValue(mixed $value): string
|
||||
{
|
||||
if ($value instanceof DynamicParameter) {
|
||||
|
|
@ -105,6 +111,9 @@ final class Helpers
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Adds a TypeMismatch error to the context if the value does not match the expected type.
|
||||
*/
|
||||
public static function validateType(mixed $value, string $expected, Context $context): void
|
||||
{
|
||||
if (!Nette\Utils\Validators::is($value, $expected)) {
|
||||
|
|
@ -119,7 +128,10 @@ final class Helpers
|
|||
}
|
||||
|
||||
|
||||
/** @param array{?float, ?float} $range */
|
||||
/**
|
||||
* Adds a range error to the context if the value (or its length for strings/arrays) is outside the given range.
|
||||
* @param array{?float, ?float} $range
|
||||
*/
|
||||
public static function validateRange(mixed $value, array $range, Context $context, string $types = ''): void
|
||||
{
|
||||
if (is_array($value) || is_string($value)) {
|
||||
|
|
@ -146,7 +158,10 @@ final class Helpers
|
|||
}
|
||||
|
||||
|
||||
/** @param array{?float, ?float} $range */
|
||||
/**
|
||||
* Checks whether a value falls within the given [min, max] range (null means no bound).
|
||||
* @param array{?float, ?float} $range
|
||||
*/
|
||||
public static function isInRange(mixed $value, array $range): bool
|
||||
{
|
||||
return ($range[0] === null || $value >= $range[0])
|
||||
|
|
@ -154,6 +169,9 @@ final class Helpers
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Adds a PatternMismatch error to the context if the value does not match the pattern.
|
||||
*/
|
||||
public static function validatePattern(string $value, string $pattern, Context $context): void
|
||||
{
|
||||
if (!preg_match("\x01^(?:$pattern)$\x01Du", $value)) {
|
||||
|
|
@ -166,7 +184,10 @@ final class Helpers
|
|||
}
|
||||
|
||||
|
||||
/** @return \Closure(mixed): mixed */
|
||||
/**
|
||||
* Returns a closure that casts a value to the given type (built-in, backed enum, class with constructor, or plain class).
|
||||
* @return \Closure(mixed, Context): mixed
|
||||
*/
|
||||
public static function getCastStrategy(string $type): \Closure
|
||||
{
|
||||
if (Nette\Utils\Validators::isBuiltinType($type)) {
|
||||
|
|
@ -174,12 +195,37 @@ final class Helpers
|
|||
settype($value, $type);
|
||||
return $value;
|
||||
};
|
||||
} elseif (method_exists($type, '__construct')) {
|
||||
return static fn($value) => is_array($value) || $value instanceof \stdClass
|
||||
? new $type(...(array) $value)
|
||||
: new $type($value);
|
||||
} else {
|
||||
return static fn($value) => Nette\Utils\Arrays::toObject((array) $value, new $type);
|
||||
|
||||
} elseif (is_subclass_of($type, \BackedEnum::class)) {
|
||||
return static function ($value, Context $context) use ($type) {
|
||||
try {
|
||||
return $type::from($value);
|
||||
} catch (\TypeError | \ValueError) {
|
||||
$context->addError(
|
||||
'The %label% %path% expects to be %expected%, %value% given.',
|
||||
Message::TypeMismatch,
|
||||
['value' => $value, 'expected' => implode('|', array_map(fn(\BackedEnum $case) => self::formatValue($case->value), $type::cases()))],
|
||||
);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
} elseif (is_subclass_of($type, \UnitEnum::class)) {
|
||||
throw new Nette\InvalidStateException("Cannot cast value to pure enum $type.");
|
||||
}
|
||||
|
||||
$factory = method_exists($type, '__construct')
|
||||
? static fn($value) => is_array($value) || $value instanceof \stdClass
|
||||
? new $type(...(array) $value)
|
||||
: new $type($value)
|
||||
: static fn($value) => Nette\Utils\Arrays::toObject((array) $value, new $type);
|
||||
|
||||
return static function ($value) use ($factory, $type) {
|
||||
try {
|
||||
return $factory($value);
|
||||
} catch (\Error $e) {
|
||||
throw new Nette\InvalidStateException("Unable to cast value to $type: " . $e->getMessage(), 0, $e);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
12
vendor/nette/schema/src/Schema/Message.php
vendored
12
vendor/nette/schema/src/Schema/Message.php
vendored
|
|
@ -7,9 +7,12 @@
|
|||
|
||||
namespace Nette\Schema;
|
||||
|
||||
use function implode, preg_replace_callback;
|
||||
use function array_key_exists, implode, preg_replace_callback;
|
||||
|
||||
|
||||
/**
|
||||
* Represents a single validation error or warning with a message template, error code, path, and variables.
|
||||
*/
|
||||
final class Message
|
||||
{
|
||||
/** variables: {value: mixed, expected: string} */
|
||||
|
|
@ -72,6 +75,9 @@ final class Message
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Formats the message template by substituting %variable% placeholders with their values.
|
||||
*/
|
||||
public function toString(): string
|
||||
{
|
||||
$vars = $this->variables;
|
||||
|
|
@ -83,7 +89,9 @@ final class Message
|
|||
|
||||
return preg_replace_callback('~( ?)%(\w+)%~', function ($m) use ($vars) {
|
||||
[, $space, $key] = $m;
|
||||
return $vars[$key] === null ? '' : $space . $vars[$key];
|
||||
return array_key_exists($key, $vars)
|
||||
? ($vars[$key] === null ? '' : $space . $vars[$key])
|
||||
: $m[0];
|
||||
}, $this->message);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
24
vendor/nette/schema/src/Schema/Processor.php
vendored
24
vendor/nette/schema/src/Schema/Processor.php
vendored
|
|
@ -21,6 +21,9 @@ final class Processor
|
|||
private bool $skipDefaults = false;
|
||||
|
||||
|
||||
/**
|
||||
* When enabled, properties with default values are omitted from the output.
|
||||
*/
|
||||
public function skipDefaults(bool $value = true): void
|
||||
{
|
||||
$this->skipDefaults = $value;
|
||||
|
|
@ -35,9 +38,9 @@ final class Processor
|
|||
{
|
||||
$this->createContext();
|
||||
$data = $schema->normalize($data, $this->context);
|
||||
$this->throwsErrors();
|
||||
$this->throwErrors();
|
||||
$data = $schema->complete($data, $this->context);
|
||||
$this->throwsErrors();
|
||||
$this->throwErrors();
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
|
@ -54,30 +57,35 @@ final class Processor
|
|||
$first = true;
|
||||
foreach ($dataset as $data) {
|
||||
$data = $schema->normalize($data, $this->context);
|
||||
$this->throwsErrors();
|
||||
$this->throwErrors();
|
||||
$flatten = $first ? $data : $schema->merge($data, $flatten);
|
||||
$first = false;
|
||||
}
|
||||
|
||||
$data = $schema->complete($flatten, $this->context);
|
||||
$this->throwsErrors();
|
||||
$this->throwErrors();
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
/** @return list<string> */
|
||||
/**
|
||||
* Returns all deprecation warnings collected during the last processing run.
|
||||
* @return list<string>
|
||||
*/
|
||||
public function getWarnings(): array
|
||||
{
|
||||
$res = [];
|
||||
foreach ($this->context->warnings as $message) {
|
||||
$res[] = $message->toString();
|
||||
if (isset($this->context)) {
|
||||
foreach ($this->context->warnings as $message) {
|
||||
$res[] = $message->toString();
|
||||
}
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
|
||||
private function throwsErrors(): void
|
||||
private function throwErrors(): void
|
||||
{
|
||||
if ($this->context->errors) {
|
||||
throw new ValidationException(null, $this->context->errors);
|
||||
|
|
|
|||
10
vendor/nette/schema/src/Schema/Schema.php
vendored
10
vendor/nette/schema/src/Schema/Schema.php
vendored
|
|
@ -8,27 +8,31 @@
|
|||
namespace Nette\Schema;
|
||||
|
||||
|
||||
/**
|
||||
* Defines the contract for schema elements used in data validation and normalization.
|
||||
*/
|
||||
interface Schema
|
||||
{
|
||||
/**
|
||||
* Normalization.
|
||||
* Applies pre-processing transformations to the raw input value (e.g., via before() hooks).
|
||||
* @return mixed
|
||||
*/
|
||||
function normalize(mixed $value, Context $context);
|
||||
|
||||
/**
|
||||
* Merging.
|
||||
* Merges two normalized values, with $value taking priority over $base.
|
||||
* @return mixed
|
||||
*/
|
||||
function merge(mixed $value, mixed $base);
|
||||
|
||||
/**
|
||||
* Validation and finalization.
|
||||
* Validates the value and applies defaults, transforms, and assertions.
|
||||
* @return mixed
|
||||
*/
|
||||
function complete(mixed $value, Context $context);
|
||||
|
||||
/**
|
||||
* Returns the default value, or adds a missing-item error if the field is required.
|
||||
* @return mixed
|
||||
*/
|
||||
function completeDefault(Context $context);
|
||||
|
|
|
|||
|
|
@ -18,13 +18,16 @@ class ValidationException extends Nette\InvalidStateException
|
|||
public function __construct(
|
||||
?string $message,
|
||||
/** @var list<Message> */
|
||||
private array $messages = [],
|
||||
private readonly array $messages = [],
|
||||
) {
|
||||
parent::__construct($message ?? $messages[0]->toString());
|
||||
}
|
||||
|
||||
|
||||
/** @return list<string> */
|
||||
/**
|
||||
* Returns all validation error messages as formatted strings.
|
||||
* @return list<string>
|
||||
*/
|
||||
public function getMessages(): array
|
||||
{
|
||||
$res = [];
|
||||
|
|
@ -36,7 +39,10 @@ class ValidationException extends Nette\InvalidStateException
|
|||
}
|
||||
|
||||
|
||||
/** @return list<Message> */
|
||||
/**
|
||||
* Returns all validation error messages as Message objects.
|
||||
* @return list<Message>
|
||||
*/
|
||||
public function getMessageObjects(): array
|
||||
{
|
||||
return $this->messages;
|
||||
|
|
|
|||
Loading…
Reference in a new issue