Skip to content

Commit

Permalink
Remove duplicated code
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
henriquemoody committed Feb 9, 2024
1 parent 02b70bf commit c6677fd
Show file tree
Hide file tree
Showing 20 changed files with 102 additions and 145 deletions.
10 changes: 5 additions & 5 deletions library/Rules/AbstractFilterRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, mixed>
*/
Expand All @@ -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);
Expand Down
6 changes: 1 addition & 5 deletions library/Rules/AbstractRelated.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
7 changes: 6 additions & 1 deletion library/Rules/AbstractRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions library/Rules/AllOf.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -66,4 +61,9 @@ public function validate(mixed $input): bool

return true;
}

protected function getStandardTemplate(mixed $input): string
{
return self::TEMPLATE_SOME;
}
}
20 changes: 8 additions & 12 deletions library/Rules/CreditCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>
*/
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<string, string>
*/
public function getParams(): array
{
return ['brand' => $this->brand];
}
}
10 changes: 5 additions & 5 deletions library/Rules/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,16 @@ 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<string, mixed>
*/
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;
}
}
10 changes: 5 additions & 5 deletions library/Rules/Ip.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, mixed>
*/
Expand All @@ -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) {
Expand Down
22 changes: 9 additions & 13 deletions library/Rules/KeySet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, mixed>
*/
Expand All @@ -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) {
Expand Down
34 changes: 15 additions & 19 deletions library/Rules/KeyValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, mixed>
*/
Expand All @@ -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])) {
Expand Down
26 changes: 11 additions & 15 deletions library/Rules/Length.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<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;
}
Expand All @@ -103,17 +110,6 @@ public function getTemplate(mixed $input): string
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)) {
Expand Down
6 changes: 1 addition & 5 deletions library/Rules/NotBlank.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 1 addition & 5 deletions library/Rules/NotEmpty.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 1 addition & 5 deletions library/Rules/NotOptional.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 1 addition & 5 deletions library/Rules/Nullable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 1 addition & 5 deletions library/Rules/Optional.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
10 changes: 5 additions & 5 deletions library/Rules/Phone.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,16 @@ 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<string, mixed>
*/
public function getParams(): array
{
return ['countryName' => $this->countryName];
}

protected function getStandardTemplate(mixed $input): string
{
return $this->countryName ? self::TEMPLATE_FOR_COUNTRY : self::TEMPLATE_INTERNATIONAL;
}
}
Loading

0 comments on commit c6677fd

Please sign in to comment.