Skip to content

Commit

Permalink
Update the validation engine of the "Extension" rule
Browse files Browse the repository at this point in the history
Signed-off-by: Henrique Moody <[email protected]>
  • Loading branch information
henriquemoody committed Feb 23, 2024
1 parent e520b2d commit afb27d6
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions library/Rules/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Respect\Validation\Rules;

use Respect\Validation\Message\Template;
use Respect\Validation\Result;
use SplFileInfo;

use function is_string;
Expand All @@ -21,31 +22,26 @@
'{{name}} must have {{extension}} extension',
'{{name}} must not have {{extension}} extension',
)]
final class Extension extends AbstractRule
final class Extension extends Standard
{
public function __construct(
private readonly string $extension
) {
}

public function validate(mixed $input): bool
public function evaluate(mixed $input): Result
{
$parameters = ['extension' => $this->extension];
if ($input instanceof SplFileInfo) {
return $this->extension === $input->getExtension();
return (new Result($this->extension === $input->getExtension(), $input, $this))
->withParameters($parameters);
}

if (!is_string($input)) {
return false;
return Result::failed($input, $this)->withParameters($parameters);
}

return $this->extension === pathinfo($input, PATHINFO_EXTENSION);
}

/**
* @return array<string, string>
*/
public function getParams(): array
{
return ['extension' => $this->extension];
return (new Result($this->extension === pathinfo($input, PATHINFO_EXTENSION), $input, $this))
->withParameters($parameters);
}
}

0 comments on commit afb27d6

Please sign in to comment.