Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
16 / 16 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
Contains | |
100.00% |
16 / 16 |
|
100.00% |
3 / 3 |
5 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
validate | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 | |||
getInverseError | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace Verja\Validator; |
4 | |
5 | use Verja\Error; |
6 | use Verja\Validator; |
7 | |
8 | class Contains extends Validator |
9 | { |
10 | /** @var string */ |
11 | protected $subString; |
12 | |
13 | /** |
14 | * Contains constructor. |
15 | * |
16 | * @param string $subString |
17 | */ |
18 | public function __construct(string $subString) |
19 | { |
20 | $this->subString = $subString; |
21 | } |
22 | |
23 | /** {@inheritdoc} */ |
24 | public function validate($value, array $context = []): bool |
25 | { |
26 | if (!is_string($value) || strpos($value, $this->subString) === false) { |
27 | $this->error = new Error( |
28 | 'NOT_CONTAINS', |
29 | $value, |
30 | sprintf('value should contain "%s"', $this->subString), |
31 | [ 'subString' => $this->subString ] |
32 | ); |
33 | return false; |
34 | } |
35 | |
36 | return true; |
37 | } |
38 | |
39 | /** {@inheritdoc} */ |
40 | public function getInverseError($value) |
41 | { |
42 | return new Error( |
43 | 'CONTAINS', |
44 | $value, |
45 | sprintf('value should not contain "%s"', $this->subString), |
46 | [ 'subString' => $this->subString ] |
47 | ); |
48 | } |
49 | } |