mirror of
https://github.com/Respect/Validation.git
synced 2024-06-07 16:22:16 +02:00
Create transformers for deprecated rules
For now, those transformers will help transition from the current major version to the next. As I'm making so many changes to the behaviour of the rules and renaming others, it could be overwhelming to update all of them suddenly. The Transformers will change the rules' specifications before the Factory creates them, allowing us to keep the behaviour of the old rules -- even if a little bit different -- while triggering E_USER_DEPRECATED errors to indicate that the client needs to update their code until the next major version. However, those transformers will do more shortly, for example, allowing us to prefix rules to make it easier to create complex rules, like `notPositive()` or even `keyBetween('foo', 1, 10)` and so on. Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
fe68eab37d
commit
1e2c1bfe30
|
@ -21,6 +21,14 @@ use Respect\Validation\Message\Parameter\Stringify;
|
|||
use Respect\Validation\Message\Parameter\Trans;
|
||||
use Respect\Validation\Message\TemplateCollector;
|
||||
use Respect\Validation\Message\TemplateRenderer;
|
||||
use Respect\Validation\Transformers\DeprecatedAttribute;
|
||||
use Respect\Validation\Transformers\DeprecatedKey;
|
||||
use Respect\Validation\Transformers\DeprecatedKeyNested;
|
||||
use Respect\Validation\Transformers\DeprecatedKeyValue;
|
||||
use Respect\Validation\Transformers\DeprecatedMinAndMax;
|
||||
use Respect\Validation\Transformers\DeprecatedType;
|
||||
use Respect\Validation\Transformers\RuleSpec;
|
||||
use Respect\Validation\Transformers\Transformer;
|
||||
|
||||
use function lcfirst;
|
||||
use function sprintf;
|
||||
|
@ -43,6 +51,8 @@ final class Factory
|
|||
|
||||
private TemplateCollector $templateCollector;
|
||||
|
||||
private Transformer $transformer;
|
||||
|
||||
private static Factory $defaultInstance;
|
||||
|
||||
public function __construct()
|
||||
|
@ -50,6 +60,15 @@ final class Factory
|
|||
$this->translator = static fn (string $message) => $message;
|
||||
$this->processor = new Raw(new Trans($this->translator, new Stringify()));
|
||||
$this->templateCollector = new TemplateCollector();
|
||||
$this->transformer = new DeprecatedAttribute(
|
||||
new DeprecatedKey(
|
||||
new DeprecatedKeyValue(
|
||||
new DeprecatedMinAndMax(
|
||||
new DeprecatedKeyNested(new DeprecatedType())
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public static function getDefaultInstance(): self
|
||||
|
@ -101,17 +120,18 @@ final class Factory
|
|||
*/
|
||||
public function rule(string $ruleName, array $arguments = []): Validatable
|
||||
{
|
||||
$ruleSpec = $this->transformer->transform(new RuleSpec($ruleName, $arguments));
|
||||
foreach ($this->rulesNamespaces as $namespace) {
|
||||
try {
|
||||
/** @var class-string<Validatable> $name */
|
||||
$name = $namespace . '\\' . ucfirst($ruleName);
|
||||
$name = $namespace . '\\' . ucfirst($ruleSpec->name);
|
||||
/** @var Validatable $rule */
|
||||
$rule = $this
|
||||
->createReflectionClass($name, Validatable::class)
|
||||
->newInstanceArgs($arguments);
|
||||
->newInstanceArgs($ruleSpec->arguments);
|
||||
|
||||
return $rule;
|
||||
} catch (ReflectionException $exception) {
|
||||
} catch (ReflectionException) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,72 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Exceptions\ComponentException;
|
||||
use Respect\Validation\Message\Template;
|
||||
|
||||
use function array_keys;
|
||||
use function gettype;
|
||||
use function implode;
|
||||
use function is_callable;
|
||||
use function sprintf;
|
||||
|
||||
#[Template(
|
||||
'{{name}} must be {{type}}',
|
||||
'{{name}} must not be {{type}}',
|
||||
)]
|
||||
final class Type extends AbstractRule
|
||||
{
|
||||
private const AVAILABLE_TYPES = [
|
||||
'array' => 'array',
|
||||
'bool' => 'boolean',
|
||||
'boolean' => 'boolean',
|
||||
'callable' => 'callable',
|
||||
'double' => 'double',
|
||||
'float' => 'double',
|
||||
'int' => 'integer',
|
||||
'integer' => 'integer',
|
||||
'null' => 'NULL',
|
||||
'object' => 'object',
|
||||
'resource' => 'resource',
|
||||
'string' => 'string',
|
||||
];
|
||||
|
||||
public function __construct(
|
||||
private readonly string $type
|
||||
) {
|
||||
if (!isset(self::AVAILABLE_TYPES[$type])) {
|
||||
throw new ComponentException(
|
||||
sprintf(
|
||||
'"%s" is not a valid type (Available: %s)',
|
||||
$type,
|
||||
implode(', ', array_keys(self::AVAILABLE_TYPES))
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function validate(mixed $input): bool
|
||||
{
|
||||
if ($this->type === 'callable') {
|
||||
return is_callable($input);
|
||||
}
|
||||
|
||||
return self::AVAILABLE_TYPES[$this->type] === gettype($input);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function getParams(): array
|
||||
{
|
||||
return ['type' => $this->type];
|
||||
}
|
||||
}
|
65
library/Transformers/DeprecatedAttribute.php
Normal file
65
library/Transformers/DeprecatedAttribute.php
Normal file
|
@ -0,0 +1,65 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Respect\Validation\Transformers;
|
||||
|
||||
use function array_key_exists;
|
||||
use function trigger_error;
|
||||
|
||||
use const E_USER_DEPRECATED;
|
||||
|
||||
final class DeprecatedAttribute implements Transformer
|
||||
{
|
||||
public function __construct(
|
||||
private readonly Transformer $next
|
||||
) {
|
||||
}
|
||||
|
||||
public function transform(RuleSpec $ruleSpec): RuleSpec
|
||||
{
|
||||
if ($ruleSpec->name !== 'attribute') {
|
||||
return $this->next->transform($ruleSpec);
|
||||
}
|
||||
|
||||
$arguments = $ruleSpec->arguments;
|
||||
$firstMessage = 'The attribute() rule has been deprecated and will be removed in the next major version.';
|
||||
if (!array_key_exists(1, $arguments)) {
|
||||
trigger_error(
|
||||
$firstMessage . ' Use propertyExists() instead.',
|
||||
E_USER_DEPRECATED
|
||||
);
|
||||
$name = 'propertyExists';
|
||||
} elseif (array_key_exists(2, $arguments)) {
|
||||
$name = $arguments[2] ? 'property' : 'propertyOptional';
|
||||
unset($arguments[2]);
|
||||
|
||||
if ($name === 'propertyOptional') {
|
||||
trigger_error(
|
||||
$firstMessage . ' Use propertyOptional() instead.',
|
||||
E_USER_DEPRECATED
|
||||
);
|
||||
}
|
||||
|
||||
if ($name === 'property') {
|
||||
trigger_error(
|
||||
$firstMessage . ' Use property() without it.',
|
||||
E_USER_DEPRECATED
|
||||
);
|
||||
}
|
||||
} else {
|
||||
trigger_error(
|
||||
$firstMessage . ' Use property() instead.',
|
||||
E_USER_DEPRECATED
|
||||
);
|
||||
$name = 'property';
|
||||
}
|
||||
|
||||
return new RuleSpec($name, $arguments);
|
||||
}
|
||||
}
|
63
library/Transformers/DeprecatedKey.php
Normal file
63
library/Transformers/DeprecatedKey.php
Normal file
|
@ -0,0 +1,63 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Respect\Validation\Transformers;
|
||||
|
||||
use function array_key_exists;
|
||||
use function trigger_error;
|
||||
|
||||
use const E_USER_DEPRECATED;
|
||||
|
||||
final class DeprecatedKey implements Transformer
|
||||
{
|
||||
public function __construct(
|
||||
private readonly Transformer $next
|
||||
) {
|
||||
}
|
||||
|
||||
public function transform(RuleSpec $ruleSpec): RuleSpec
|
||||
{
|
||||
if ($ruleSpec->name !== 'key') {
|
||||
return $this->next->transform($ruleSpec);
|
||||
}
|
||||
|
||||
$name = $ruleSpec->name;
|
||||
$arguments = $ruleSpec->arguments;
|
||||
|
||||
if (!array_key_exists(1, $arguments)) {
|
||||
trigger_error(
|
||||
'Calling key() without a second parameter has been deprecated, ' .
|
||||
'and will be not be allowed in the next major version. Use keyExists() instead.',
|
||||
E_USER_DEPRECATED
|
||||
);
|
||||
$name = 'keyExists';
|
||||
} elseif (array_key_exists(2, $arguments)) {
|
||||
$name = $arguments[2] ? 'key' : 'keyOptional';
|
||||
unset($arguments[2]);
|
||||
|
||||
if ($name === 'keyOptional') {
|
||||
trigger_error(
|
||||
'Calling key() with a third parameter has been deprecated, ' .
|
||||
'and will be not be allowed in the next major version. Use keyOptional() instead.',
|
||||
E_USER_DEPRECATED
|
||||
);
|
||||
}
|
||||
|
||||
if ($name === 'key') {
|
||||
trigger_error(
|
||||
'Calling key() with a third parameter has been deprecated, ' .
|
||||
'and will be not be allowed in the next major version. Use key() without it the third parameter.',
|
||||
E_USER_DEPRECATED
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return new RuleSpec($name, $arguments);
|
||||
}
|
||||
}
|
101
library/Transformers/DeprecatedKeyNested.php
Normal file
101
library/Transformers/DeprecatedKeyNested.php
Normal file
|
@ -0,0 +1,101 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Respect\Validation\Transformers;
|
||||
|
||||
use Respect\Validation\Rules\ArrayVal;
|
||||
use Respect\Validation\Rules\Key;
|
||||
use Respect\Validation\Rules\KeyExists;
|
||||
use Respect\Validation\Rules\KeyOptional;
|
||||
use Respect\Validation\Rules\Property;
|
||||
use Respect\Validation\Rules\PropertyExists;
|
||||
use Respect\Validation\Rules\PropertyOptional;
|
||||
use Respect\Validation\Rules\When;
|
||||
use Respect\Validation\Validatable;
|
||||
|
||||
use function array_pop;
|
||||
use function array_reduce;
|
||||
use function array_reverse;
|
||||
use function explode;
|
||||
use function trigger_error;
|
||||
use function trim;
|
||||
|
||||
use const E_USER_DEPRECATED;
|
||||
|
||||
final class DeprecatedKeyNested implements Transformer
|
||||
{
|
||||
public function __construct(
|
||||
private readonly Transformer $next
|
||||
) {
|
||||
}
|
||||
|
||||
public function transform(RuleSpec $ruleSpec): RuleSpec
|
||||
{
|
||||
if ($ruleSpec->name !== 'keyNested') {
|
||||
return $this->next->transform($ruleSpec);
|
||||
}
|
||||
|
||||
trigger_error(
|
||||
'The keyNested() rule is deprecated and will be removed in the next major version. ' .
|
||||
'Use nested key() or property() instead.',
|
||||
E_USER_DEPRECATED
|
||||
);
|
||||
|
||||
$key = $ruleSpec->arguments[0] ?? '';
|
||||
$rule = $ruleSpec->arguments[1] ?? null;
|
||||
$mandatory = $ruleSpec->arguments[2] ?? true;
|
||||
$pieces = array_reverse(explode('.', trim($key, '.')));
|
||||
$firstKey = array_pop($pieces);
|
||||
$arrayVal = new ArrayVal();
|
||||
$firstRule = array_reduce(
|
||||
$pieces,
|
||||
fn (?Validatable $rule, string $piece) => new When(
|
||||
$arrayVal,
|
||||
$this->createKeyRule($piece, $mandatory, $rule),
|
||||
$this->createPropertyRule($piece, $mandatory, $rule),
|
||||
),
|
||||
$rule,
|
||||
);
|
||||
|
||||
return new RuleSpec(
|
||||
'when',
|
||||
[
|
||||
$arrayVal,
|
||||
$this->createKeyRule($firstKey, $mandatory, $firstRule),
|
||||
$this->createPropertyRule($firstKey, $mandatory, $firstRule),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
private function createPropertyRule(string $name, bool $mandatory, ?Validatable $rule): Validatable
|
||||
{
|
||||
if ($rule === null) {
|
||||
return new PropertyExists($name);
|
||||
}
|
||||
|
||||
if ($mandatory) {
|
||||
return new Property($name, $rule);
|
||||
}
|
||||
|
||||
return new PropertyOptional($name, $rule);
|
||||
}
|
||||
|
||||
private function createKeyRule(string $key, bool $mandatory, ?Validatable $rule): Validatable
|
||||
{
|
||||
if ($rule === null) {
|
||||
return new KeyExists($key);
|
||||
}
|
||||
|
||||
if ($mandatory) {
|
||||
return new Key($key, $rule);
|
||||
}
|
||||
|
||||
return new KeyOptional($key, $rule);
|
||||
}
|
||||
}
|
67
library/Transformers/DeprecatedKeyValue.php
Normal file
67
library/Transformers/DeprecatedKeyValue.php
Normal file
|
@ -0,0 +1,67 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Respect\Validation\Transformers;
|
||||
|
||||
use Respect\Validation\Rules\AlwaysInvalid;
|
||||
use Respect\Validation\Rules\Key;
|
||||
use Respect\Validation\Rules\KeyExists;
|
||||
use Respect\Validation\Rules\Lazy;
|
||||
use Respect\Validation\Validator;
|
||||
use Throwable;
|
||||
|
||||
use function sprintf;
|
||||
use function trigger_error;
|
||||
|
||||
use const E_USER_DEPRECATED;
|
||||
|
||||
final class DeprecatedKeyValue implements Transformer
|
||||
{
|
||||
public function __construct(
|
||||
private readonly Transformer $next
|
||||
) {
|
||||
}
|
||||
|
||||
public function transform(RuleSpec $ruleSpec): RuleSpec
|
||||
{
|
||||
if ($ruleSpec->name !== 'keyValue') {
|
||||
return $this->next->transform($ruleSpec);
|
||||
}
|
||||
|
||||
trigger_error(
|
||||
'The keyValue() rule has been deprecated and will be removed in the next major version. ' .
|
||||
'Use nested lazy() instead.',
|
||||
E_USER_DEPRECATED
|
||||
);
|
||||
|
||||
[$comparedKey, $ruleName, $baseKey] = $ruleSpec->arguments;
|
||||
|
||||
return new RuleSpec('consecutive', [
|
||||
new KeyExists($comparedKey),
|
||||
new KeyExists($baseKey),
|
||||
new Lazy(
|
||||
static function ($input) use ($comparedKey, $ruleName, $baseKey) {
|
||||
try {
|
||||
return new Key($comparedKey, Validator::__callStatic($ruleName, [$input[$baseKey]]));
|
||||
} catch (Throwable) {
|
||||
$rule = new AlwaysInvalid();
|
||||
$rule->setName($comparedKey);
|
||||
$rule->setTemplate(sprintf(
|
||||
'%s must be valid to validate %s',
|
||||
$baseKey,
|
||||
$comparedKey,
|
||||
));
|
||||
|
||||
return $rule;
|
||||
}
|
||||
}
|
||||
),
|
||||
]);
|
||||
}
|
||||
}
|
55
library/Transformers/DeprecatedMinAndMax.php
Normal file
55
library/Transformers/DeprecatedMinAndMax.php
Normal file
|
@ -0,0 +1,55 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Respect\Validation\Transformers;
|
||||
|
||||
use Respect\Validation\Validatable;
|
||||
|
||||
use function in_array;
|
||||
use function sprintf;
|
||||
use function trigger_error;
|
||||
|
||||
use const E_USER_DEPRECATED;
|
||||
|
||||
final class DeprecatedMinAndMax implements Transformer
|
||||
{
|
||||
public function __construct(
|
||||
private readonly Transformer $next
|
||||
) {
|
||||
}
|
||||
|
||||
public function transform(RuleSpec $ruleSpec): RuleSpec
|
||||
{
|
||||
if (!in_array($ruleSpec->name, ['min', 'max'])) {
|
||||
return $this->next->transform($ruleSpec);
|
||||
}
|
||||
|
||||
if (isset($ruleSpec->arguments[0]) && $ruleSpec->arguments[0] instanceof Validatable) {
|
||||
return $this->next->transform($ruleSpec);
|
||||
}
|
||||
|
||||
// @phpstan-ignore-next-line
|
||||
$name = match ($ruleSpec->name) {
|
||||
'min' => 'greaterThanOrEqual',
|
||||
'max' => 'lessThanOrEqual',
|
||||
};
|
||||
|
||||
trigger_error(
|
||||
sprintf(
|
||||
'Calling %s() with a scalar value has been deprecated, ' .
|
||||
'and will be not allows in the next major version. Use %s() instead.',
|
||||
$ruleSpec->name,
|
||||
$name,
|
||||
),
|
||||
E_USER_DEPRECATED
|
||||
);
|
||||
|
||||
return new RuleSpec($name, $ruleSpec->arguments);
|
||||
}
|
||||
}
|
54
library/Transformers/DeprecatedType.php
Normal file
54
library/Transformers/DeprecatedType.php
Normal file
|
@ -0,0 +1,54 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Respect\Validation\Transformers;
|
||||
|
||||
use function current;
|
||||
use function sprintf;
|
||||
use function trigger_error;
|
||||
|
||||
use const E_USER_DEPRECATED;
|
||||
|
||||
final class DeprecatedType implements Transformer
|
||||
{
|
||||
private const AVAILABLE_TYPES = [
|
||||
'array' => 'arrayType',
|
||||
'bool' => 'boolType',
|
||||
'boolean' => 'boolType',
|
||||
'callable' => 'callableType',
|
||||
'double' => 'floatType',
|
||||
'float' => 'floatType',
|
||||
'int' => 'intType',
|
||||
'integer' => 'intType',
|
||||
'null' => 'nullType',
|
||||
'object' => 'objectType',
|
||||
'resource' => 'resourceType',
|
||||
'string' => 'stringType',
|
||||
];
|
||||
|
||||
public function transform(RuleSpec $ruleSpec): RuleSpec
|
||||
{
|
||||
if ($ruleSpec->name !== 'type') {
|
||||
return $ruleSpec;
|
||||
}
|
||||
|
||||
$type = current($ruleSpec->arguments);
|
||||
$name = self::AVAILABLE_TYPES[$type] ?? $type;
|
||||
|
||||
trigger_error(
|
||||
sprintf(
|
||||
'The type() rule is deprecated and will be removed in the next major version. Use %s() instead.',
|
||||
$name
|
||||
),
|
||||
E_USER_DEPRECATED
|
||||
);
|
||||
|
||||
return new RuleSpec($name, []);
|
||||
}
|
||||
}
|
20
library/Transformers/RuleSpec.php
Normal file
20
library/Transformers/RuleSpec.php
Normal file
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Respect\Validation\Transformers;
|
||||
|
||||
final class RuleSpec
|
||||
{
|
||||
/** @param array<mixed> $arguments */
|
||||
public function __construct(
|
||||
public readonly string $name,
|
||||
public readonly array $arguments = []
|
||||
) {
|
||||
}
|
||||
}
|
15
library/Transformers/Transformer.php
Normal file
15
library/Transformers/Transformer.php
Normal file
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Respect\Validation\Transformers;
|
||||
|
||||
interface Transformer
|
||||
{
|
||||
public function transform(RuleSpec $ruleSpec): RuleSpec;
|
||||
}
|
48
tests/integration/rules/deprecated/attribute.phpt
Normal file
48
tests/integration/rules/deprecated/attribute.phpt
Normal file
|
@ -0,0 +1,48 @@
|
|||
--FILE--
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
$object = new stdClass();
|
||||
$object->foo = true;
|
||||
$object->bar = 42;
|
||||
|
||||
exceptionMessage(static fn() => v::attribute('baz')->assert($object));
|
||||
exceptionMessage(static fn() => v::not(v::attribute('foo'))->assert($object));
|
||||
exceptionMessage(static fn() => v::attribute('foo', v::falseVal())->assert($object));
|
||||
exceptionMessage(static fn() => v::not(v::attribute('foo', v::trueVal()))->assert($object));
|
||||
exceptionMessage(static fn() => v::attribute('foo', v::falseVal(), true)->assert($object));
|
||||
exceptionMessage(static fn() => v::not(v::attribute('foo', v::trueVal(), true))->assert($object));
|
||||
exceptionMessage(static fn() => v::attribute('foo', v::falseVal(), false)->assert($object));
|
||||
exceptionMessage(static fn() => v::not(v::attribute('foo', v::trueVal(), false))->assert($object));
|
||||
// phpcs:disable Generic.Files.LineLength.TooLong
|
||||
?>
|
||||
--EXPECTF--
|
||||
|
||||
Deprecated: The attribute() rule has been deprecated and will be removed in the next major version. Use propertyExists() instead. %s
|
||||
baz must be present
|
||||
|
||||
Deprecated: The attribute() rule has been deprecated and will be removed in the next major version. Use propertyExists() instead. %s
|
||||
foo must not be present
|
||||
|
||||
Deprecated: The attribute() rule has been deprecated and will be removed in the next major version. Use property() instead. %s
|
||||
foo must evaluate to `false`
|
||||
|
||||
Deprecated: The attribute() rule has been deprecated and will be removed in the next major version. Use property() instead. %s
|
||||
foo must not evaluate to `true`
|
||||
|
||||
Deprecated: The attribute() rule has been deprecated and will be removed in the next major version. Use property() without it. %s
|
||||
foo must evaluate to `false`
|
||||
|
||||
Deprecated: The attribute() rule has been deprecated and will be removed in the next major version. Use property() without it. %s
|
||||
foo must not evaluate to `true`
|
||||
|
||||
Deprecated: The attribute() rule has been deprecated and will be removed in the next major version. Use propertyOptional() instead. %s
|
||||
foo must evaluate to `false`
|
||||
|
||||
Deprecated: The attribute() rule has been deprecated and will be removed in the next major version. Use propertyOptional() instead. %s
|
||||
foo must not evaluate to `true`
|
38
tests/integration/rules/deprecated/key.phpt
Normal file
38
tests/integration/rules/deprecated/key.phpt
Normal file
|
@ -0,0 +1,38 @@
|
|||
--FILE--
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
$array = ['foo' => true, 'bar' => 42];
|
||||
|
||||
exceptionMessage(static fn() => v::key('baz')->assert($array));
|
||||
exceptionMessage(static fn() => v::not(v::key('foo'))->assert($array));
|
||||
exceptionMessage(static fn() => v::key('foo', v::falseVal(), true)->assert($array));
|
||||
exceptionMessage(static fn() => v::not(v::key('foo', v::trueVal(), true))->assert($array));
|
||||
exceptionMessage(static fn() => v::key('foo', v::falseVal(), false)->assert($array));
|
||||
exceptionMessage(static fn() => v::not(v::key('foo', v::trueVal(), false))->assert($array));
|
||||
// phpcs:disable Generic.Files.LineLength.TooLong
|
||||
?>
|
||||
--EXPECTF--
|
||||
|
||||
Deprecated: Calling key() without a second parameter has been deprecated, and will be not be allowed in the next major version. Use keyExists() instead. %s
|
||||
baz must be present
|
||||
|
||||
Deprecated: Calling key() without a second parameter has been deprecated, and will be not be allowed in the next major version. Use keyExists() instead. %s
|
||||
foo must not be present
|
||||
|
||||
Deprecated: Calling key() with a third parameter has been deprecated, and will be not be allowed in the next major version. Use key() without it the third parameter. %s
|
||||
foo must evaluate to `false`
|
||||
|
||||
Deprecated: Calling key() with a third parameter has been deprecated, and will be not be allowed in the next major version. Use key() without it the third parameter. %s
|
||||
foo must not evaluate to `true`
|
||||
|
||||
Deprecated: Calling key() with a third parameter has been deprecated, and will be not be allowed in the next major version. Use keyOptional() instead. %s
|
||||
foo must evaluate to `false`
|
||||
|
||||
Deprecated: Calling key() with a third parameter has been deprecated, and will be not be allowed in the next major version. Use keyOptional() instead. %s
|
||||
foo must not evaluate to `true`
|
50
tests/integration/rules/deprecated/keyNested.phpt
Normal file
50
tests/integration/rules/deprecated/keyNested.phpt
Normal file
|
@ -0,0 +1,50 @@
|
|||
--FILE--
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
$input = [
|
||||
'foo' => (object) [
|
||||
'bar' => 123,
|
||||
],
|
||||
];
|
||||
|
||||
exceptionMessage(static fn() => v::keyNested('foo.bar.baz')->assert(['foo.bar.baz' => false]));
|
||||
exceptionMessage(static fn() => v::keyNested('foo.bar')->assert($input));
|
||||
exceptionMessage(static fn() => v::keyNested('foo.bar')->assert(new ArrayObject($input)));
|
||||
exceptionMessage(static fn() => v::keyNested('foo.bar', v::negative())->assert($input));
|
||||
exceptionMessage(static fn() => v::keyNested('foo.bar')->assert($input));
|
||||
exceptionMessage(static fn() => v::keyNested('foo.bar', v::stringType())->assert($input));
|
||||
exceptionMessage(static fn() => v::keyNested('foo.bar.baz', v::notEmpty(), false)->assert($input));
|
||||
exceptionMessage(static fn() => v::keyNested('foo.bar', v::floatType(), false)->assert($input));
|
||||
// phpcs:disable Generic.Files.LineLength.TooLong
|
||||
?>
|
||||
--EXPECTF--
|
||||
|
||||
Deprecated: The keyNested() rule is deprecated and will be removed in the next major version. Use nested key() or property() instead. in %s
|
||||
foo must be present after asserting that `["foo.bar.baz": false]` must be an array value
|
||||
|
||||
Deprecated: The keyNested() rule is deprecated and will be removed in the next major version. Use nested key() or property() instead. in %s
|
||||
No exception was thrown
|
||||
|
||||
Deprecated: The keyNested() rule is deprecated and will be removed in the next major version. Use nested key() or property() instead. in %s
|
||||
No exception was thrown
|
||||
|
||||
Deprecated: The keyNested() rule is deprecated and will be removed in the next major version. Use nested key() or property() instead. in %s
|
||||
bar must be negative
|
||||
|
||||
Deprecated: The keyNested() rule is deprecated and will be removed in the next major version. Use nested key() or property() instead. in %s
|
||||
No exception was thrown
|
||||
|
||||
Deprecated: The keyNested() rule is deprecated and will be removed in the next major version. Use nested key() or property() instead. in %s
|
||||
bar must be of type string
|
||||
|
||||
Deprecated: The keyNested() rule is deprecated and will be removed in the next major version. Use nested key() or property() instead. in %s
|
||||
No exception was thrown
|
||||
|
||||
Deprecated: The keyNested() rule is deprecated and will be removed in the next major version. Use nested key() or property() instead. in %s
|
||||
bar must be of type float
|
52
tests/integration/rules/deprecated/keyValue.phpt
Normal file
52
tests/integration/rules/deprecated/keyValue.phpt
Normal file
|
@ -0,0 +1,52 @@
|
|||
--FILE--
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
exceptionMessage(static fn() => v::keyValue('foo', 'equals', 'bar')->check(['bar' => 42]));
|
||||
exceptionMessage(static fn() => v::keyValue('foo', 'equals', 'bar')->check(['foo' => 42]));
|
||||
exceptionMessage(static fn() => v::keyValue('foo', 'json', 'bar')->check(['foo' => 42, 'bar' => 43]));
|
||||
exceptionMessage(static fn() => v::keyValue('foo', 'equals', 'bar')->check(['foo' => 1, 'bar' => 2]));
|
||||
exceptionMessage(static fn() => v::not(v::keyValue('foo', 'equals', 'bar'))->check(['foo' => 1, 'bar' => 1]));
|
||||
exceptionFullMessage(static fn() => v::keyValue('foo', 'equals', 'bar')->assert(['bar' => 42]));
|
||||
exceptionFullMessage(static fn() => v::keyValue('foo', 'equals', 'bar')->assert(['foo' => 42]));
|
||||
exceptionFullMessage(static fn() => v::keyValue('foo', 'json', 'bar')->assert(['foo' => 42, 'bar' => 43]));
|
||||
exceptionFullMessage(static fn() => v::keyValue('foo', 'equals', 'bar')->assert(['foo' => 1, 'bar' => 2]));
|
||||
exceptionFullMessage(static fn() => v::not(v::keyValue('foo', 'equals', 'bar'))->assert(['foo' => 1, 'bar' => 1]));
|
||||
// phpcs:disable Generic.Files.LineLength.TooLong
|
||||
?>
|
||||
--EXPECTF--
|
||||
|
||||
Deprecated: The keyValue() rule has been deprecated and will be removed in the next major version. Use nested lazy() instead. in %s
|
||||
foo must be present
|
||||
|
||||
Deprecated: The keyValue() rule has been deprecated and will be removed in the next major version. Use nested lazy() instead. in %s
|
||||
bar must be present
|
||||
|
||||
Deprecated: The keyValue() rule has been deprecated and will be removed in the next major version. Use nested lazy() instead. in %s
|
||||
bar must be valid to validate foo
|
||||
|
||||
Deprecated: The keyValue() rule has been deprecated and will be removed in the next major version. Use nested lazy() instead. in %s
|
||||
foo must equal 2
|
||||
|
||||
Deprecated: The keyValue() rule has been deprecated and will be removed in the next major version. Use nested lazy() instead. in %s
|
||||
foo must not equal 1
|
||||
|
||||
Deprecated: The keyValue() rule has been deprecated and will be removed in the next major version. Use nested lazy() instead. in %s
|
||||
- foo must be present
|
||||
|
||||
Deprecated: The keyValue() rule has been deprecated and will be removed in the next major version. Use nested lazy() instead. in %s
|
||||
- bar must be present
|
||||
|
||||
Deprecated: The keyValue() rule has been deprecated and will be removed in the next major version. Use nested lazy() instead. in %s
|
||||
- bar must be valid to validate foo
|
||||
|
||||
Deprecated: The keyValue() rule has been deprecated and will be removed in the next major version. Use nested lazy() instead. in %s
|
||||
- foo must equal 2
|
||||
|
||||
Deprecated: The keyValue() rule has been deprecated and will be removed in the next major version. Use nested lazy() instead. in %s
|
||||
- foo must not equal 1
|
28
tests/integration/rules/deprecated/max.phpt
Normal file
28
tests/integration/rules/deprecated/max.phpt
Normal file
|
@ -0,0 +1,28 @@
|
|||
--FILE--
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
exceptionMessage(static fn() => v::max(10)->check(11));
|
||||
exceptionMessage(static fn() => v::not(v::max(10))->check(5));
|
||||
exceptionFullMessage(static fn() => v::max('today')->assert('tomorrow'));
|
||||
exceptionFullMessage(static fn() => v::not(v::max('b'))->assert('a'));
|
||||
// phpcs:disable Generic.Files.LineLength.TooLong
|
||||
?>
|
||||
--EXPECTF--
|
||||
|
||||
Deprecated: Calling max() with a scalar value has been deprecated, and will be not allows in the next major version. Use lessThanOrEqual() instead. in %s
|
||||
11 must be less than or equal to 10
|
||||
|
||||
Deprecated: Calling max() with a scalar value has been deprecated, and will be not allows in the next major version. Use lessThanOrEqual() instead. in %s
|
||||
5 must not be less than or equal to 10
|
||||
|
||||
Deprecated: Calling max() with a scalar value has been deprecated, and will be not allows in the next major version. Use lessThanOrEqual() instead. in %s
|
||||
- "tomorrow" must be less than or equal to "today"
|
||||
|
||||
Deprecated: Calling max() with a scalar value has been deprecated, and will be not allows in the next major version. Use lessThanOrEqual() instead. in %s
|
||||
- "a" must not be less than or equal to "b"
|
28
tests/integration/rules/deprecated/min.phpt
Normal file
28
tests/integration/rules/deprecated/min.phpt
Normal file
|
@ -0,0 +1,28 @@
|
|||
--FILE--
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
exceptionMessage(static fn() => v::min(INF)->check(10));
|
||||
exceptionMessage(static fn() => v::not(v::min(5))->check(INF));
|
||||
exceptionFullMessage(static fn() => v::min('today')->assert('yesterday'));
|
||||
exceptionFullMessage(static fn() => v::not(v::min('a'))->assert('z'));
|
||||
// phpcs:disable Generic.Files.LineLength.TooLong
|
||||
?>
|
||||
--EXPECTF--
|
||||
|
||||
Deprecated: Calling min() with a scalar value has been deprecated, and will be not allows in the next major version. Use greaterThanOrEqual() instead. in %s
|
||||
10 must be greater than or equal to `INF`
|
||||
|
||||
Deprecated: Calling min() with a scalar value has been deprecated, and will be not allows in the next major version. Use greaterThanOrEqual() instead. in %s
|
||||
`INF` must not be greater than or equal to 5
|
||||
|
||||
Deprecated: Calling min() with a scalar value has been deprecated, and will be not allows in the next major version. Use greaterThanOrEqual() instead. in %s
|
||||
- "yesterday" must be greater than or equal to "today"
|
||||
|
||||
Deprecated: Calling min() with a scalar value has been deprecated, and will be not allows in the next major version. Use greaterThanOrEqual() instead. in %s
|
||||
- "z" must not be greater than or equal to "a"
|
60
tests/integration/rules/deprecated/type.phpt
Normal file
60
tests/integration/rules/deprecated/type.phpt
Normal file
|
@ -0,0 +1,60 @@
|
|||
--FILE--
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
exceptionMessage(static fn() => v::type('array')->assert(1));
|
||||
exceptionMessage(static fn() => v::type('bool')->assert(1));
|
||||
exceptionMessage(static fn() => v::type('boolean')->assert(1));
|
||||
exceptionMessage(static fn() => v::type('callable')->assert(1));
|
||||
exceptionMessage(static fn() => v::type('double')->assert(1));
|
||||
exceptionMessage(static fn() => v::type('float')->assert(1));
|
||||
exceptionMessage(static fn() => v::type('int')->assert('1'));
|
||||
exceptionMessage(static fn() => v::type('integer')->assert('1'));
|
||||
exceptionMessage(static fn() => v::type('null')->assert(1));
|
||||
exceptionMessage(static fn() => v::type('object')->assert(1));
|
||||
exceptionMessage(static fn() => v::type('resource')->assert(1));
|
||||
exceptionMessage(static fn() => v::type('string')->assert(1));
|
||||
// phpcs:disable Generic.Files.LineLength.TooLong
|
||||
?>
|
||||
--EXPECTF--
|
||||
|
||||
Deprecated: The type() rule is deprecated and will be removed in the next major version. Use arrayType() instead. in %s
|
||||
1 must be of type array
|
||||
|
||||
Deprecated: The type() rule is deprecated and will be removed in the next major version. Use boolType() instead. in %s
|
||||
1 must be of type boolean
|
||||
|
||||
Deprecated: The type() rule is deprecated and will be removed in the next major version. Use boolType() instead. in %s
|
||||
1 must be of type boolean
|
||||
|
||||
Deprecated: The type() rule is deprecated and will be removed in the next major version. Use callableType() instead. in %s
|
||||
1 must be callable
|
||||
|
||||
Deprecated: The type() rule is deprecated and will be removed in the next major version. Use floatType() instead. in %s
|
||||
1 must be of type float
|
||||
|
||||
Deprecated: The type() rule is deprecated and will be removed in the next major version. Use floatType() instead. in %s
|
||||
1 must be of type float
|
||||
|
||||
Deprecated: The type() rule is deprecated and will be removed in the next major version. Use intType() instead. in %s
|
||||
"1" must be of type integer
|
||||
|
||||
Deprecated: The type() rule is deprecated and will be removed in the next major version. Use intType() instead. in %s
|
||||
"1" must be of type integer
|
||||
|
||||
Deprecated: The type() rule is deprecated and will be removed in the next major version. Use nullType() instead. in %s
|
||||
1 must be null
|
||||
|
||||
Deprecated: The type() rule is deprecated and will be removed in the next major version. Use objectType() instead. in %s
|
||||
1 must be of type object
|
||||
|
||||
Deprecated: The type() rule is deprecated and will be removed in the next major version. Use resourceType() instead. in %s
|
||||
1 must be a resource
|
||||
|
||||
Deprecated: The type() rule is deprecated and will be removed in the next major version. Use stringType() instead. in %s
|
||||
1 must be of type string
|
|
@ -1,19 +0,0 @@
|
|||
--FILE--
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
exceptionMessage(static fn() => v::type('integer')->check('42'));
|
||||
exceptionMessage(static fn() => v::not(v::type('string'))->check('foo'));
|
||||
exceptionFullMessage(static fn() => v::type('double')->assert(20));
|
||||
exceptionFullMessage(static fn() => v::not(v::type('bool'))->assert(true));
|
||||
?>
|
||||
--EXPECT--
|
||||
"42" must be "integer"
|
||||
"foo" must not be "string"
|
||||
- 20 must be "double"
|
||||
- `true` must not be "bool"
|
|
@ -1,65 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\Group;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use Respect\Validation\Exceptions\ComponentException;
|
||||
use Respect\Validation\Test\RuleTestCase;
|
||||
use stdClass;
|
||||
|
||||
use function tmpfile;
|
||||
|
||||
#[Group(' rule')]
|
||||
#[CoversClass(Type::class)]
|
||||
final class TypeTest extends RuleTestCase
|
||||
{
|
||||
#[Test]
|
||||
public function shouldThrowExceptionWhenTypeIsNotValid(): void
|
||||
{
|
||||
$this->expectException(ComponentException::class);
|
||||
$this->expectExceptionMessageMatches('/"whatever" is not a valid type \(Available: .+\)/');
|
||||
|
||||
new Type('whatever');
|
||||
}
|
||||
|
||||
/** @return iterable<array{Type, mixed}> */
|
||||
public static function providerForValidInput(): iterable
|
||||
{
|
||||
return [
|
||||
[new Type('array'), []],
|
||||
[new Type('bool'), true],
|
||||
[new Type('boolean'), false],
|
||||
[
|
||||
new Type('callable'),
|
||||
static function (): void {
|
||||
},
|
||||
],
|
||||
[new Type('double'), 0.8],
|
||||
[new Type('float'), 1.0],
|
||||
[new Type('int'), 42],
|
||||
[new Type('integer'), 13],
|
||||
[new Type('null'), null],
|
||||
[new Type('object'), new stdClass()],
|
||||
[new Type('resource'), tmpfile()],
|
||||
[new Type('string'), 'Something'],
|
||||
];
|
||||
}
|
||||
|
||||
/** @return iterable<array{Type, mixed}> */
|
||||
public static function providerForInvalidInput(): iterable
|
||||
{
|
||||
return [
|
||||
[new Type('int'), '1'],
|
||||
[new Type('bool'), '1'],
|
||||
];
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue