|
| 1 | +# Refactoring Legacy Validators |
| 2 | + |
| 3 | +This document is intended to show an example of refactoring custom validators to remove runtime mutation of options and move option determination to the constructor of your validator. |
| 4 | + |
| 5 | +## The Old Validator |
| 6 | + |
| 7 | +The following custom validator is our starting point which relies on now removed methods and behaviour from the `AbstractValidator` in the 2.x series of releases. |
| 8 | + |
| 9 | +```php |
| 10 | +namespace My; |
| 11 | + |
| 12 | +use Laminas\Validator\AbstractValidator; |
| 13 | + |
| 14 | +class MuppetNameValidator extends AbstractValidator { |
| 15 | + public const KNOWN_MUPPETS = [ |
| 16 | + 'Kermit', |
| 17 | + 'Miss Piggy', |
| 18 | + 'Fozzie Bear', |
| 19 | + 'Gonzo the Great', |
| 20 | + 'Scooter', |
| 21 | + 'Animal', |
| 22 | + 'Beaker', |
| 23 | + ]; |
| 24 | + |
| 25 | + public const ERR_NOT_STRING = 'notString'; |
| 26 | + public const ERR_NOT_ALLOWED = 'notAllowed'; |
| 27 | + |
| 28 | + protected array $messageTemplates = [ |
| 29 | + self::ERR_NOT_STRING => 'Please provide a string value', |
| 30 | + self::ERR_NOT_ALLOWED => '"%value%" is not an allowed muppet name', |
| 31 | + ]; |
| 32 | + |
| 33 | + public function setAllowedMuppets(array $muppets): void { |
| 34 | + $this->options['allowed_muppets'] = []; |
| 35 | + foreach ($muppets as $muppet) { |
| 36 | + $this->addMuppet($muppet); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + public function addMuppet(string $muppet): void |
| 41 | + { |
| 42 | + $this->options['allowed_muppets'][] = $muppet; |
| 43 | + } |
| 44 | + |
| 45 | + public function setCaseSensitive(bool $caseSensitive): void |
| 46 | + { |
| 47 | + $this->options['case_sensitive'] = $caseSensitive; |
| 48 | + } |
| 49 | + |
| 50 | + public function isValid(mixed $value): bool { |
| 51 | + if (! is_string($value)) { |
| 52 | + $this->error(self::ERR_NOT_STRING); |
| 53 | + |
| 54 | + return false; |
| 55 | + } |
| 56 | + |
| 57 | + $list = $this->options['allowed_muppets']; |
| 58 | + if (! $this->options['case_sensitive']) { |
| 59 | + $list = array_map('strtolower', $list); |
| 60 | + $value = strtolower($value); |
| 61 | + } |
| 62 | + |
| 63 | + if (! in_array($value, $list, true)) { |
| 64 | + $this->error(self::ERR_NOT_ALLOWED); |
| 65 | + |
| 66 | + return false; |
| 67 | + } |
| 68 | + |
| 69 | + return true; |
| 70 | + } |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +Given an array of options such as `['allowed_muppets' => ['Miss Piggy'], 'caseSensitive' => false]`, previously, the `AbstractValidator` would have "magically" called the setter methods `setAllowedMuppets` and `setCaseSensitive`. The same would be true if you provided these options to the removed `AbstractValidator::setOptions()` method. |
| 75 | + |
| 76 | +Additionally, with the class above, there is nothing to stop you from creating the validator in an invalid state with: |
| 77 | + |
| 78 | +```php |
| 79 | +$validator = new MuppetNameValidator(); |
| 80 | +$validator->isValid('Kermit'); |
| 81 | +// false, because the list of allowed muppets has not been initialised |
| 82 | +``` |
| 83 | + |
| 84 | +## The Refactored Validator |
| 85 | + |
| 86 | +```php |
| 87 | +final readonly class MuppetNameValidator extends AbstractValidator { |
| 88 | + public const KNOWN_MUPPETS = [ |
| 89 | + 'Kermit', |
| 90 | + 'Miss Piggy', |
| 91 | + 'Fozzie Bear', |
| 92 | + 'Gonzo the Great', |
| 93 | + 'Scooter', |
| 94 | + 'Animal', |
| 95 | + 'Beaker', |
| 96 | + ]; |
| 97 | + |
| 98 | + public const ERR_NOT_STRING = 'notString'; |
| 99 | + public const ERR_NOT_ALLOWED = 'notAllowed'; |
| 100 | + |
| 101 | + protected array $messageTemplates = [ |
| 102 | + self::ERR_NOT_STRING => 'Please provide a string value', |
| 103 | + self::ERR_NOT_ALLOWED => '"%value%" is not an allowed muppet name', |
| 104 | + ]; |
| 105 | + |
| 106 | + private array $allowed; |
| 107 | + private bool $caseSensitive; |
| 108 | + |
| 109 | + /** |
| 110 | + * @param array{ |
| 111 | + * allowed_muppets: list<non-empty-string>, |
| 112 | + * case_sensitive: bool, |
| 113 | + * } $options |
| 114 | + */ |
| 115 | + public function __construct(array $options) |
| 116 | + { |
| 117 | + $this->allowed = $options['allowed_muppets'] ?? self::KNOWN_MUPPETS; |
| 118 | + $this->caseSensitive = $options['case_sensitive'] ?? true; |
| 119 | + |
| 120 | + // Pass options such as the translator, overridden error messages, etc |
| 121 | + // to the parent AbstractValidator |
| 122 | + parent::__construct($options); |
| 123 | + } |
| 124 | + |
| 125 | + public function isValid(mixed $value): bool { |
| 126 | + if (! is_string($value)) { |
| 127 | + $this->error(self::ERR_NOT_STRING); |
| 128 | + |
| 129 | + return false; |
| 130 | + } |
| 131 | + |
| 132 | + $list = $this->allowed; |
| 133 | + if (! $this->caseSensitive) { |
| 134 | + $list = array_map('strtolower', $list); |
| 135 | + $value = strtolower($value); |
| 136 | + } |
| 137 | + |
| 138 | + if (! in_array($value, $list, true)) { |
| 139 | + $this->error(self::ERR_NOT_ALLOWED); |
| 140 | + |
| 141 | + return false; |
| 142 | + } |
| 143 | + |
| 144 | + return true; |
| 145 | + } |
| 146 | +} |
| 147 | +``` |
| 148 | + |
| 149 | +With the refactored validator, our options are clearly and obviously declared as class properties, and cannot be changed once they have been set. |
| 150 | + |
| 151 | +There are fewer methods to test; In your test case you can easily set up data providers with varying options to thoroughly test that your validator behaves in the expected way. |
0 commit comments