This commit is contained in:
2022-12-24 22:10:40 +08:00
parent 84fc1030a7
commit 2a00928da5
4898 changed files with 429855 additions and 77 deletions
@@ -0,0 +1,72 @@
<?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.
*/
declare(strict_types=1);
namespace Respect\Validation\Helpers;
use Countable;
use DateTimeImmutable;
use DateTimeInterface;
use Throwable;
use function is_numeric;
use function is_scalar;
use function is_string;
use function mb_strlen;
/**
* Helps to deal with comparable values.
*
* @author Emmerson Siqueira <emmersonsiqueira@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
trait CanCompareValues
{
/**
* Tries to convert a value into something that can be compared with PHP operators.
*
* @param mixed $value
*
* @return mixed
*/
private function toComparable($value)
{
if ($value instanceof Countable) {
return $value->count();
}
if ($value instanceof DateTimeInterface || !is_string($value) || is_numeric($value) || empty($value)) {
return $value;
}
if (mb_strlen($value) === 1) {
return $value;
}
try {
return new DateTimeImmutable($value);
} catch (Throwable $e) {
return $value;
}
}
/**
* Returns whether the values can be compared or not.
*
* @param mixed $left
* @param mixed $right
*/
private function isAbleToCompareValues($left, $right): bool
{
return is_scalar($left) === is_scalar($right);
}
}
@@ -0,0 +1,74 @@
<?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.
*/
declare(strict_types=1);
namespace Respect\Validation\Helpers;
use function checkdate;
use function date_parse_from_format;
use function preg_match;
/**
* Helper to handle date/time.
*
* @author Henrique Moody <henriquemoody@gmail.com>
*/
trait CanValidateDateTime
{
/**
* Finds whether a value is a valid date/time in a specific format.
*/
private function isDateTime(string $format, string $value): bool
{
$exceptionalFormats = [
'c' => 'Y-m-d\TH:i:sP',
'r' => 'D, d M Y H:i:s O',
];
$info = date_parse_from_format($exceptionalFormats[$format] ?? $format, $value);
if (!$this->isDateTimeParsable($info)) {
return false;
}
if ($this->isDateFormat($format)) {
return $this->isDateInformation($info);
}
return true;
}
/**
* @param int[] $info
*/
private function isDateTimeParsable(array $info): bool
{
return $info['error_count'] === 0 && $info['warning_count'] === 0;
}
private function isDateFormat(string $format): bool
{
return preg_match('/[djSFmMnYy]/', $format) > 0;
}
/**
* @param mixed[] $info
*/
private function isDateInformation(array $info): bool
{
if ($info['day']) {
return checkdate((int) $info['month'], $info['day'], (int) $info['year']);
}
return checkdate($info['month'] ?: 1, $info['day'] ?: 1, $info['year'] ?: 1);
}
}
@@ -0,0 +1,37 @@
<?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.
*/
declare(strict_types=1);
namespace Respect\Validation\Helpers;
use stdClass;
use Traversable;
use function is_array;
/**
* Helper to handle iterable values.
*
* @author Henrique Moody <henriquemoody@gmail.com>
*/
trait CanValidateIterable
{
/**
* Returns whether the value is iterable or not.
*
* @param mixed $value
*/
public function isIterable($value): bool
{
return is_array($value) || $value instanceof stdClass || $value instanceof Traversable;
}
}
@@ -0,0 +1,34 @@
<?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.
*/
declare(strict_types=1);
namespace Respect\Validation\Helpers;
use function in_array;
/**
* Helper to identify values that Validation consider as "undefined".
*
* @author Henrique Moody <henriquemoody@gmail.com>
*/
trait CanValidateUndefined
{
/**
* Finds whether the value is undefined or not.
*
* @param mixed $value
*/
private function isUndefined($value): bool
{
return in_array($value, [null, ''], true);
}
}
@@ -0,0 +1,52 @@
<?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.
*/
declare(strict_types=1);
namespace Respect\Validation\Helpers;
use Respect\Validation\Exceptions\ComponentException;
use function file_exists;
use function file_get_contents;
use function json_decode;
use function sprintf;
final class Subdivisions
{
/**
* @var mixed[]
*/
private $data;
public function __construct(string $countryCode)
{
$filename = __DIR__ . '/../../data/iso_3166-2/' . $countryCode . '.json';
if (!file_exists($filename)) {
throw new ComponentException(sprintf('"%s" is not a supported country code', $countryCode));
}
$this->data = (array) json_decode((string) file_get_contents($filename), true);
}
public function getCountry(): string
{
return $this->data['country'];
}
/**
* @return string[]
*/
public function getSubdivisions(): array
{
return $this->data['subdivisions'];
}
}