🔧 Update npm + login page

This commit is contained in:
TiclemFR
2024-01-25 14:00:48 +01:00
parent 468c2cd0e6
commit 336f2bae93
128 changed files with 3078 additions and 2368 deletions

View File

@@ -15,12 +15,12 @@
}
],
"require": {
"php": "7.1 - 8.3",
"nette/utils": "^2.5.7 || ^3.1.5 || ^4.0"
"php": "8.1 - 8.3",
"nette/utils": "^4.0"
},
"require-dev": {
"nette/tester": "^2.3 || ^2.4",
"tracy/tracy": "^2.7",
"nette/tester": "^2.4",
"tracy/tracy": "^2.8",
"phpstan/phpstan-nette": "^1.0"
},
"autoload": {
@@ -33,7 +33,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.2-dev"
"dev-master": "1.3-dev"
}
}
}

View File

@@ -21,7 +21,7 @@ Installation:
composer require nette/schema
```
It requires PHP version 7.1 and supports PHP up to 8.3.
It requires PHP version 8.1 and supports PHP up to 8.3.
[Support Me](https://github.com/sponsors/dg)
@@ -439,7 +439,7 @@ Expect::structure([
// 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):
If the class has a constructor, the elements of the structure are passed as named parameters to the constructor:
```php
class Info

View File

@@ -9,30 +9,24 @@ declare(strict_types=1);
namespace Nette\Schema;
use Nette;
final class Context
{
use Nette\SmartObject;
/** @var bool */
public $skipDefaults = false;
public bool $skipDefaults = false;
/** @var string[] */
public $path = [];
public array $path = [];
/** @var bool */
public $isKey = false;
public bool $isKey = false;
/** @var Message[] */
public $errors = [];
public array $errors = [];
/** @var Message[] */
public $warnings = [];
public array $warnings = [];
/** @var array[] */
public $dynamics = [];
public array $dynamics = [];
public function addError(string $message, string $code, array $variables = []): Message
@@ -52,8 +46,6 @@ final class Context
public function createChecker(): \Closure
{
$count = count($this->errors);
return function () use ($count): bool {
return $count === count($this->errors);
};
return fn(): bool => $count === count($this->errors);
}
}

View File

