INIT
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
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,86 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Respect\Validation\Helpers;
|
||||
|
||||
use DateTime;
|
||||
use DateTimeZone;
|
||||
|
||||
use function checkdate;
|
||||
use function date_default_timezone_get;
|
||||
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',
|
||||
];
|
||||
|
||||
$format = $exceptionalFormats[$format] ?? $format;
|
||||
|
||||
$info = date_parse_from_format($format, $value);
|
||||
|
||||
if (!$this->isDateTimeParsable($info)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->isDateFormat($format)) {
|
||||
$formattedDate = DateTime::createFromFormat(
|
||||
$format,
|
||||
$value,
|
||||
new DateTimeZone(date_default_timezone_get())
|
||||
);
|
||||
|
||||
if ($formattedDate === false || $value !== $formattedDate->format($format)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->isDateInformation($info);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed[] $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, 1, $info['year'] ?: 1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
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,30 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
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,54 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Respect\Validation\Helpers;
|
||||
|
||||
use Respect\Validation\Exceptions\ComponentException;
|
||||
|
||||
use function file_exists;
|
||||
use function sprintf;
|
||||
|
||||
final class CountryInfo
|
||||
{
|
||||
/**
|
||||
* @var mixed[]
|
||||
*/
|
||||
private $data;
|
||||
|
||||
/**
|
||||
* @var mixed[]
|
||||
*/
|
||||
private static $runtimeCache = [];
|
||||
|
||||
public function __construct(string $countryCode)
|
||||
{
|
||||
if (!isset(static::$runtimeCache[$countryCode])) {
|
||||
$filename = __DIR__ . '/../../data/iso_3166-2/' . $countryCode . '.php';
|
||||
if (!file_exists($filename)) {
|
||||
throw new ComponentException(sprintf('"%s" is not a supported country code', $countryCode));
|
||||
}
|
||||
static::$runtimeCache[$countryCode] = require $filename;
|
||||
}
|
||||
|
||||
$this->data = static::$runtimeCache[$countryCode];
|
||||
}
|
||||
|
||||
public function getCountry(): string
|
||||
{
|
||||
return $this->data['country'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getSubdivisions(): array
|
||||
{
|
||||
return $this->data['subdivisions'];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Respect\Validation\Helpers;
|
||||
|
||||
use function file_exists;
|
||||
use function mb_strtoupper;
|
||||
|
||||
final class DomainInfo
|
||||
{
|
||||
/**
|
||||
* @var mixed[]
|
||||
*/
|
||||
private $data;
|
||||
|
||||
/**
|
||||
* @var mixed[]
|
||||
*/
|
||||
private static $runtimeCache = [];
|
||||
|
||||
public function __construct(string $tld)
|
||||
{
|
||||
$tld = mb_strtoupper($tld);
|
||||
|
||||
if (!isset(static::$runtimeCache[$tld])) {
|
||||
$filename = __DIR__ . '/../../data/domain/public-suffix/' . $tld . '.php';
|
||||
static::$runtimeCache[$tld] = file_exists($filename) ? require $filename : [];
|
||||
}
|
||||
|
||||
$this->data = static::$runtimeCache[$tld];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string>
|
||||
*/
|
||||
public function getPublicSuffixes(): array
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user