| 1 |
<?php |
| 2 |
|
| 3 |
namespace ipl\Validator; |
| 4 |
|
| 5 |
/** |
| 6 |
* Validate if specific single or multiple values exist in an array |
| 7 |
*/ |
| 8 |
class InArrayValidator extends BaseValidator |
| 9 |
{ |
| 10 |
/** @var ?mixed[] The array */ |
| 11 |
protected ?array $haystack = null; |
| 12 |
|
| 13 |
/** @var bool Whether the types of the needle in the haystack should also match */ |
| 14 |
protected bool $strict = false; |
| 15 |
|
| 16 |
/** |
| 17 |
* Create a new InArrayValidator |
| 18 |
* |
| 19 |
* **Optional options:** |
| 20 |
* |
| 21 |
* * `haystack`: (`array`) The array |
| 22 |
* * `strict`: (`bool`) Whether the types of the needle in the haystack should also match, default `false` |
| 23 |
* |
| 24 |
* @param array{haystack?: mixed[], strict?: bool} $options |
| 25 |
*/ |
| 26 |
public function __construct(array $options = []) |
| 27 |
{ |
| 28 |
if (isset($options['haystack'])) { |
| 29 |
$this->setHaystack($options['haystack']); |
| 30 |
} |
| 31 |
|
| 32 |
$this->setStrict($options['strict'] ?? false); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Get the haystack |
| 37 |
* |
| 38 |
* @return mixed[] |
| 39 |
*/ |
| 40 |
public function getHaystack(): array |
| 41 |
{ |
| 42 |
return $this->haystack ?? []; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Set the haystack |
| 47 |
* |
| 48 |
* @param mixed[] $haystack |
| 49 |
* |
| 50 |
* @return $this |
| 51 |
*/ |
| 52 |
public function setHaystack(array $haystack): static |
| 53 |
{ |
| 54 |
$this->haystack = $haystack; |
| 55 |
|
| 56 |
return $this; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Get whether the types of the needle in the haystack should also match |
| 61 |
* |
| 62 |
* @return bool |
| 63 |
*/ |
| 64 |
public function isStrict(): bool |
| 65 |
{ |
| 66 |
return $this->strict; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Set whether the types of the needle in the haystack should also match |
| 71 |
* |
| 72 |
* @param bool $strict |
| 73 |
* |
| 74 |
* @return $this |
| 75 |
*/ |
| 76 |
public function setStrict(bool $strict = true): static |
| 77 |
{ |
| 78 |
$this->strict = $strict; |
| 79 |
|
| 80 |
return $this; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Check whether the value exists in the haystack |
| 85 |
* |
| 86 |
* @param mixed $value |
| 87 |
* |
| 88 |
* @return bool |
| 89 |
*/ |
| 90 |
public function isValid($value): bool |
| 91 |
{ |
| 92 |
// Reset messages from a previous isValid() call. |
| 93 |
$this->clearMessages(); |
| 94 |
|
| 95 |
$notInArray = $this->findInvalid((array) $value); |
| 96 |
|
| 97 |
if (empty($notInArray)) { |
| 98 |
return true; |
| 99 |
} |
| 100 |
|
| 101 |
$this->addMessage(sprintf( |
| 102 |
$this->translatePlural( |
| 103 |
"%s was not found in the haystack", |
| 104 |
"%s were not found in the haystack", |
| 105 |
count($notInArray) |
| 106 |
), |
| 107 |
implode(', ', $notInArray) |
| 108 |
)); |
| 109 |
|
| 110 |
return false; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Get the values from the specified array that are not present in the haystack |
| 115 |
* |
| 116 |
* @param mixed[] $values |
| 117 |
* |
| 118 |
* @return mixed[] Values not found in the haystack |
| 119 |
*/ |
| 120 |
protected function findInvalid(array $values = []): array |
| 121 |
{ |
| 122 |
$notInArray = []; |
| 123 |
foreach ($values as $val) { |
| 124 |
if (! in_array($val, $this->getHaystack(), $this->isStrict())) { |
| 125 |
$notInArray[] = $val; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
return $notInArray; |
| 130 |
} |
| 131 |
} |