Framework Update

This commit is contained in:
2024-01-31 22:15:08 +08:00
parent b5ff5e8b5f
commit 20678a6a0c
1459 changed files with 25954 additions and 16153 deletions
+9 -13
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -45,6 +41,13 @@ abstract class AbstractAge extends AbstractRule
*/
private $baseDate;
/**
* Should compare the current base date with the given one.
*
* The dates are represented as integers in the format "Ymd".
*/
abstract protected function compare(int $baseDate, int $givenDate): bool;
/**
* Initializes the rule.
*/
@@ -71,13 +74,6 @@ abstract class AbstractAge extends AbstractRule
return $this->isValidWithFormat($this->format, (string) $input);
}
/**
* Should compare the current base date with the given one.
*
* The dates are represented as integers in the format "Ymd".
*/
abstract protected function compare(int $baseDate, int $givenDate): bool;
private function isValidWithoutFormat(string $dateTime): bool
{
$timestamp = strtotime($dateTime);
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -29,6 +25,14 @@ abstract class AbstractComparison extends AbstractRule
*/
private $compareTo;
/**
* Compare both values and return whether the comparison is valid or not.
*
* @param mixed $left
* @param mixed $right
*/
abstract protected function compare($left, $right): bool;
/**
* Initializes the rule by setting the value to be compared to the input.
*
@@ -53,12 +57,4 @@ abstract class AbstractComparison extends AbstractRule
return $this->compare($left, $right);
}
/**
* Compare both values and return whether the comparison is valid or not.
*
* @param mixed $left
* @param mixed $right
*/
abstract protected function compare($left, $right): bool;
}
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -23,7 +19,7 @@ use function array_map;
/**
* Abstract class for rules that are composed by other rules.
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Wojciech Frącz <fraczwojciech@gmail.com>
*/
@@ -60,6 +56,18 @@ abstract class AbstractComposite extends AbstractRule
return parent::setName($name);
}
public function setDefault(string $default, bool $defaultType=false): Validatable
{
$parentDefault = $this->getDefault();
foreach ($this->rules as $rule) {
$ruleDefault = $rule->getDefault();
if ($ruleDefault && $parentDefault !== $ruleDefault) {
continue;
}
$rule->setDefault($default, $defaultType);
}
return parent::setDefault($default, $defaultType);
}
/**
* Append a rule into the stack of rules.
*
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -29,6 +25,8 @@ abstract class AbstractFilterRule extends AbstractRule
*/
private $additionalChars;
abstract protected function validateFilteredInput(string $input): bool;
/**
* Initializes the rule with a list of characters to be ignored by the validation.
*/
@@ -56,8 +54,6 @@ abstract class AbstractFilterRule extends AbstractRule
return $filteredInput === '' || $this->validateFilteredInput($filteredInput);
}
abstract protected function validateFilteredInput(string $input): bool;
private function filter(string $input): string
{
return str_replace(str_split($this->additionalChars), '', $input);
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -20,7 +16,7 @@ use Respect\Validation\Validatable;
use function is_scalar;
/**
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
* @author Emmerson Siqueira <emmersonsiqueira@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Nick Lombard <github@jigsoft.co.za>
+40 -15
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -18,13 +14,21 @@ use Respect\Validation\Factory;
use Respect\Validation\Validatable;
/**
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Nick Lombard <github@jigsoft.co.za>
* @author Vicente Mendoza <vicentemmor@yahoo.com.mx>
*/
abstract class AbstractRule implements Validatable
{
/**
* @var string|null
*/
protected $default;
/**
* @var bool|false
*/
protected $defaultType;
/**
* @var string|null
*/
@@ -35,14 +39,6 @@ abstract class AbstractRule implements Validatable
*/
protected $template;
/**
* @param mixed$input
*/
public function __invoke($input): bool
{
return $this->validate($input);
}
/**
* {@inheritDoc}
*/
@@ -98,4 +94,33 @@ abstract class AbstractRule implements Validatable
return $this;
}
/**
* {@inheritDoc}
*/
public function getDefault(): ?string
{
return $this->default;
}
/**
* {@inheritDoc}
*/
public function getDefaultType(): ?bool
{
return $this->defaultType;
}
public function setDefault(string $default, bool $defaultType=false): Validatable
{
$this->default = $default;
$this->defaultType = $defaultType;
return $this;
}
/**
* @param mixed$input
*/
public function __invoke($input): bool
{
return $this->validate($input);
}
}
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -16,6 +12,8 @@ namespace Respect\Validation\Rules;
use Respect\Validation\Helpers\CanValidateUndefined;
use function in_array;
use function is_scalar;
use function mb_strtoupper;
/**
* Abstract class for searches into arrays.
@@ -26,21 +24,27 @@ abstract class AbstractSearcher extends AbstractRule
{
use CanValidateUndefined;
/**
* @param mixed $input
* @return mixed[]
*/
abstract protected function getDataSource($input = null): array;
/**
* {@inheritDoc}
*/
public function validate($input): bool
{
$dataSource = $this->getDataSource();
$dataSource = $this->getDataSource($input);
if ($this->isUndefined($input) && empty($dataSource)) {
return true;
}
return in_array($input, $dataSource, true);
}
if (!is_scalar($input)) {
return false;
}
/**
* @return mixed[]
*/
abstract protected function getDataSource(): array;
return in_array(mb_strtoupper((string) $input), $dataSource, true);
}
}
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
+3 -7
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -18,7 +14,7 @@ use Respect\Validation\Exceptions\AllOfException;
use function count;
/**
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
class AllOf extends AbstractComposite
+3 -7
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -21,7 +17,7 @@ use function ctype_alnum;
* Alphanumeric is a combination of alphabetic (a-z and A-Z) and numeric (0-9)
* characters.
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Nick Lombard <github@jigsoft.co.za>
*/
+3 -7
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -18,7 +14,7 @@ use function ctype_alpha;
/**
* Validates whether the input contains only alphabetic characters.
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Nick Lombard <github@jigsoft.co.za>
*/
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -16,7 +12,7 @@ namespace Respect\Validation\Rules;
/**
* Validates any input as invalid.
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
* @author William Espindola <oi@williamespindola.com.br>
*/
+3 -7
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -16,7 +12,7 @@ namespace Respect\Validation\Rules;
/**
* Validates any input as valid.
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
* @author William Espindola <oi@williamespindola.com.br>
*/
+3 -7
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -19,7 +15,7 @@ use Respect\Validation\Exceptions\ValidationException;
use function count;
/**
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
final class AnyOf extends AbstractComposite
+3 -7
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -18,7 +14,7 @@ use function is_array;
/**
* Validates whether the type of an input is array.
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
* @author Emmerson Siqueira <emmersonsiqueira@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
* @author João Torquato <joao.otl@gmail.com>
+3 -7
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -23,7 +19,7 @@ use function is_array;
*
* Instance of `ArrayAccess` or `SimpleXMLElement` are also considered as valid.
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
* @author Emmerson Siqueira <emmersonsiqueira@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
+3 -7
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -23,7 +19,7 @@ use function property_exists;
/**
* Validates an object attribute, event private ones.
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
* @author Emmerson Siqueira <emmersonsiqueira@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
+2 -6
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
+2 -6
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
+3 -7
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -19,7 +15,7 @@ use Respect\Validation\Helpers\CanCompareValues;
/**
* Validates whether the input is between two other values.
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
final class Between extends AbstractEnvelope
+2 -6
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
+2 -6
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
+14 -9
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -14,7 +10,10 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
use function ctype_digit;
use function intval;
use function is_scalar;
use function mb_strlen;
use function strval;
/**
* Validates a Dutch citizen service number (BSN).
@@ -32,17 +31,23 @@ final class Bsn extends AbstractRule
*/
public function validate($input): bool
{
if (!is_scalar($input)) {
return false;
}
$input = (string) $input;
if (!ctype_digit($input)) {
return false;
}
if (mb_strlen($input) !== 9) {
if (mb_strlen(strval($input)) !== 9) {
return false;
}
$sum = -1 * $input[8];
$sum = -1 * intval($input[8]);
for ($i = 9; $i > 1; --$i) {
$sum += $i * $input[9 - $i];
$sum += $i * intval($input[9 - $i]);
}
return $sum !== 0 && $sum % 11 === 0;
+3 -7
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -24,7 +20,7 @@ use function set_error_handler;
/**
* Validates the return of a callable for a given input.
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
* @author Emmerson Siqueira <emmersonsiqueira@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
+2 -6
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
+3 -7
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -20,7 +16,7 @@ use function count;
/**
* Validates the input using the return of a given callable.
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
* @author William Espindola <oi@williamespindola.com.br>
*/
+4 -10
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -23,7 +19,7 @@ use function mb_list_encodings;
/**
* Validates if a string is in a specific charset.
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
* @author William Espindola <oi@williamespindola.com.br>
*/
@@ -54,8 +50,6 @@ final class Charset extends AbstractRule
*/
public function validate($input): bool
{
$detectedEncoding = mb_detect_encoding($input, $this->charset, true);
return in_array($detectedEncoding, $this->charset, true);
return in_array(mb_detect_encoding($input, $this->charset, true), $this->charset, true);
}
}
+2 -6
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
+3 -7
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -23,7 +19,7 @@ use function str_split;
/**
* Validates if the input is a Brazilian National Registry of Legal Entities (CNPJ) number.
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Jayson Reis <santosdosreis@gmail.com>
* @author Nick Lombard <github@jigsoft.co.za>
+2 -6
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
+5 -11
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -16,14 +12,13 @@ namespace Respect\Validation\Rules;
use function in_array;
use function is_array;
use function is_scalar;
use function mb_detect_encoding;
use function mb_stripos;
use function mb_strpos;
/**
* Validates if the input contains some value.
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Marcelo Araujo <msaraujo@php.net>
* @author William Espindola <oi@williamespindola.com.br>
@@ -74,11 +69,10 @@ final class Contains extends AbstractRule
return false;
}
$encoding = (string) mb_detect_encoding($haystack);
if ($this->identical) {
return mb_strpos($haystack, $needle, 0, $encoding) !== false;
return mb_strpos($haystack, $needle) !== false;
}
return mb_stripos($haystack, $needle, 0, $encoding) !== false;
return mb_stripos($haystack, $needle) !== false;
}
}
+2 -6
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
+2 -6
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
+2 -6
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
+7 -9
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -25,7 +21,7 @@ use function sprintf;
*
* This rule supports the three sets of country codes (alpha-2, alpha-3, and numeric).
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
* @author Felipe Martins <me@fefas.net>
* @author Henrique Moody <henriquemoody@gmail.com>
* @author William Espindola <oi@williamespindola.com.br>
@@ -60,6 +56,7 @@ final class CountryCode extends AbstractSearcher
* @see https://salsa.debian.org/iso-codes-team/iso-codes
*/
private const COUNTRY_CODES = [
// begin of auto-generated code
['AD', 'AND', '020'], // Andorra
['AE', 'ARE', '784'], // United Arab Emirates
['AF', 'AFG', '004'], // Afghanistan
@@ -310,7 +307,7 @@ final class CountryCode extends AbstractSearcher
['TN', 'TUN', '788'], // Tunisia
['TO', 'TON', '776'], // Tonga
['TP', 'TMP', '626'], // East Timor
['TR', 'TUR', '792'], // Turkey
['TR', 'TUR', '792'], // Türkiye
['TT', 'TTO', '780'], // Trinidad and Tobago
['TV', 'TUV', '798'], // Tuvalu
['TW', 'TWN', '158'], // Taiwan, Province of China
@@ -340,6 +337,7 @@ final class CountryCode extends AbstractSearcher
['ZM', 'ZMB', '894'], // Zambia
['ZR', 'ZAR', '180'], // Zaire, Republic of
['ZW', 'ZWE', '716'], // Zimbabwe
// end of auto-generated code
];
/**
@@ -370,7 +368,7 @@ final class CountryCode extends AbstractSearcher
/**
* {@inheritDoc}
*/
protected function getDataSource(): array
protected function getDataSource($input = null): array
{
return array_column(self::COUNTRY_CODES, self::SET_INDEXES[$this->set]);
}
+6 -9
View File
@@ -1,18 +1,15 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
namespace Respect\Validation\Rules;
use function intval;
use function mb_strlen;
use function preg_match;
use function preg_replace;
@@ -20,7 +17,7 @@ use function preg_replace;
/**
* Validates whether the input is a CPF (Brazilian Natural Persons Register) number.
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Jair Henrique <jair.henrique@gmail.com>
* @author Jayson Reis <santosdosreis@gmail.com>
@@ -43,7 +40,7 @@ final class Cpf extends AbstractRule
$n = 0;
for ($s = 10, $i = 0; $s >= 2; ++$i, --$s) {
$n += $c[$i] * $s;
$n += intval($c[$i]) * $s;
}
if ($c[9] != (($n %= 11) < 2 ? 0 : 11 - $n)) {
@@ -52,7 +49,7 @@ final class Cpf extends AbstractRule
$n = 0;
for ($s = 11, $i = 0; $s >= 2; ++$i, --$s) {
$n += $c[$i] * $s;
$n += intval($c[$i]) * $s;
}
$check = ($n %= 11) < 2 ? 0 : 11 - $n;
+6 -6
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -31,6 +27,7 @@ use function sprintf;
* @author Jean Pimentel <jeanfap@gmail.com>
* @author Nick Lombard <github@jigsoft.co.za>
* @author William Espindola <oi@williamespindola.com.br>
* @author Rakshit Arora <rakshit087@gmail.com>
*/
final class CreditCard extends AbstractRule
{
@@ -48,6 +45,8 @@ final class CreditCard extends AbstractRule
public const VISA = 'Visa';
public const RUPAY = 'RuPay';
private const BRAND_REGEX_LIST = [
self::ANY => '/^[0-9]+$/',
self::AMERICAN_EXPRESS => '/^3[47]\d{13}$/',
@@ -56,6 +55,7 @@ final class CreditCard extends AbstractRule
self::JCB => '/^(?:2131|1800|35\d{3})\d{11}$/',
self::MASTERCARD => '/(5[1-5]|2[2-7])\d{14}$/',
self::VISA => '/^4\d{12}(?:\d{3})?$/',
self::RUPAY => '/^6(?!011)(?:0[0-9]{14}|52[12][0-9]{12})$/',
];
/**
+5 -8
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -28,7 +24,7 @@ final class CurrencyCode extends AbstractSearcher
*
* {@inheritDoc}
*/
protected function getDataSource(): array
protected function getDataSource($input = null): array
{
return [
'AED', // UAE Dirham
@@ -92,7 +88,6 @@ final class CurrencyCode extends AbstractSearcher
'GYD', // Guyana Dollar
'HKD', // Hong Kong Dollar
'HNL', // Lempira
'HRK', // Kuna
'HTG', // Gourde
'HUF', // Forint
'IDR', // Rupiah
@@ -160,6 +155,7 @@ final class CurrencyCode extends AbstractSearcher
'SEK', // Swedish Krona
'SGD', // Singapore Dollar
'SHP', // Saint Helena Pound
'SLE', // Leone
'SLL', // Leone
'SOS', // Somali Shilling
'SRD', // Surinam Dollar
@@ -185,6 +181,7 @@ final class CurrencyCode extends AbstractSearcher
'UYU', // Peso Uruguayo
'UYW', // Unidad Previsional
'UZS', // Uzbekistan Sum
'VED', // Bolívar Soberano
'VES', // Bolívar Soberano
'VND', // Dong
'VUV', // Vatu
+2 -6
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
+3 -7
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -21,7 +17,7 @@ use function is_scalar;
use function strtotime;
/**
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
* @author Emmerson Siqueira <emmersonsiqueira@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
+71
View File
@@ -0,0 +1,71 @@
<?php
/*
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
namespace Respect\Validation\Rules;
use function is_numeric;
use function is_string;
use function number_format;
use function preg_replace;
use function var_export;
/**
* Validates the decimal
*
* @author Henrique Moody <henriquemoody@gmail.com>
*/
final class Decimal extends AbstractRule
{
/**
* @var int
*/
private $decimals;
public function __construct(int $decimals)
{
$this->decimals = $decimals;
}
/**
* {@inheritDoc}
*/
public function validate($input): bool
{
if (!is_numeric($input)) {
return false;
}
return $this->toFormattedString($input) === $this->toRawString($input);
}
/**
* @param mixed $input
*/
private function toRawString($input): string
{
if (is_string($input)) {
return $input;
}
return var_export($input, true);
}
/**
* @param mixed $input
*/
private function toFormattedString($input): string
{
$formatted = number_format((float) $input, $this->decimals, '.', '');
if (is_string($input)) {
return $formatted;
}
return preg_replace('/^(\d+\.\d)0*$/', '$1', $formatted) ?: '';
}
}
+3 -7
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -18,7 +14,7 @@ use function ctype_digit;
/**
* Validates whether the input contains only digits.
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Nick Lombard <github@jigsoft.co.za>
*/
+2 -6
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
+3 -7
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -28,7 +24,7 @@ use function mb_substr_count;
/**
* Validates whether the input is a valid domain name or not.
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Mehmet Tolga Avcioglu <mehmet@activecom.net>
* @author Nick Lombard <github@jigsoft.co.za>
+3 -7
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -21,7 +17,7 @@ use Respect\Validation\Validatable;
/**
* Validates whether each value in the input is valid according to another rule.
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Nick Lombard <github@jigsoft.co.za>
* @author William Espindola <oi@williamespindola.com.br>
+2 -6
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
+5 -16
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -15,7 +11,6 @@ namespace Respect\Validation\Rules;
use function end;
use function is_array;
use function mb_detect_encoding;
use function mb_strlen;
use function mb_strripos;
use function mb_strrpos;
@@ -23,7 +18,7 @@ use function mb_strrpos;
/**
* Validates only if the value is at the end of the input.
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Hugo Hamon <hugo.hamon@sensiolabs.com>
* @author William Espindola <oi@williamespindola.com.br>
@@ -70,10 +65,7 @@ final class EndsWith extends AbstractRule
return end($input) == $this->endValue;
}
$encoding = (string) mb_detect_encoding($input);
$endPosition = mb_strlen($input, $encoding) - mb_strlen($this->endValue, $encoding);
return mb_strripos($input, $this->endValue, -1, $encoding) === $endPosition;
return mb_strripos($input, $this->endValue) === mb_strlen($input) - mb_strlen($this->endValue);
}
/**
@@ -85,9 +77,6 @@ final class EndsWith extends AbstractRule
return end($input) === $this->endValue;
}
$encoding = (string) mb_detect_encoding($input);
$endPosition = mb_strlen($input, $encoding) - mb_strlen($this->endValue, $encoding);
return mb_strrpos($input, $this->endValue, 0, $encoding) === $endPosition;
return mb_strrpos($input, $this->endValue) === mb_strlen($input) - mb_strlen($this->endValue);
}
}
+3 -7
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -16,7 +12,7 @@ namespace Respect\Validation\Rules;
/**
* Validates if the input is equal to some value.
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Hugo Hamon <hugo.hamon@sensiolabs.com>
*/
+2 -6
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
+2 -6
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
+2 -6
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
+2 -6
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
+2 -6
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
+2 -6
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
+2 -6
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
+2 -6
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
+2 -6
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
+25 -17
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -15,7 +11,10 @@ namespace Respect\Validation\Rules;
use Respect\Validation\Exceptions\ComponentException;
use function in_array;
use function array_key_exists;
use function filter_var;
use function is_array;
use function is_int;
use const FILTER_VALIDATE_BOOLEAN;
use const FILTER_VALIDATE_DOMAIN;
@@ -34,14 +33,14 @@ use const FILTER_VALIDATE_URL;
final class FilterVar extends AbstractEnvelope
{
private const ALLOWED_FILTERS = [
FILTER_VALIDATE_BOOLEAN,
FILTER_VALIDATE_DOMAIN,
FILTER_VALIDATE_EMAIL,
FILTER_VALIDATE_FLOAT,
FILTER_VALIDATE_INT,
FILTER_VALIDATE_IP,
FILTER_VALIDATE_REGEXP,
FILTER_VALIDATE_URL,
FILTER_VALIDATE_BOOLEAN => 'is_bool',
FILTER_VALIDATE_DOMAIN => 'is_string',
FILTER_VALIDATE_EMAIL => 'is_string',
FILTER_VALIDATE_FLOAT => 'is_float',
FILTER_VALIDATE_INT => 'is_int',
FILTER_VALIDATE_IP => 'is_string',
FILTER_VALIDATE_REGEXP => 'is_string',
FILTER_VALIDATE_URL => 'is_string',
];
/**
@@ -53,10 +52,19 @@ final class FilterVar extends AbstractEnvelope
*/
public function __construct(int $filter, $options = [])
{
if (!in_array($filter, self::ALLOWED_FILTERS)) {
if (!array_key_exists($filter, self::ALLOWED_FILTERS)) {
throw new ComponentException('Cannot accept the given filter');
}
parent::__construct(new Callback('filter_var', $filter, $options));
$arguments = [$filter];
if (is_array($options) || is_int($options)) {
$arguments[] = $options;
}
parent::__construct(new Callback(static function ($input) use ($filter, $arguments) {
return (self::ALLOWED_FILTERS[$filter])(
filter_var($input, ...$arguments)
);
}));
}
}
+2 -6
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
+2 -6
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
+3 -7
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -21,7 +17,7 @@ use const FILTER_VALIDATE_FLOAT;
/**
* Validate whether the input value is float.
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
* @author Danilo Benevides <danilobenevides01@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Jayson Reis <santosdosreis@gmail.com>
+2 -6
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
+2 -6
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
+3 -7
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -23,6 +19,6 @@ final class HexRgbColor extends AbstractEnvelope
{
public function __construct()
{
parent::__construct(new Regex('/^#?([0-9A-F]{3}|[0-9A-F]{6})$/'));
parent::__construct(new Regex('/^#?([0-9A-F]{3}|[0-9A-F]{6})$/i'));
}
}
+7 -10
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -20,6 +16,7 @@ use function preg_match;
use function preg_replace_callback;
use function str_replace;
use function strlen;
use function strval;
use function substr;
/**
@@ -127,7 +124,7 @@ final class Iban extends AbstractRule
$bban = substr($iban, 4);
$rearranged = $bban . $countryCode . $checkDigits;
return bcmod($this->convertToInteger($rearranged), '97') === '1';
return bcmod($this->convertToIntegerAsString($rearranged), '97') === '1';
}
private function hasValidCountryLength(string $iban, string $countryCode): bool
@@ -139,12 +136,12 @@ final class Iban extends AbstractRule
return strlen($iban) === self::COUNTRIES_LENGTHS[$countryCode];
}
private function convertToInteger(string $reArrangedIban): string
private function convertToIntegerAsString(string $reArrangedIban): string
{
return (string) preg_replace_callback(
'/[A-Z]/',
static function (array $match): int {
return ord($match[0]) - 55;
static function (array $match): string {
return strval(ord($match[0]) - 55);
},
$reArrangedIban
);
+2 -6
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
+2 -6
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
+2 -6
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
+5 -14
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -15,14 +11,13 @@ namespace Respect\Validation\Rules;
use function in_array;
use function is_array;
use function mb_detect_encoding;
use function mb_stripos;
use function mb_strpos;
/**
* Validates if the input can be found in a defined array or string.
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
* @author Danilo Benevides <danilobenevides01@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
@@ -74,9 +69,7 @@ final class In extends AbstractRule
return $input == $this->haystack;
}
$inputString = (string) $input;
return mb_stripos($this->haystack, $inputString, 0, (string) mb_detect_encoding($inputString)) !== false;
return mb_stripos($this->haystack, (string) $input) !== false;
}
/**
@@ -92,8 +85,6 @@ final class In extends AbstractRule
return $input === $this->haystack;
}
$inputString = (string) $input;
return mb_strpos($this->haystack, $inputString, 0, (string) mb_detect_encoding($inputString)) !== false;
return mb_strpos($this->haystack, (string) $input) !== false;
}
}
+2 -6
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
+3 -7
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -16,7 +12,7 @@ namespace Respect\Validation\Rules;
/**
* Validates if the input is an instance of the given class or interface.
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
* @author Danilo Benevides <danilobenevides01@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
+2 -6
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
+10 -9
View File
@@ -1,26 +1,23 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
namespace Respect\Validation\Rules;
use function ctype_digit;
use function is_int;
use function is_string;
use function preg_match;
/**
* Validates if the input is an integer.
*
* @author Adam Benson <adam.benson@bigcommerce.com>
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
* @author Andrei Drulchenko <andrdru@gmail.com>
* @author Danilo Benevides <danilobenevides01@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
@@ -36,6 +33,10 @@ final class IntVal extends AbstractRule
return true;
}
return ctype_digit($input);
if (!is_string($input)) {
return false;
}
return preg_match('/^-?\d+$/', $input) === 1;
}
}
+5 -9
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -35,7 +31,7 @@ use const FILTER_VALIDATE_IP;
*
* This validator uses the native filter_var() PHP function.
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
* @author Danilo Benevides <danilobenevides01@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
@@ -105,7 +101,7 @@ final class Ip extends AbstractRule
private function createRange(): ?string
{
if ($this->endAddress && $this->endAddress) {
if ($this->startAddress && $this->endAddress) {
return $this->startAddress . '-' . $this->endAddress;
}
@@ -181,7 +177,7 @@ final class Ip extends AbstractRule
throw new ComponentException('Invalid network mask');
}
$this->mask = sprintf('%032b', ip2long(long2ip(~(2 ** (32 - (int) $parts[1]) - 1))));
$this->mask = sprintf('%032b', ip2long((string) long2ip(~(2 ** (32 - (int) $parts[1]) - 1))));
}
private function verifyAddress(string $address): bool
+2 -6
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
+2 -6
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
+8 -7
View File
@@ -1,18 +1,15 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
namespace Respect\Validation\Rules;
use function function_exists;
use function is_string;
use function json_decode;
use function json_last_error;
@@ -20,7 +17,7 @@ use function json_last_error;
use const JSON_ERROR_NONE;
/**
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
* @author Danilo Benevides <danilobenevides01@gmail.com>
* @author Emmerson Siqueira <emmersonsiqueira@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
@@ -36,6 +33,10 @@ final class Json extends AbstractRule
return false;
}
if (function_exists('json_validate')) {
return json_validate($input);
}
json_decode($input);
return json_last_error() === JSON_ERROR_NONE;
+3 -7
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -21,7 +17,7 @@ use function is_array;
use function is_scalar;
/**
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
* @author Emmerson Siqueira <emmersonsiqueira@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
+5 -9
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -28,7 +24,7 @@ use function rtrim;
use function sprintf;
/**
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
* @author Emmerson Siqueira <emmersonsiqueira@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Ivan Zinovyev <vanyazin@gmail.com>
@@ -93,6 +89,7 @@ final class KeyNested extends AbstractRelated
}
/**
* @param ArrayAccess<mixed, mixed> $array
* @param mixed $key
*
* @return mixed
@@ -110,11 +107,10 @@ final class KeyNested extends AbstractRelated
/**
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
*
* @param object $object
*
* @return mixed
*/
private function getValueFromObject($object, string $property)
private function getValueFromObject(object $object, string $property)
{
if (empty($property) || !property_exists($object, $property)) {
$message = sprintf('Cannot select the property %s from the given object', $this->getReference());
+15 -8
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -14,6 +10,7 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\NonNegatable;
use Respect\Validation\Validatable;
use function array_key_exists;
@@ -28,13 +25,18 @@ use function is_array;
* @author Emmerson Siqueira <emmersonsiqueira@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
final class KeySet extends AbstractWrapper
final class KeySet extends AbstractWrapper implements NonNegatable
{
/**
* @var mixed[]
*/
private $keys;
/**
* @var mixed[]
*/
private $extraKeys = [];
/**
* @var Key[]
*/
@@ -43,7 +45,8 @@ final class KeySet extends AbstractWrapper
/**
* Initializes the rule.
*
* @param Validatable[] ...$validatables
* phpcs:ignore SlevomatCodingStandard.TypeHints.ParameterTypeHint.UselessAnnotation
* @param Validatable ...$validatables
*/
public function __construct(Validatable ...$validatables)
{
@@ -130,6 +133,10 @@ final class KeySet extends AbstractWrapper
unset($input[$keyRule->getReference()]);
}
foreach ($input as $extraKey => &$ignoreValue) {
$this->extraKeys[] = $extraKey;
}
return count($input) == 0;
}
}
+2 -6
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
+2 -6
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
+2 -6
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
+2 -6
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
+4 -9
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -22,14 +18,13 @@ use function is_array;
use function is_int;
use function is_object;
use function is_string;
use function mb_detect_encoding;
use function mb_strlen;
use function sprintf;
/**
* Validates the length of the given input.
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
* @author Blake Hair <blake.hair@gmail.com>
* @author Danilo Correa <danilosilva87@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
@@ -89,7 +84,7 @@ final class Length extends AbstractRule
private function extractLength($input): ?int
{
if (is_string($input)) {
return (int) mb_strlen($input, (string) mb_detect_encoding($input));
return (int) mb_strlen($input);
}
if (is_array($input) || $input instanceof CountableInterface) {
+2 -6
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
+4 -9
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -14,13 +10,12 @@ declare(strict_types=1);
namespace Respect\Validation\Rules;
use function is_string;
use function mb_detect_encoding;
use function mb_strtolower;
/**
* Validates whether the characters in the input are lowercase.
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
* @author Danilo Benevides <danilobenevides01@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Jean Pimentel <jeanfap@gmail.com>
@@ -36,6 +31,6 @@ final class Lowercase extends AbstractRule
return false;
}
return $input === mb_strtolower($input, (string) mb_detect_encoding($input));
return $input === mb_strtolower($input);
}
}
+4 -8
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -48,7 +44,7 @@ final class Luhn extends AbstractRule
$parity = $numDigits % 2;
for ($i = 0; $i < $numDigits; ++$i) {
$digit = $digits[$i];
if ($parity == ($i % 2)) {
if ($parity == $i % 2) {
$digit <<= 1;
if (9 < $digit) {
$digit = $digit - 9;
@@ -57,6 +53,6 @@ final class Luhn extends AbstractRule
$sum += $digit;
}
return ($sum % 10) == 0;
return $sum % 10 == 0;
}
}
+3 -7
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -19,7 +15,7 @@ use function preg_match;
/**
* Validates whether the input is a valid MAC address.
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
* @author Danilo Correa <danilosilva87@gmail.com>
* @author Fábio da Silva Ribeiro <fabiorphp@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
+3 -7
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -16,7 +12,7 @@ namespace Respect\Validation\Rules;
/**
* Validates whether the input is less than or equal to a value.
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
final class Max extends AbstractComparison
+2 -6
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
+2 -6
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
+3 -7
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -16,7 +12,7 @@ namespace Respect\Validation\Rules;
/**
* Validates whether the input is greater than or equal to a value.
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
final class Min extends AbstractComparison
+2 -6
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
+30
View File
@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace Respect\Validation\Rules;
use libphonenumber\NumberParseException;
use function is_scalar;
final class Mobile extends AbstractRule
{
/**
* {@inheritDoc}
*/
public function validate($input): bool
{
if (!is_scalar($input)) {
return false;
}
try {
// 此处使用简单的正则表达式检查中国大陆的手机号码格式
return preg_match('/^1[3456789]\d{9}$/', (string) $input) === 1;
} catch (NumberParseException $e) {
return false;
}
}
}
+2 -6
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
+3 -7
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -18,7 +14,7 @@ use function is_numeric;
/**
* Validates whether the input is a negative number.
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Ismael Elias <ismael.esq@hotmail.com>
*/
+4 -8
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -44,7 +40,7 @@ final class NfeAccessKey extends AbstractRule
$digits = array_map('intval', str_split($input));
$w = [];
for ($i = 0, $z = 5, $m = 43; $i <= $m; ++$i) {
$z = $i < $m ? ($z - 1) == 1 ? 9 : ($z - 1) : 0;
$z = $i < $m ? $z - 1 == 1 ? 9 : $z - 1 : 0;
$w[] = $z;
}
@@ -53,7 +49,7 @@ final class NfeAccessKey extends AbstractRule
}
$s -= 11 * floor($s / 11);
$v = $s == 0 || $s == 1 ? 0 : (11 - $s);
$v = $s == 0 || $s == 1 ? 0 : 11 - $s;
return $v == $digits[43];
}
+5 -9
View File
@@ -1,12 +1,8 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
@@ -59,7 +55,7 @@ final class Nif extends AbstractRule
private function validateDni(int $number, string $control): bool
{
return mb_substr('TRWAGMYFPDXBNJZSQVHLCKE', ($number % 23), 1) === $control;
return mb_substr('TRWAGMYFPDXBNJZSQVHLCKE', $number % 23, 1) === $control;
}
private function validateNie(string $prefix, string $number, string $control): bool
@@ -92,12 +88,12 @@ final class Nif extends AbstractRule
$digits = str_split((string) $code);
$lastDigit = (int) array_pop($digits);
$key = $lastDigit === 0 ? 0 : (10 - $lastDigit);
$key = $lastDigit === 0 ? 0 : 10 - $lastDigit;
if (is_numeric($control)) {
return (int) $key === (int) $control;
}
return mb_substr('JABCDEFGHI', ($key % 10), 1) === $control;
return mb_substr('JABCDEFGHI', $key % 10, 1) === $control;
}
}

Some files were not shown because too many files have changed in this diff Show More