Remove duplicated code

We expect that every rule that has a custom template to use that
template instead of the standard one. This change ensures that happens.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
Henrique Moody 2024-02-09 19:44:52 +01:00
parent 02b70bf1cb
commit c6677fd7ce
No known key found for this signature in database
GPG key ID: 221E9281655813A6
20 changed files with 105 additions and 148 deletions

View file

@ -43,11 +43,6 @@ abstract class AbstractFilterRule extends AbstractRule
return $filteredInput === '' || $this->validateFilteredInput($filteredInput);
}
public function getTemplate(mixed $input): string
{
return $this->template ?? ($this->additionalChars ? self::TEMPLATE_EXTRA : self::TEMPLATE_STANDARD);
}
/**
* @return array<string, mixed>
*/
@ -56,6 +51,11 @@ abstract class AbstractFilterRule extends AbstractRule
return ['additionalChars' => $this->additionalChars];
}
protected function getStandardTemplate(mixed $input): string
{
return $this->additionalChars ? self::TEMPLATE_EXTRA : self::TEMPLATE_STANDARD;
}
private function filter(string $input): string
{
return str_replace(str_split($this->additionalChars), '', $input);

View file

@ -108,12 +108,8 @@ abstract class AbstractRelated extends AbstractRule
return $this->rule->validate($this->getReferenceValue($input));
}
public function getTemplate(mixed $input): string
protected function getStandardTemplate(mixed $input): string
{
if ($this->template !== null) {
return $this->template;
}
if ($this->rule === null) {
return self::TEMPLATE_NOT_PRESENT;
}

View file

@ -62,7 +62,7 @@ abstract class AbstractRule implements Validatable
public function getTemplate(mixed $input): string
{
return $this->template ?? self::TEMPLATE_STANDARD;
return $this->template ?? $this->getStandardTemplate($input);
}
/**
@ -73,6 +73,11 @@ abstract class AbstractRule implements Validatable
return [];
}
protected function getStandardTemplate(mixed $input): string
{
return self::TEMPLATE_STANDARD;
}
public function __invoke(mixed $input): bool
{
return $this->validate($input);

View file

@ -44,11 +44,6 @@ class AllOf extends AbstractComposite
}
}
public function getTemplate(mixed $input): string
{
return $this->template ?? self::TEMPLATE_SOME;
}
public function check(mixed $input): void
{
foreach ($this->getRules() as $rule) {
@ -66,4 +61,9 @@ class AllOf extends AbstractComposite
return true;
}
protected function getStandardTemplate(mixed $input): string
{
return self::TEMPLATE_SOME;
}
}

View file

@ -88,19 +88,6 @@ final class CreditCard extends AbstractRule
return preg_match(self::BRAND_REGEX_LIST[$this->brand], $input) > 0;
}
public function getTemplate(mixed $input): string
{
if ($this->template !== null) {
return $this->template;
}
if ($this->brand === CreditCard::ANY) {
return self::TEMPLATE_STANDARD;
}
return self::TEMPLATE_BRANDED;
}
/**
* @return array<string, string>
*/
@ -108,4 +95,13 @@ final class CreditCard extends AbstractRule
{
return ['brand' => $this->brand];
}
protected function getStandardTemplate(mixed $input): string
{
if ($this->brand === CreditCard::ANY) {
return self::TEMPLATE_STANDARD;
}
return self::TEMPLATE_BRANDED;
}
}

View file

@ -55,11 +55,6 @@ final class DateTime extends AbstractRule
return $this->isDateTime($this->format, (string) $input);
}
public function getTemplate(mixed $input): string
{
return $this->template ?? ($this->format !== null ? self::TEMPLATE_FORMAT : self::TEMPLATE_STANDARD);
}
/**
* @return array<string, mixed>
*/
@ -67,4 +62,9 @@ final class DateTime extends AbstractRule
{
return ['sample' => date($this->format ?: 'c', strtotime('2005-12-30 01:02:03'))];
}
protected function getStandardTemplate(mixed $input): string
{
return $this->format !== null ? self::TEMPLATE_FORMAT : self::TEMPLATE_STANDARD;
}
}

View file

