Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
25 / 25 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| Contains | |
100.00% |
25 / 25 |
|
100.00% |
2 / 2 |
14 | |
100.00% |
1 / 1 |
| evaluate | |
100.00% |
22 / 22 |
|
100.00% |
1 / 1 |
12 | |||
| generateDefaultMessage | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Pop PHP Framework (http://www.popphp.org/) |
| 4 | * |
| 5 | * @link https://github.com/popphp/popphp-framework |
| 6 | * @author Nick Sagona, III <dev@noladev.com> |
| 7 | * @copyright Copyright (c) 2009-2025 NOLA Interactive, LLC. |
| 8 | * @license http://www.popphp.org/license New BSD License |
| 9 | */ |
| 10 | |
| 11 | /** |
| 12 | * @namespace |
| 13 | */ |
| 14 | namespace Pop\Validator; |
| 15 | |
| 16 | /** |
| 17 | * Contains validator class |
| 18 | * |
| 19 | * @category Pop |
| 20 | * @package Pop\Validator |
| 21 | * @author Nick Sagona, III <dev@noladev.com> |
| 22 | * @copyright Copyright (c) 2009-2025 NOLA Interactive, LLC. |
| 23 | * @license http://www.popphp.org/license New BSD License |
| 24 | * @version 4.5.0 |
| 25 | */ |
| 26 | class Contains extends AbstractValidator |
| 27 | { |
| 28 | |
| 29 | /** |
| 30 | * Method to evaluate the validator |
| 31 | * |
| 32 | * @param mixed $input |
| 33 | * @return bool |
| 34 | */ |
| 35 | public function evaluate(mixed $input = null): bool |
| 36 | { |
| 37 | // Set the input, if passed |
| 38 | if ($input !== null) { |
| 39 | $this->input = $input; |
| 40 | } |
| 41 | |
| 42 | // Set the default message |
| 43 | if (!$this->hasMessage()) { |
| 44 | $this->generateDefaultMessage(); |
| 45 | } |
| 46 | |
| 47 | $result = false; |
| 48 | $needle = $this->value; |
| 49 | $haystack = $this->input; |
| 50 | |
| 51 | if (is_string($needle) && is_string($haystack)) { |
| 52 | $result = (str_contains($haystack, $needle)); |
| 53 | } else if (!is_array($needle) && is_array($haystack)) { |
| 54 | $result = in_array($needle, $haystack); |
| 55 | } else if (is_array($needle)) { |
| 56 | $result = true; |
| 57 | foreach ($needle as $n) { |
| 58 | if (is_array($haystack)) { |
| 59 | if (!in_array($n, $haystack)) { |
| 60 | $result = false; |
| 61 | break; |
| 62 | } |
| 63 | } else if (!str_contains((string)$haystack, $n)) { |
| 64 | $result = false; |
| 65 | break; |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | return $result; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Generate default message |
| 75 | |
| 76 | * @param mixed $name |
| 77 | * @param mixed $value |
| 78 | * @return string |
| 79 | */ |
| 80 | public function generateDefaultMessage(mixed $name = null, mixed $value = null): string |
| 81 | { |
| 82 | $this->message = "The " . (($name !== null) ? "'" . $name . "'" : "value") . |
| 83 | " must be contained in the input."; |
| 84 | |
| 85 | return $this->message; |
| 86 | } |
| 87 | |
| 88 | } |