diff --git a/library/Rules/AbstractFilterRule.php b/library/Rules/AbstractFilterRule.php index 60806e410..9b5e9bba2 100644 --- a/library/Rules/AbstractFilterRule.php +++ b/library/Rules/AbstractFilterRule.php @@ -43,11 +43,6 @@ public function validate(mixed $input): bool return $filteredInput === '' || $this->validateFilteredInput($filteredInput); } - public function getTemplate(mixed $input): string - { - return $this->template ?? ($this->additionalChars ? self::TEMPLATE_EXTRA : self::TEMPLATE_STANDARD); - } - /** * @return array */ @@ -56,6 +51,11 @@ public function getParams(): array 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); diff --git a/library/Rules/AbstractRelated.php b/library/Rules/AbstractRelated.php index 9128d2126..80f263caa 100644 --- a/library/Rules/AbstractRelated.php +++ b/library/Rules/AbstractRelated.php @@ -108,12 +108,8 @@ public function validate(mixed $input): bool 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; } diff --git a/library/Rules/AbstractRule.php b/library/Rules/AbstractRule.php index 0c65466ce..f47045d6f 100644 --- a/library/Rules/AbstractRule.php +++ b/library/Rules/AbstractRule.php @@ -62,7 +62,7 @@ public function setTemplate(string $template): Validatable public function getTemplate(mixed $input): string { - return $this->template ?? self::TEMPLATE_STANDARD; + return $this->template ?? $this->getStandardTemplate($input); } /** @@ -73,6 +73,11 @@ public function getParams(): array return []; } + protected function getStandardTemplate(mixed $input): string + { + return self::TEMPLATE_STANDARD; + } + public function __invoke(mixed $input): bool { return $this->validate($input); diff --git a/library/Rules/AllOf.php b/library/Rules/AllOf.php index a179bedb9..ff13005ab 100644 --- a/library/Rules/AllOf.php +++ b/library/Rules/AllOf.php @@ -44,11 +44,6 @@ public function assert(mixed $input): void } } - 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 @@ public function validate(mixed $input): bool return true; } + + protected function getStandardTemplate(mixed $input): string + { + return self::TEMPLATE_SOME; + } } diff --git a/library/Rules/CreditCard.php b/library/Rules/CreditCard.php index ff1f4f7fa..9e3ba1711 100644 --- a/library/Rules/CreditCard.php +++ b/library/Rules/CreditCard.php @@ -88,24 +88,20 @@ public function validate(mixed $input): bool return preg_match(self::BRAND_REGEX_LIST[$this->brand], $input) > 0; } - public function getTemplate(mixed $input): string + /** + * @return array + */ + public function getParams(): array { - if ($this->template !== null) { - return $this->template; - } + return ['brand' => $this->brand]; + } + protected function getStandardTemplate(mixed $input): string + { if ($this->brand === CreditCard::ANY) { return self::TEMPLATE_STANDARD; } return self::TEMPLATE_BRANDED; } - - /** - * @return array - */ - public function getParams(): array - { - return ['brand' => $this->brand]; - } } diff --git a/library/Rules/DateTime.php b/library/Rules/DateTime.php index dc7faaed8..73850fa96 100644 --- a/library/Rules/DateTime.php +++ b/library/Rules/DateTime.php @@ -55,11 +55,6 @@ public function validate(mixed $input): bool 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 */ @@ -67,4 +62,9 @@ public function getParams(): array { 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; + } } diff --git a/library/Rules/Ip.php b/library/Rules/Ip.php index f99606396..bf8da26de 100644 --- a/library/Rules/Ip.php +++ b/library/Rules/Ip.php @@ -76,11 +76,6 @@ public function validate(mixed $input): bool return true; } - public function getTemplate(mixed $input): string - { - return $this->template ?? ($this->range ? self::TEMPLATE_NETWORK_RANGE : self::TEMPLATE_STANDARD); - } - /** * @return array */ @@ -89,6 +84,11 @@ public function getParams(): array 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) { diff --git a/library/Rules/KeySet.php b/library/Rules/KeySet.php index ca02e93d6..4a0bb834f 100644 --- a/library/Rules/KeySet.php +++ b/library/Rules/KeySet.php @@ -100,19 +100,6 @@ public function validate(mixed $input): bool 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 */ @@ -124,6 +111,15 @@ public function getParams(): array ]; } + 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) { diff --git a/library/Rules/KeyValue.php b/library/Rules/KeyValue.php index cb06297bd..c3edeeedb 100644 --- a/library/Rules/KeyValue.php +++ b/library/Rules/KeyValue.php @@ -73,25 +73,6 @@ public function validate(mixed $input): bool 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 */ @@ -115,6 +96,21 @@ public function reportError(mixed $input, array $extraParameters = []): Validati } } + 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])) { diff --git a/library/Rules/Length.php b/library/Rules/Length.php index 08e3fdbb1..b9100edca 100644 --- a/library/Rules/Length.php +++ b/library/Rules/Length.php @@ -82,12 +82,19 @@ public function validate(mixed $input): bool return $this->validateMin($length) && $this->validateMax($length); } - public function getTemplate(mixed $input): string + /** + * @return array + */ + 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 @@ public function getTemplate(mixed $input): string return self::TEMPLATE_BOTH; } - /** - * @return array - */ - public function getParams(): array - { - return [ - 'minValue' => $this->minValue, - 'maxValue' => $this->maxValue, - ]; - } - private function extractLength(mixed $input): ?int { if (is_string($input)) { diff --git a/library/Rules/NotBlank.php b/library/Rules/NotBlank.php index 9f8135f51..d5fc2ca4a 100644 --- a/library/Rules/NotBlank.php +++ b/library/Rules/NotBlank.php @@ -53,12 +53,8 @@ public function validate(mixed $input): bool 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; } diff --git a/library/Rules/NotEmpty.php b/library/Rules/NotEmpty.php index 95baeda45..8a4f55f26 100644 --- a/library/Rules/NotEmpty.php +++ b/library/Rules/NotEmpty.php @@ -37,12 +37,8 @@ public function validate(mixed $input): bool 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; } diff --git a/library/Rules/NotOptional.php b/library/Rules/NotOptional.php index 63395f058..eda38332e 100644 --- a/library/Rules/NotOptional.php +++ b/library/Rules/NotOptional.php @@ -33,12 +33,8 @@ public function validate(mixed $input): bool 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; } diff --git a/library/Rules/Nullable.php b/library/Rules/Nullable.php index e845ab946..b13568e79 100644 --- a/library/Rules/Nullable.php +++ b/library/Rules/Nullable.php @@ -52,12 +52,8 @@ public function validate(mixed $input): bool 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; } diff --git a/library/Rules/Optional.php b/library/Rules/Optional.php index 7cb089c3c..ed133b90b 100644 --- a/library/Rules/Optional.php +++ b/library/Rules/Optional.php @@ -55,12 +55,8 @@ public function validate(mixed $input): bool 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; } diff --git a/library/Rules/Phone.php b/library/Rules/Phone.php index 4a9092a69..9d616097a 100644 --- a/library/Rules/Phone.php +++ b/library/Rules/Phone.php @@ -70,11 +70,6 @@ public function validate(mixed $input): bool } } - public function getTemplate(mixed $input): string - { - return $this->template ?? $this->countryName ? self::TEMPLATE_FOR_COUNTRY : self::TEMPLATE_INTERNATIONAL; - } - /** * @return array */ @@ -82,4 +77,9 @@ public function getParams(): array { return ['countryName' => $this->countryName]; } + + protected function getStandardTemplate(mixed $input): string + { + return $this->countryName ? self::TEMPLATE_FOR_COUNTRY : self::TEMPLATE_INTERNATIONAL; + } } diff --git a/library/Rules/Size.php b/library/Rules/Size.php index d96533c7f..eb656fffa 100644 --- a/library/Rules/Size.php +++ b/library/Rules/Size.php @@ -76,12 +76,19 @@ public function validate(mixed $input): bool return false; } - public function getTemplate(mixed $input): string + /** + * @return array + */ + public function getParams(): array { - if ($this->template !== null) { - return $this->template; - } + return [ + 'minSize' => $this->minSize, + 'maxSize' => $this->maxSize, + ]; + } + protected function getStandardTemplate(mixed $input): string + { if (!$this->minValue) { return self::TEMPLATE_GREATER; } @@ -93,17 +100,6 @@ public function getTemplate(mixed $input): string return self::TEMPLATE_BOTH; } - /** - * @return array - */ - public function getParams(): array - { - return [ - 'minSize' => $this->minSize, - 'maxSize' => $this->maxSize, - ]; - } - /** * @todo Move it to a trait */ diff --git a/library/Rules/Sorted.php b/library/Rules/Sorted.php index 2ebd2ade6..055ed8feb 100644 --- a/library/Rules/Sorted.php +++ b/library/Rules/Sorted.php @@ -64,12 +64,8 @@ public function validate(mixed $input): bool 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; } diff --git a/library/Rules/Uuid.php b/library/Rules/Uuid.php index 74e623283..16a3a0bb3 100644 --- a/library/Rules/Uuid.php +++ b/library/Rules/Uuid.php @@ -49,11 +49,6 @@ public function validate(mixed $input): bool 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 */ @@ -62,6 +57,11 @@ public function getParams(): array 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; diff --git a/library/Rules/VideoUrl.php b/library/Rules/VideoUrl.php index c3db3aebc..562be963f 100644 --- a/library/Rules/VideoUrl.php +++ b/library/Rules/VideoUrl.php @@ -69,11 +69,6 @@ public function validate(mixed $input): bool return false; } - public function getTemplate(mixed $input): string - { - return $this->template ?? ($this->service ? self::TEMPLATE_SERVICE : self::TEMPLATE_STANDARD); - } - /** * @return array */ @@ -82,6 +77,11 @@ public function getParams(): array 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)]);