| 1 | <?php |
||
| 18 | class ContainsRule extends AbstractRule |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @var string needle for search inside input data (haystack) |
||
| 22 | */ |
||
| 23 | protected $containsValue; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Setup validation rule |
||
| 27 | * |
||
| 28 | * @param mixed $containsValue |
||
| 29 | */ |
||
| 30 | 15 | public function __construct($containsValue) |
|
| 34 | |||
| 35 | /** |
||
| 36 | * Check input data |
||
| 37 | * |
||
| 38 | * @param string|array $input |
||
| 39 | * |
||
| 40 | * @return bool |
||
| 41 | */ |
||
| 42 | 9 | public function validate($input): bool |
|
| 43 | { |
||
| 44 | // for array |
||
| 45 | 9 | if (is_array($input)) { |
|
| 46 | 4 | return in_array($this->containsValue, $input, false); |
|
| 47 | } |
||
| 48 | // for string |
||
| 49 | 5 | if (is_string($input)) { |
|
| 50 | 5 | return false !== mb_stripos($input, $this->containsValue, 0, mb_detect_encoding($input)); |
|
| 51 | } |
||
| 52 | // can't compare |
||
| 53 | return false; |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Get error template |
||
| 58 | * |
||
| 59 | * @return string |
||
| 60 | */ |
||
| 61 | 4 | public function getDescription() : string |
|
| 65 | } |
||
| 66 |