@ -76,11 +76,6 @@ final class Ip extends AbstractRule
return true;
}
public function getTemplate(mixed $input): string
{
return $this->template ?? ($this->range ? self::TEMPLATE_NETWORK_RANGE : self::TEMPLATE_STANDARD);
}
/**
* @return array<string, mixed>
*/
@ -89,6 +84,11 @@ final class Ip extends AbstractRule
return ['range' => $this->range];
}
protected function getStandardTemplate(mixed $input): string
{
return $this->range ? self::TEMPLATE_NETWORK_RANGE : self::TEMPLATE_STANDARD;
}
private function createRange(): ?string
{
if ($this->startAddress && $this->endAddress) {

View file

@ -100,19 +100,6 @@ final class KeySet extends AbstractWrapper implements NonNegatable
return parent::validate($input);
}
public function getTemplate(mixed $input): string
{
if ($this->template !== null) {
return $this->template;
}
if (count($this->extraKeys)) {
return self::TEMPLATE_STRUCTURE_EXTRA;
}
return KeySet::TEMPLATE_STRUCTURE;
}
/**
* @return array<string, mixed>
*/
@ -124,6 +111,15 @@ final class KeySet extends AbstractWrapper implements NonNegatable
];
}
protected function getStandardTemplate(mixed $input): string
{
if (count($this->extraKeys)) {
return self::TEMPLATE_STRUCTURE_EXTRA;
}
return KeySet::TEMPLATE_STRUCTURE;
}
private function getKeyRule(Validatable $validatable): Key
{
if ($validatable instanceof Key) {

View file

@ -73,25 +73,6 @@ final class KeyValue extends AbstractRule
return $rule->validate($input[$this->comparedKey]);
}
public function getTemplate(mixed $input): string
{
if ($this->template !== null) {
return $this->template;
}
if (!isset($input[$this->comparedKey]) || !isset($input[$this->baseKey])) {
return self::TEMPLATE_STANDARD;
}
try {
$this->createRule($input[$this->baseKey]);
} catch (ComponentException) {
return self::TEMPLATE_COMPONENT;
}
return self::TEMPLATE_STANDARD;
}
/**
* @return array<string, mixed>
*/
@ -115,6 +96,21 @@ final class KeyValue extends AbstractRule
}
}
protected function getStandardTemplate(mixed $input): string
{
if (!isset($input[$this->comparedKey]) || !isset($input[$this->baseKey])) {
return self::TEMPLATE_STANDARD;
}
try {
$this->createRule($input[$this->baseKey]);
} catch (ComponentException) {
return self::TEMPLATE_COMPONENT;
}
return self::TEMPLATE_STANDARD;
}
private function getRule(mixed $input): Validatable
{
if (!isset($input[$this->comparedKey])) {

View file

@ -82,12 +82,19 @@ final class Length extends AbstractRule
return $this->validateMin($length) && $this->validateMax($length);
}
public function getTemplate(mixed $input): string
/**
* @return array<string, mixed>
*/
public function getParams(): array
{
if ($this->template !== null) {
return $this->template;
}
return [
'minValue' => $this->minValue,
'maxValue' => $this->maxValue,
];
}
protected function getStandardTemplate(mixed $input): string
{
if (!$this->minValue) {
return $this->inclusive === true ? self::TEMPLATE_GREATER_INCLUSIVE : self::TEMPLATE_GREATER;
}
@ -103,17 +110,6 @@ final class Length extends AbstractRule
return self::TEMPLATE_BOTH;
}
/**
* @return array<string, mixed>
*/
public function getParams(): array
{
return [
'minValue' => $this->minValue,
'maxValue' => $this->maxValue,
];
}
private function extractLength(mixed $input): ?int
{
if (is_string($input)) {

View file

@ -53,12 +53,8 @@ final class NotBlank extends AbstractRule
return !empty($input);
}
public function getTemplate(mixed $input): string
protected function getStandardTemplate(mixed $input): string
{
if ($this->template !== null) {
return $this->template;
}
if ($input || $this->getName()) {
return self::TEMPLATE_NAMED;
}

View file

@ -37,12 +37,8 @@ final class NotEmpty extends AbstractRule
return !empty($input);
}
public function getTemplate(mixed $input): string
protected function getStandardTemplate(mixed $input): string
{
if ($this->template !== null) {
return $this->template;
}
if ($input || $this->getName()) {
return self::TEMPLATE_NAMED;
}

View file

@ -33,12 +33,8 @@ final class NotOptional extends AbstractRule
return $this->isUndefined($input) === false;
}
public function getTemplate(mixed $input): string
protected function getStandardTemplate(mixed $input): string
{
if ($this->template !== null) {
return $this->template;
}
if ($input || $this->getName()) {
return self::TEMPLATE_NAMED;
}

View file

@ -52,12 +52,8 @@ final class Nullable extends AbstractWrapper
return parent::validate($input);
}
public function getTemplate(mixed $input): string
protected function getStandardTemplate(mixed $input): string
{
if ($this->template !== null) {
return $this->template;
}
if ($input || $this->getName()) {
return self::TEMPLATE_NAMED;
}

View file

@ -55,12 +55,8 @@ final class Optional extends AbstractWrapper
return parent::validate($input);
}
public function getTemplate(mixed $input): string
protected function getStandardTemplate(mixed $input): string
{
if ($this->template !== null) {
return $this->template;
}
if ($this->getName()) {
return self::TEMPLATE_NAMED;
}

View file

@ -70,11 +70,6 @@ final class Phone extends AbstractRule
}
}
public function getTemplate(mixed $input): string
{
return $this->template ?? $this->countryName ? self::TEMPLATE_FOR_COUNTRY : self::TEMPLATE_INTERNATIONAL;
}
/**
* @return array<string, mixed>
*/
@ -82,4 +77,9 @@ final class Phone extends AbstractRule
{
return ['countryName' => $this->countryName];
}
protected function getStandardTemplate(mixed $input): string
{
return $this->countryName ? self::TEMPLATE_FOR_COUNTRY : self::TEMPLATE_INTERNATIONAL;
}
}