@@ -18,16 +18,11 @@ use Nette\Schema\Schema;
final class AnyOf implements Schema
{
use Base;
use Nette\SmartObject;
/** @var array */
private $set;
private array $set;
/**
* @param mixed|Schema ...$set
*/
public function __construct(...$set)
public function __construct(mixed ...$set)
{
if (!$set) {
throw new Nette\InvalidStateException('The enumeration must not be empty.');
@@ -61,13 +56,13 @@ final class AnyOf implements Schema
/********************* processing ****************d*g**/
public function normalize($value, Context $context)
public function normalize(mixed $value, Context $context): mixed
{
return $this->doNormalize($value, $context);
}
public function merge($value, $base)
public function merge(mixed $value, mixed $base): mixed
{
if (is_array($value) && isset($value[Helpers::PreventMerging])) {
unset($value[Helpers::PreventMerging]);
@@ -78,7 +73,7 @@ final class AnyOf implements Schema
}
public function complete($value, Context $context)
public function complete(mixed $value, Context $context): mixed
{
$isOk = $context->createChecker();
$value = $this->findAlternative($value, $context);
@@ -87,7 +82,7 @@ final class AnyOf implements Schema
}
private function findAlternative($value, Context $context)
private function findAlternative(mixed $value, Context $context): mixed
{
$expecteds = $innerErrors = [];
foreach ($this->set as $item) {
@@ -125,18 +120,20 @@ final class AnyOf implements Schema
[
'value' => $value,
'expected' => implode('|', array_unique($expecteds)),
]
],
);
}
return null;
}
public function completeDefault(Context $context)
public function completeDefault(Context $context): mixed
{
if ($this->required) {
$context->addError(
'The mandatory item %path% is missing.',
Nette\Schema\Message::MissingItem
Nette\Schema\Message::MissingItem,
);
return null;
}

View File

@@ -19,23 +19,18 @@ use Nette\Schema\Helpers;
*/
trait Base
{
/** @var bool */
private $required = false;
private bool $required = false;
private mixed $default = null;
/** @var mixed */
private $default;
/** @var callable|null */
/** @var ?callable */
private $before;
/** @var callable[] */
private $transforms = [];
/** @var string|null */
private $deprecated;
private array $transforms = [];
private ?string $deprecated = null;
public function default($value): self
public function default(mixed $value): self
{
$this->default = $value;
return $this;
@@ -79,7 +74,7 @@ trait Base
$context->addError(
'Failed assertion ' . ($description ? "'%assertion%'" : '%assertion%') . ' for %label% %path% with value %value%.',
Nette\Schema\Message::FailedAssertion,
['value' => $value, 'assertion' => $expected]
['value' => $value, 'assertion' => $expected],
);
});
}
@@ -93,12 +88,12 @@ trait Base
}
public function completeDefault(Context $context)
public function completeDefault(Context $context): mixed
{
if ($this->required) {
$context->addError(
'The mandatory item %path% is missing.',
Nette\Schema\Message::MissingItem
Nette\Schema\Message::MissingItem,
);
return null;
}
@@ -107,7 +102,7 @@ trait Base
}
public function doNormalize($value, Context $context)
public function doNormalize(mixed $value, Context $context): mixed
{
if ($this->before) {
$value = ($this->before)($value);
@@ -122,13 +117,13 @@ trait Base
if ($this->deprecated !== null) {
$context->addWarning(
$this->deprecated,
Nette\Schema\Message::Deprecated
Nette\Schema\Message::Deprecated,
);
}
}
private function doTransform($value, Context $context)
private function doTransform(mixed $value, Context $context): mixed
{
$isOk = $context->createChecker();
foreach ($this->transforms as $handler) {
@@ -142,7 +137,7 @@ trait Base
/** @deprecated use Nette\Schema\Validators::validateType() */
private function doValidate($value, string $expected, Context $context): bool
private function doValidate(mixed $value, string $expected, Context $context): bool
{
$isOk = $context->createChecker();
Helpers::validateType($value, $expected, $context);
@@ -151,7 +146,7 @@ trait Base
/** @deprecated use Nette\Schema\Validators::validateRange() */
private static function doValidateRange($value, array $range, Context $context, string $types = ''): bool
private static function doValidateRange(mixed $value, array $range, Context $context, string $types = ''): bool
{
$isOk = $context->createChecker();
Helpers::validateRange($value, $range, $context, $types);
@@ -160,7 +155,7 @@ trait Base
/** @deprecated use doTransform() */
private function doFinalize($value, Context $context)
private function doFinalize(mixed $value, Context $context): mixed
{
return $this->doTransform($value, $context);
}

View File

@@ -18,19 +18,16 @@ use Nette\Schema\Schema;
final class Structure implements Schema
{
use Base;
use Nette\SmartObject;
/** @var Schema[] */
private $items;
private array $items;
/** @var Schema|null for array|list */
private $otherItems;
/** for array|list */
private ?Schema $otherItems = null;
/** @var array{?int, ?int} */
private $range = [null, null];
/** @var bool */
private $skipDefaults = false;
private array $range = [null, null];
private bool $skipDefaults = false;
/**
@@ -45,7 +42,7 @@ final class Structure implements Schema
}
public function default($value): self
public function default(mixed $value): self
{
throw new Nette\InvalidStateException('Structure cannot have default value.');
}
@@ -65,10 +62,7 @@ final class Structure implements Schema
}
/**
* @param string|Schema $type
*/
public function otherItems($type = 'mixed'): self
public function otherItems(string|Schema $type = 'mixed'): self
{
$this->otherItems = $type instanceof Schema ? $type : new Type($type);
return $this;
@@ -85,7 +79,7 @@ final class Structure implements Schema
/********************* processing ****************d*g**/
public function normalize($value, Context $context)
public function normalize(mixed $value, Context $context): mixed
{
if ($prevent = (is_array($value) && isset($value[Helpers::PreventMerging]))) {
unset($value[Helpers::PreventMerging]);
@@ -115,7 +109,7 @@ final class Structure implements Schema
}
public function merge($value, $base)
public function merge(mixed $value, mixed $base): mixed
{
if (is_array($value) && isset($value[Helpers::PreventMerging])) {
unset($value[Helpers::PreventMerging]);
@@ -145,7 +139,7 @@ final class Structure implements Schema
}
public function complete($value, Context $context)
public function complete(mixed $value, Context $context): mixed
{
if ($value === null) {
$value = []; // is unable to distinguish null from array in NEON
@@ -171,11 +165,11 @@ final class Structure implements Schema
} else {
$keys = array_map('strval', array_keys($items));
foreach ($extraKeys as $key) {
$hint = Nette\Utils\ObjectHelpers::getSuggestion($keys, (string) $key);
$hint = Nette\Utils\Helpers::getSuggestion($keys, (string) $key);
$context->addError(
'Unexpected item %path%' . ($hint ? ", did you mean '%hint%'?" : '.'),
Nette\Schema\Message::UnexpectedItem,
['hint' => $hint]
['hint' => $hint],
)->path[] = $key;
}
}
@@ -197,7 +191,7 @@ final class Structure implements Schema
}
public function completeDefault(Context $context)
public function completeDefault(Context $context): mixed
{
return $this->required
? $this->complete([], $context)

View File

@@ -9,7 +9,6 @@ declare(strict_types=1);
namespace Nette\Schema\Elements;
use Nette;
use Nette\Schema\Context;
use Nette\Schema\DynamicParameter;
use Nette\Schema\Helpers;
@@ -19,25 +18,15 @@ use Nette\Schema\Schema;
final class Type implements Schema
{
use Base;
use Nette\SmartObject;
/** @var string */
private $type;
/** @var Schema|null for arrays */
private $itemsValue;
/** @var Schema|null for arrays */
private $itemsKey;
private string $type;
private ?Schema $itemsValue = null;
private ?Schema $itemsKey = null;
/** @var array{?float, ?float} */
private $range = [null, null];
/** @var string|null */
private $pattern;
/** @var bool */
private $merge = true;
private array $range = [null, null];
private ?string $pattern = null;
private bool $merge = true;
public function __construct(string $type)
@@ -84,11 +73,9 @@ final class Type implements Schema
/**
* @param string|Schema $valueType
* @param string|Schema|null $keyType
* @internal use arrayOf() or listOf()
*/
public function items($valueType = 'mixed', $keyType = null): self
public function items(string|Schema $valueType = 'mixed', string|Schema $keyType = null): self
{
$this->itemsValue = $valueType instanceof Schema
? $valueType
@@ -110,7 +97,7 @@ final class Type implements Schema
/********************* processing ****************d*g**/
public function normalize($value, Context $context)
public function normalize(mixed $value, Context $context): mixed
{
if ($prevent = (is_array($value) && isset($value[Helpers::PreventMerging]))) {
unset($value[Helpers::PreventMerging]);
@@ -141,7 +128,7 @@ final class Type implements Schema
}
public function merge($value, $base)
public function merge(mixed $value, mixed $base): mixed
{
if (is_array($value) && isset($value[Helpers::PreventMerging])) {
unset($value[Helpers::PreventMerging]);
@@ -168,7 +155,7 @@ final class Type implements Schema
}
public function complete($value, Context $context)
public function complete(mixed $value, Context $context): mixed
{
$merge = $this->merge;
if (is_array($value) && isset($value[Helpers::PreventMerging])) {

View File

@@ -32,8 +32,6 @@ use Nette\Schema\Elements\Type;
*/
final class Expect
{
use Nette\SmartObject;
public static function __callStatic(string $name, array $args): Type
{
$type = new Type($name);
@@ -51,10 +49,7 @@ final class Expect
}
/**
* @param mixed|Schema ...$set
*/
public static function anyOf(...$set): AnyOf
public static function anyOf(mixed ...$set): AnyOf
{
return new AnyOf(...$set);
}
@@ -69,21 +64,20 @@ final class Expect
}
/**
* @param object $object
*/
public static function from($object, array $items = []): Structure
public static function from(object $object, array $items = []): Structure
{
$ro = new \ReflectionObject($object);
foreach ($ro->getProperties() as $prop) {
$type = Helpers::getPropertyType($prop) ?? 'mixed';
$props = $ro->hasMethod('__construct')
? $ro->getMethod('__construct')->getParameters()
: $ro->getProperties();
foreach ($props as $prop) {
$item = &$items[$prop->getName()];
if (!$item) {
$type = Helpers::getPropertyType($prop) ?? 'mixed';
$item = new Type($type);
if (PHP_VERSION_ID >= 70400 && !$prop->isInitialized($object)) {
$item->required();
} else {
$def = $prop->getValue($object);
if ($prop instanceof \ReflectionProperty ? $prop->isInitialized($object) : $prop->isOptional()) {
$def = ($prop instanceof \ReflectionProperty ? $prop->getValue($object) : $prop->getDefaultValue());
if (is_object($def)) {
$item = static::from($def);
} elseif ($def === null && !Nette\Utils\Validators::is(null, $type)) {
@@ -91,6 +85,8 @@ final class Expect
} else {
$item->default($def);
}
} else {
$item->required();
}
}
}
@@ -99,20 +95,13 @@ final class Expect
}
/**
* @param string|Schema $valueType
* @param string|Schema|null $keyType
*/
public static function arrayOf($valueType, $keyType = null): Type
public static function arrayOf(string|Schema $valueType, string|Schema $keyType = null): Type
{
return (new Type('array'))->items($valueType, $keyType);
}
/**
* @param string|Schema $type
*/
public static function listOf($type): Type
public static function listOf(string|Schema $type): Type
{
return (new Type('list'))->items($type);
}

View File

@@ -21,14 +21,12 @@ final class Helpers
use Nette\StaticClass;
public const PreventMerging = '_prevent_merging';
public const PREVENT_MERGING = self::PreventMerging;
/**
* Merges dataset. Left has higher priority than right one.
* @return array|string
*/
public static function merge($value, $base)
public static function merge(mixed $value, mixed $base): mixed
{
if (is_array($value) && isset($value[self::PreventMerging])) {
unset($value[self::PreventMerging]);
@@ -57,17 +55,16 @@ final class Helpers
}
public static function getPropertyType(\ReflectionProperty $prop): ?string
public static function getPropertyType(\ReflectionProperty|\ReflectionParameter $prop): ?string
{
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)) {
if ($type = Nette\Utils\Type::fromReflection($prop)) {
return (string) $type;
} elseif ($type = preg_replace('#\s.*#', '', (string) self::parseAnnotation($prop, 'var'))) {
} elseif (
($prop instanceof \ReflectionProperty)
&& ($type = preg_replace('#\s.*#', '', (string) self::parseAnnotation($prop, 'var')))
) {
$class = Reflection::getPropertyDeclaringClass($prop);
return preg_replace_callback('#[\w\\\\]+#', function ($m) use ($class) {
return Reflection::expandClassName($m[0], $class);
}, $type);
return preg_replace_callback('#[\w\\\\]+#', fn($m) => Reflection::expandClassName($m[0], $class), $type);
}
return null;
@@ -93,24 +90,23 @@ final class Helpers
}
/**
* @param mixed $value
*/
public static function formatValue($value): string
public static function formatValue(mixed $value): string
{
if (is_object($value)) {
return 'object ' . get_class($value);
if ($value instanceof DynamicParameter) {
return 'dynamic';
} elseif (is_object($value)) {
return 'object ' . $value::class;
} elseif (is_string($value)) {
return "'" . Nette\Utils\Strings::truncate($value, 15, '...') . "'";
} elseif (is_scalar($value)) {
return var_export($value, true);
return var_export($value, return: true);
} else {
return strtolower(gettype($value));
return get_debug_type($value);
}
}
public static function validateType($value, string $expected, Context $context): void
public static function validateType(mixed $value, string $expected, Context $context): void
{
if (!Nette\Utils\Validators::is($value, $expected)) {
$expected = str_replace(DynamicParameter::class . '|', '', $expected);
@@ -118,13 +114,13 @@ final class Helpers
$context->addError(
'The %label% %path% expects to be %expected%, %value% given.',
Message::TypeMismatch,
['value' => $value, 'expected' => $expected]
['value' => $value, 'expected' => $expected],
);
}
}
public static function validateRange($value, array $range, Context $context, string $types = ''): void
public static function validateRange(mixed $value, array $range, Context $context, string $types = ''): void
{
if (is_array($value) || is_string($value)) {
[$length, $label] = is_array($value)
@@ -137,20 +133,20 @@ final class Helpers
$context->addError(
"The length of %label% %path% expects to be in range %expected%, %length% $label given.",
Message::LengthOutOfRange,
['value' => $value, 'length' => $length, 'expected' => implode('..', $range)]
['value' => $value, 'length' => $length, 'expected' => implode('..', $range)],
);
}
} elseif ((is_int($value) || is_float($value)) && !self::isInRange($value, $range)) {
$context->addError(
'The %label% %path% expects to be in range %expected%, %value% given.',
Message::ValueOutOfRange,
['value' => $value, 'expected' => implode('..', $range)]
['value' => $value, 'expected' => implode('..', $range)],
);
}
}
public static function isInRange($value, array $range): bool
public static function isInRange(mixed $value, array $range): bool
{
return ($range[0] === null || $value >= $range[0])
&& ($range[1] === null || $value <= $range[1]);
@@ -163,7 +159,7 @@ final class Helpers
$context->addError(
"The %label% %path% expects to match pattern '%pattern%', %value% given.",
Message::PatternMismatch,
['value' => $value, 'pattern' => $pattern]
['value' => $value, 'pattern' => $pattern],
);
}
}
@@ -177,22 +173,11 @@ final class Helpers
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);
};
return static fn($value) => is_array($value) || $value instanceof \stdClass
? new $type(...(array) $value)
: new $type($value);
} else {
return static function ($value) use ($type) {
$object = new $type;
foreach ($value as $k => $v) {
$object->$k = $v;
}
return $object;
};
return static fn($value) => Nette\Utils\Arrays::toObject((array) $value, new $type);
}
}
}

View File

@@ -14,8 +14,6 @@ use Nette;
final class Message
{
use Nette\SmartObject;
/** variables: {value: mixed, expected: string} */
public const TypeMismatch = 'schema.typeMismatch';
@@ -40,27 +38,38 @@ final class Message
/** no variables */
public const Deprecated = 'schema.deprecated';
/** Deprecated */
/** @deprecated use Message::TypeMismatch */
public const TYPE_MISMATCH = self::TypeMismatch;
/** @deprecated use Message::ValueOutOfRange */
public const VALUE_OUT_OF_RANGE = self::ValueOutOfRange;
/** @deprecated use Message::LengthOutOfRange */
public const LENGTH_OUT_OF_RANGE = self::LengthOutOfRange;
/** @deprecated use Message::PatternMismatch */
public const PATTERN_MISMATCH = self::PatternMismatch;
/** @deprecated use Message::FailedAssertion */
public const FAILED_ASSERTION = self::FailedAssertion;
/** @deprecated use Message::MissingItem */
public const MISSING_ITEM = self::MissingItem;
/** @deprecated use Message::UnexpectedItem */
public const UNEXPECTED_ITEM = self::UnexpectedItem;
/** @deprecated use Message::Deprecated */
public const DEPRECATED = self::Deprecated;
/** @var string */
public $message;
/** @var string */
public $code;
public string $message;
public string $code;
/** @var string[] */
public $path;
public array $path;
/** @var string[] */
public $variables;
public array $variables;
public function __construct(string $message, string $code, array $path, array $variables = [])
@@ -84,6 +93,6 @@ final class Message
return preg_replace_callback('~( ?)%(\w+)%~', function ($m) use ($vars) {
[, $space, $key] = $m;
return $vars[$key] === null ? '' : $space . $vars[$key];
}, $this->message);
}, $this->message) ?? throw new Nette\InvalidStateException(preg_last_error_msg());
}
}

View File

@@ -17,19 +17,12 @@ use Nette;
*/
final class Processor
{
use Nette\SmartObject;
/** @var array */
public $onNewContext = [];
/** @var Context|null */
private $context;
/** @var bool */
private $skipDefaults;
public array $onNewContext = [];
private Context $context;
private bool $skipDefaults = false;
public function skipDefaults(bool $value = true)
public function skipDefaults(bool $value = true): void
{
$this->skipDefaults = $value;
}
@@ -37,10 +30,9 @@ final class Processor
/**
* Normalizes and validates data. Result is a clean completed data.
* @return mixed
* @throws ValidationException
*/
public function process(Schema $schema, $data)
public function process(Schema $schema, mixed $data): mixed
{
$this->createContext();
$data = $schema->normalize($data, $this->context);
@@ -53,10 +45,9 @@ final class Processor
/**
* Normalizes and validates and merges multiple data. Result is a clean completed data.
* @return mixed
* @throws ValidationException
*/
public function processMultiple(Schema $schema, array $dataset)
public function processMultiple(Schema $schema, array $dataset): mixed
{
$this->createContext();
$flatten = null;
@@ -96,10 +87,10 @@ final class Processor
}
private function createContext()
private function createContext(): void
{
$this->context = new Context;
$this->context->skipDefaults = $this->skipDefaults;
$this->onNewContext($this->context);
Nette\Utils\Arrays::invoke($this->onNewContext, $this->context);
}
}

View File

@@ -16,19 +16,19 @@ interface Schema
* Normalization.
* @return mixed
*/
function normalize($value, Context $context);
function normalize(mixed $value, Context $context);
/**
* Merging.
* @return mixed
*/
function merge($value, $base);
function merge(mixed $value, mixed $base);
/**
* Validation and finalization.
* @return mixed
*/
function complete($value, Context $context);
function complete(mixed $value, Context $context);
/**
* @return mixed

View File

@@ -18,7 +18,7 @@ use Nette;
class ValidationException extends Nette\InvalidStateException
{
/** @var Message[] */
private $messages;
private array $messages;
/**