diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-13 11:30:08 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-13 11:30:08 +0000 |
commit | 4ce65d59ca91871cfd126497158200a818720bce (patch) | |
tree | e277def01fc7eba7dbc21c4a4ae5576e8aa2cf1f /vendor/ipl/validator/src/InArrayValidator.php | |
parent | Initial commit. (diff) | |
download | icinga-php-library-upstream/0.13.1.tar.xz icinga-php-library-upstream/0.13.1.zip |
Adding upstream version 0.13.1.upstream/0.13.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/ipl/validator/src/InArrayValidator.php')
-rw-r--r-- | vendor/ipl/validator/src/InArrayValidator.php | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/vendor/ipl/validator/src/InArrayValidator.php b/vendor/ipl/validator/src/InArrayValidator.php new file mode 100644 index 0000000..f8c18ef --- /dev/null +++ b/vendor/ipl/validator/src/InArrayValidator.php @@ -0,0 +1,128 @@ +<?php + +namespace ipl\Validator; + +use ipl\I18n\Translation; + +/** + * Validate if specific single or multiple values exist in an array + */ +class InArrayValidator extends BaseValidator +{ + use Translation; + + /** @var array The array */ + protected $haystack; + + /** @var bool Whether the types of the needle in the haystack should also match */ + protected $strict = false; + + /** + * Create a new InArray validator + * + * **Optional options:** + * + * * `haystack`: (`array`) The array + * * `strict`: (`bool`) Whether the types of the needle in the haystack should also match, default `false` + * + * @param array $options + */ + public function __construct(array $options = []) + { + if (isset($options['haystack'])) { + $this->setHaystack($options['haystack']); + } + + $this->setStrict($options['strict'] ?? false); + } + + /** + * Get the haystack + * + * @return array + */ + public function getHaystack(): array + { + return $this->haystack ?? []; + } + + /** + * Set the haystack + * + * @param array $haystack + * + * @return $this + */ + public function setHaystack(array $haystack): self + { + $this->haystack = $haystack; + + return $this; + } + + /** + * Get whether the types of the needle in the haystack should also match + * + * @return bool + */ + public function isStrict(): bool + { + return $this->strict; + } + + /** + * Set whether the types of the needle in the haystack should also match + * + * @param bool $strict + * + * @return $this + */ + public function setStrict(bool $strict = true): self + { + $this->strict = $strict; + + return $this; + } + + public function isValid($value) + { + // Multiple isValid() calls must not stack validation messages + $this->clearMessages(); + + $notInArray = $this->findInvalid((array) $value); + + if (empty($notInArray)) { + return true; + } + + $this->addMessage(sprintf( + $this->translatePlural( + "%s was not found in the haystack", + "%s were not found in the haystack", + count($notInArray) + ), + implode(', ', $notInArray) + )); + + return false; + } + + /** + * Get the values from the specified array that are not present in the haystack + * + * @param array $values + * + * @return array Values not found in the haystack + */ + protected function findInvalid(array $values = []): array + { + $notInArray = []; + foreach ($values as $val) { + if (! in_array($val, $this->getHaystack(), $this->isStrict())) { + $notInArray[] = $val; + } + } + + return $notInArray; + } +} |