View file

@ -76,23 +76,6 @@ final class Size extends AbstractRule
return false;
}
public function getTemplate(mixed $input): string
{
if ($this->template !== null) {
return $this->template;
}
if (!$this->minValue) {
return self::TEMPLATE_GREATER;
}
if (!$this->maxValue) {
return self::TEMPLATE_LOWER;
}
return self::TEMPLATE_BOTH;
}
/**
* @return array<string, mixed>
*/
@ -104,6 +87,19 @@ final class Size extends AbstractRule
];
}
protected function getStandardTemplate(mixed $input): string
{
if (!$this->minValue) {
return self::TEMPLATE_GREATER;
}
if (!$this->maxValue) {
return self::TEMPLATE_LOWER;
}
return self::TEMPLATE_BOTH;
}
/**
* @todo Move it to a trait
*/

View file

@ -64,12 +64,8 @@ final class Sorted extends AbstractRule
return true;
}
public function getTemplate(mixed $input): string
protected function getStandardTemplate(mixed $input): string
{
if ($this->template !== null) {
return $this->template;
}
if ($this->direction === Sorted::ASCENDING) {
return self::TEMPLATE_ASCENDING;
}

View file

@ -49,11 +49,6 @@ final class Uuid extends AbstractRule
return preg_match($this->getPattern(), $input) > 0;
}
public function getTemplate(mixed $input): string
{
return $this->template ?? ($this->version ? self::TEMPLATE_VERSION : self::TEMPLATE_STANDARD);
}
/**
* @return array<string, mixed>
*/
@ -62,6 +57,11 @@ final class Uuid extends AbstractRule
return ['version' => $this->version];
}
protected function getStandardTemplate(mixed $input): string
{
return $this->version ? self::TEMPLATE_VERSION : self::TEMPLATE_STANDARD;
}
private function isSupportedVersion(int $version): bool
{
return $version >= 1 && $version <= 5 && $version !== 2;

View file

@ -69,11 +69,6 @@ final class VideoUrl extends AbstractRule
return false;
}
public function getTemplate(mixed $input): string
{
return $this->template ?? ($this->service ? self::TEMPLATE_SERVICE : self::TEMPLATE_STANDARD);
}
/**
* @return array<string, string|null>
*/
@ -82,6 +77,11 @@ final class VideoUrl extends AbstractRule
return ['service' => $this->service];
}
protected function getStandardTemplate(mixed $input): string
{
return $this->service ? self::TEMPLATE_SERVICE : self::TEMPLATE_STANDARD;
}
private function isSupportedService(string $service): bool
{
return isset(self::SERVICES[mb_strtolower($service)]);