Total Complexity | 7 |
Total Lines | 59 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | <?php |
||
11 | class ContainsAtLeastOneConstraint extends Constraint |
||
12 | { |
||
13 | /** |
||
14 | * @var array<string> |
||
15 | */ |
||
16 | private $members; |
||
17 | |||
18 | /** |
||
19 | * Class constructor. |
||
20 | * |
||
21 | * @param array<string> $members |
||
22 | */ |
||
23 | 59 | public function __construct(array $members) |
|
24 | { |
||
25 | 59 | $this->members = $members; |
|
26 | 59 | } |
|
27 | |||
28 | /** |
||
29 | * Returns a string representation of the constraint. |
||
30 | */ |
||
31 | 9 | public function toString(): string |
|
32 | { |
||
33 | 9 | return \sprintf( |
|
34 | 9 | 'contains at least one element of "%s"', |
|
35 | 9 | \implode(', ', $this->members) |
|
36 | ); |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * Evaluates the constraint for parameter $other. Returns true if the |
||
41 | * constraint is met, false otherwise. |
||
42 | * |
||
43 | * @param mixed $other value or object to evaluate |
||
44 | */ |
||
45 | 59 | protected function matches($other): bool |
|
46 | { |
||
47 | 59 | if (!is_array($other)) { |
|
48 | 1 | return false; |
|
49 | } |
||
50 | |||
51 | 58 | foreach ($this->members as $member) { |
|
52 | 58 | if (array_key_exists($member, $other)) { |
|
53 | 45 | return true; |
|
54 | } |
||
55 | } |
||
56 | |||
57 | 14 | return false; |
|
58 | } |
||
59 | |||
60 | /** |
||
61 | * Evaluates the constraint for parameter $other. Returns true if the |
||
62 | * constraint is met, false otherwise. |
||
63 | * |
||
64 | * @param mixed $other value or object to evaluate |
||
65 | * @return boolean |
||
66 | */ |
||
67 | 13 | public function check($other): bool |
|
70 | } |
||
71 | } |
||
72 |