mirror of
https://github.com/Respect/Validation.git
synced 2024-05-18 14:16:41 +02:00
Drop PHP 7.4 support
This commit is contained in:
parent
ab3732f91f
commit
8cafa3f298
5
.github/workflows/continuous-integration.yml
vendored
5
.github/workflows/continuous-integration.yml
vendored
|
@ -19,7 +19,6 @@ jobs:
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
php-version:
|
php-version:
|
||||||
- "7.4"
|
|
||||||
- "8.0"
|
- "8.0"
|
||||||
- "8.1"
|
- "8.1"
|
||||||
- "8.2"
|
- "8.2"
|
||||||
|
@ -63,7 +62,7 @@ jobs:
|
||||||
- name: Install PHP
|
- name: Install PHP
|
||||||
uses: shivammathur/setup-php@v2
|
uses: shivammathur/setup-php@v2
|
||||||
with:
|
with:
|
||||||
php-version: 8.1
|
php-version: 8.0
|
||||||
coverage: pcov
|
coverage: pcov
|
||||||
extensions: uopz
|
extensions: uopz
|
||||||
tools: pecl
|
tools: pecl
|
||||||
|
@ -97,7 +96,7 @@ jobs:
|
||||||
- name: Install PHP
|
- name: Install PHP
|
||||||
uses: shivammathur/setup-php@v2
|
uses: shivammathur/setup-php@v2
|
||||||
with:
|
with:
|
||||||
php-version: 8.1
|
php-version: 8.0
|
||||||
coverage: none
|
coverage: none
|
||||||
|
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
|
|
12
CHANGELOG.md
12
CHANGELOG.md
|
@ -1,5 +1,17 @@
|
||||||
# Changes in Respect\Validation 2.x
|
# Changes in Respect\Validation 2.x
|
||||||
|
|
||||||
|
## 2.3
|
||||||
|
|
||||||
|
Versioning Changes:
|
||||||
|
|
||||||
|
- Dropped support for PHP 7.4.
|
||||||
|
- Updated dev dependencies
|
||||||
|
|
||||||
|
Deprecations:
|
||||||
|
|
||||||
|
- Symfony façade validators are no longer supported and were
|
||||||
|
removed.
|
||||||
|
|
||||||
## 2.2.4
|
## 2.2.4
|
||||||
|
|
||||||
Meta:
|
Meta:
|
||||||
|
|
|
@ -18,13 +18,13 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
"php": "^7.4 || ^8.0 || ^8.1 || ^8.2",
|
"php": "^8.0 || ^8.1 || ^8.2",
|
||||||
"respect/stringifier": "^0.2.0",
|
"respect/stringifier": "^0.2.0",
|
||||||
"symfony/polyfill-mbstring": "^1.2"
|
"symfony/polyfill-mbstring": "^1.2"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"egulias/email-validator": "^3.0",
|
"egulias/email-validator": "^3.0",
|
||||||
"malukenho/docheader": "^0.1",
|
"malukenho/docheader": "^1.0",
|
||||||
"mikey179/vfsstream": "^1.6",
|
"mikey179/vfsstream": "^1.6",
|
||||||
"phpstan/phpstan": "^1.9",
|
"phpstan/phpstan": "^1.9",
|
||||||
"phpstan/phpstan-deprecation-rules": "^1.1",
|
"phpstan/phpstan-deprecation-rules": "^1.1",
|
||||||
|
@ -32,8 +32,7 @@
|
||||||
"phpunit/phpunit": "^9.6",
|
"phpunit/phpunit": "^9.6",
|
||||||
"psr/http-message": "^1.0",
|
"psr/http-message": "^1.0",
|
||||||
"respect/coding-standard": "^3.0",
|
"respect/coding-standard": "^3.0",
|
||||||
"squizlabs/php_codesniffer": "^3.7",
|
"squizlabs/php_codesniffer": "^3.7"
|
||||||
"symfony/validator": "^3.0||^4.0||^5.0"
|
|
||||||
},
|
},
|
||||||
"suggest": {
|
"suggest": {
|
||||||
"ext-bcmath": "Arbitrary Precision Mathematics",
|
"ext-bcmath": "Arbitrary Precision Mathematics",
|
||||||
|
|
|
@ -237,14 +237,6 @@ is the same as the input. You can customize a validator name using:
|
||||||
v::dateTime('Y-m-d')->between('1980-02-02', 'now')->setName('Member Since');
|
v::dateTime('Y-m-d')->between('1980-02-02', 'now')->setName('Member Since');
|
||||||
```
|
```
|
||||||
|
|
||||||
## Symfony validators
|
|
||||||
|
|
||||||
It is also possible to reuse validators from Symfony if they are installed:
|
|
||||||
|
|
||||||
```php
|
|
||||||
$timeValidator = v::sf('Time')->assert('22:00:01');
|
|
||||||
```
|
|
||||||
|
|
||||||
## Validation methods
|
## Validation methods
|
||||||
|
|
||||||
We've seen `validate()` that returns true or false and `assert()` that throws a complete
|
We've seen `validate()` that returns true or false and `assert()` that throws a complete
|
||||||
|
|
|
@ -1,27 +0,0 @@
|
||||||
# Sf
|
|
||||||
|
|
||||||
- `Sf(Constraint $constraint)`
|
|
||||||
- `Sf(Constraint $constraint, ValidatorInterface $validator)`
|
|
||||||
|
|
||||||
Validate the input with a Symfony Validator (>=4.0 or >=3.0) Constraint.
|
|
||||||
|
|
||||||
```php
|
|
||||||
use Symfony\Component\Validator\Constraint\Iban;
|
|
||||||
|
|
||||||
v::sf(new Iban())->validate('NL39 RABO 0300 0652 64'); // true
|
|
||||||
```
|
|
||||||
|
|
||||||
This rule will keep all the messages returned from Symfony.
|
|
||||||
|
|
||||||
## Categorization
|
|
||||||
|
|
||||||
- Integrations
|
|
||||||
|
|
||||||
## Changelog
|
|
||||||
|
|
||||||
Version | Description
|
|
||||||
--------|-------------
|
|
||||||
2.0.0 | Do not create constraints anymore
|
|
||||||
2.0.0 | Upgraded support to version >=4.0 or >=3.0 of Symfony Validator
|
|
||||||
0.3.9 | Created
|
|
||||||
|
|
|
@ -11,8 +11,6 @@ namespace Respect\Validation;
|
||||||
|
|
||||||
use finfo;
|
use finfo;
|
||||||
use Respect\Validation\Rules\Key;
|
use Respect\Validation\Rules\Key;
|
||||||
use Symfony\Component\Validator\Constraint;
|
|
||||||
use Symfony\Component\Validator\Validator\ValidatorInterface as SymfonyValidator;
|
|
||||||
|
|
||||||
interface ChainedValidator extends Validatable
|
interface ChainedValidator extends Validatable
|
||||||
{
|
{
|
||||||
|
@ -316,8 +314,6 @@ interface ChainedValidator extends Validatable
|
||||||
|
|
||||||
public function scalarVal(): ChainedValidator;
|
public function scalarVal(): ChainedValidator;
|
||||||
|
|
||||||
public function sf(Constraint $constraint, ?SymfonyValidator $validator = null): ChainedValidator;
|
|
||||||
|
|
||||||
public function size(?string $minSize = null, ?string $maxSize = null): ChainedValidator;
|
public function size(?string $minSize = null, ?string $maxSize = null): ChainedValidator;
|
||||||
|
|
||||||
public function slug(): ChainedValidator;
|
public function slug(): ChainedValidator;
|
||||||
|
|
|
@ -1,29 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
||||||
* SPDX-License-Identifier: MIT
|
|
||||||
*/
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Respect\Validation\Exceptions;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
||||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
|
||||||
*/
|
|
||||||
final class SfException extends ValidationException
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* {@inheritDoc}
|
|
||||||
*/
|
|
||||||
protected $defaultTemplates = [
|
|
||||||
self::MODE_DEFAULT => [
|
|
||||||
self::STANDARD => '{{name}} must be valid for {{constraint}}',
|
|
||||||
],
|
|
||||||
self::MODE_NEGATIVE => [
|
|
||||||
self::STANDARD => '{{name}} must not be valid for {{constraint}}',
|
|
||||||
],
|
|
||||||
];
|
|
||||||
}
|
|
|
@ -1,96 +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\SfException;
|
|
||||||
use Respect\Validation\Exceptions\ValidationException;
|
|
||||||
use Symfony\Component\Validator\Constraint;
|
|
||||||
use Symfony\Component\Validator\ConstraintViolationList;
|
|
||||||
use Symfony\Component\Validator\Validation;
|
|
||||||
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
|
||||||
|
|
||||||
use function trim;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Validate the input with a Symfony Validator (>=4.0 or >=3.0) Constraint.
|
|
||||||
*
|
|
||||||
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
||||||
* @author Augusto Pascutti <augusto@phpsp.org.br>
|
|
||||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
|
||||||
* @author Hugo Hamon <hugo.hamon@sensiolabs.com>
|
|
||||||
*/
|
|
||||||
final class Sf extends AbstractRule
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* @var Constraint
|
|
||||||
*/
|
|
||||||
private $constraint;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var ValidatorInterface
|
|
||||||
*/
|
|
||||||
private $validator;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initializes the rule with the Constraint and the Validator.
|
|
||||||
*
|
|
||||||
* In the the Validator is not defined, tries to create one.
|
|
||||||
*/
|
|
||||||
public function __construct(Constraint $constraint, ?ValidatorInterface $validator = null)
|
|
||||||
{
|
|
||||||
$this->constraint = $constraint;
|
|
||||||
$this->validator = $validator ?: Validation::createValidator();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritDoc}
|
|
||||||
*/
|
|
||||||
public function assert($input): void
|
|
||||||
{
|
|
||||||
/** @var ConstraintViolationList $violations */
|
|
||||||
$violations = $this->validator->validate($input, $this->constraint);
|
|
||||||
if ($violations->count() === 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($violations->count() === 1) {
|
|
||||||
throw $this->reportError($input, ['violations' => $violations[0]->getMessage()]);
|
|
||||||
}
|
|
||||||
|
|
||||||
throw $this->reportError($input, ['violations' => trim($violations->__toString())]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritDoc}
|
|
||||||
*/
|
|
||||||
public function reportError($input, array $extraParams = []): ValidationException
|
|
||||||
{
|
|
||||||
$exception = parent::reportError($input, $extraParams);
|
|
||||||
if (isset($extraParams['violations'])) {
|
|
||||||
$exception->updateTemplate($extraParams['violations']);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $exception;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritDoc}
|
|
||||||
*/
|
|
||||||
public function validate($input): bool
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
$this->assert($input);
|
|
||||||
} catch (SfException $exception) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -11,8 +11,6 @@ namespace Respect\Validation;
|
||||||
|
|
||||||
use finfo;
|
use finfo;
|
||||||
use Respect\Validation\Rules\Key;
|
use Respect\Validation\Rules\Key;
|
||||||
use Symfony\Component\Validator\Constraint;
|
|
||||||
use Symfony\Component\Validator\Validator\ValidatorInterface as SymfonyValidator;
|
|
||||||
|
|
||||||
interface StaticValidator
|
interface StaticValidator
|
||||||
{
|
{
|
||||||
|
@ -316,8 +314,6 @@ interface StaticValidator
|
||||||
|
|
||||||
public static function scalarVal(): ChainedValidator;
|
public static function scalarVal(): ChainedValidator;
|
||||||
|
|
||||||
public static function sf(Constraint $constraint, ?SymfonyValidator $validator = null): ChainedValidator;
|
|
||||||
|
|
||||||
public static function size(?string $minSize = null, ?string $maxSize = null): ChainedValidator;
|
public static function size(?string $minSize = null, ?string $maxSize = null): ChainedValidator;
|
||||||
|
|
||||||
public static function slug(): ChainedValidator;
|
public static function slug(): ChainedValidator;
|
||||||
|
|
|
@ -1,60 +0,0 @@
|
||||||
--CREDITS--
|
|
||||||
Henrique Moody <henriquemoody@gmail.com>
|
|
||||||
--FILE--
|
|
||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
require 'vendor/autoload.php';
|
|
||||||
|
|
||||||
use Respect\Validation\Exceptions\NestedValidationException;
|
|
||||||
use Respect\Validation\Exceptions\SfException;
|
|
||||||
use Respect\Validation\Validator as v;
|
|
||||||
use Symfony\Component\Validator\Constraints\Collection;
|
|
||||||
use Symfony\Component\Validator\Constraints\Email;
|
|
||||||
use Symfony\Component\Validator\Constraints\IsNull;
|
|
||||||
|
|
||||||
try {
|
|
||||||
v::sf(new IsNull())->check('something');
|
|
||||||
} catch (SfException $exception) {
|
|
||||||
echo $exception->getMessage() . PHP_EOL;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
v::not(v::sf(new IsNull()))->check(null);
|
|
||||||
} catch (SfException $exception) {
|
|
||||||
echo $exception->getMessage() . PHP_EOL;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
v::sf(new Email())->assert('not-null');
|
|
||||||
} catch (NestedValidationException $exception) {
|
|
||||||
echo $exception->getFullMessage() . PHP_EOL;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
v::not(v::sf(new Email()))->assert('example@example.com');
|
|
||||||
} catch (NestedValidationException $exception) {
|
|
||||||
echo $exception->getFullMessage() . PHP_EOL;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
v::sf(
|
|
||||||
new Collection([
|
|
||||||
'first' => new IsNull(),
|
|
||||||
'second' => new Email(),
|
|
||||||
])
|
|
||||||
)->check(['second' => 'not-email']);
|
|
||||||
} catch (SfException $exception) {
|
|
||||||
echo $exception->getMessage();
|
|
||||||
}
|
|
||||||
?>
|
|
||||||
--EXPECTF--
|
|
||||||
This value should be null.
|
|
||||||
`NULL` must not be valid for `[object] (Symfony\Component\Validator\Constraints\IsNull: { %s })`
|
|
||||||
- This value is not a valid email address.
|
|
||||||
- "example@example.com" must not be valid for `[object] (Symfony\Component\Validator\Constraints\Email: { %s })`
|
|
||||||
Array[first]:
|
|
||||||
This field is missing. (code %s)
|
|
||||||
Array[second]:
|
|
||||||
This value is not a valid email address. (code %s)
|
|
|
@ -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\Test\TestCase;
|
|
||||||
use stdClass;
|
|
||||||
use Symfony\Component\Validator\Constraints\IsFalse;
|
|
||||||
use Symfony\Component\Validator\Constraints\IsNull;
|
|
||||||
use Symfony\Component\Validator\Validation;
|
|
||||||
use Symfony\Component\Validator\Validator\TraceableValidator;
|
|
||||||
|
|
||||||
use function class_exists;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @group rule
|
|
||||||
*
|
|
||||||
* @covers \Respect\Validation\Rules\Sf
|
|
||||||
*
|
|
||||||
* @author Augusto Pascutti <augusto@phpsp.org.br>
|
|
||||||
* @author Gabriel Caruso <carusogabriel34@gmail.com>
|
|
||||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
|
||||||
*/
|
|
||||||
final class SfTest extends TestCase
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* @test
|
|
||||||
*/
|
|
||||||
public function itShouldValidateWithDefinedConstraintAndValidator(): void
|
|
||||||
{
|
|
||||||
$sut = new Sf(new IsNull());
|
|
||||||
|
|
||||||
self::assertTrue($sut->validate(null));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @test
|
|
||||||
*/
|
|
||||||
public function itShouldInvalidateWithDefinedConstraintAndValidator(): void
|
|
||||||
{
|
|
||||||
$sut = new Sf(new IsFalse());
|
|
||||||
|
|
||||||
self::assertFalse($sut->validate(true));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @test
|
|
||||||
*/
|
|
||||||
public function itShouldUseTheDefinedValidatorToValidate(): void
|
|
||||||
{
|
|
||||||
if (!class_exists(TraceableValidator::class)) {
|
|
||||||
self::markTestSkipped(
|
|
||||||
'The current version of Symfony Validator does not have ' . TraceableValidator::class
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
$input = new stdClass();
|
|
||||||
|
|
||||||
$validator = new TraceableValidator(Validation::createValidator());
|
|
||||||
|
|
||||||
$sut = new Sf(new IsNull(), $validator);
|
|
||||||
$sut->validate($input);
|
|
||||||
|
|
||||||
self::assertSame($input, $validator->getCollectedData()[0]['context']['value']);
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in a new issue