Total Complexity | 9 |
Total Lines | 70 |
Duplicated Lines | 0 % |
Coverage | 71.43% |
Changes | 0 |
1 | <?php |
||
9 | final class StrictMatchingSegment implements ISegment |
||
10 | { |
||
11 | public const NAME = 'strict_matching_segment'; |
||
12 | private string $id; |
||
13 | /** |
||
14 | * @var array<string, mixed> |
||
15 | */ |
||
16 | private array $criteria; |
||
17 | |||
18 | /** |
||
19 | * Segment constructor. |
||
20 | * |
||
21 | * @param string $id |
||
22 | * @param array<string, mixed> $criteria |
||
23 | */ |
||
24 | 8 | public function __construct(string $id, array $criteria) |
|
25 | { |
||
26 | 8 | $this->id = $id; |
|
27 | 8 | $this->criteria = $criteria; |
|
28 | 8 | } |
|
29 | |||
30 | public function id(): string |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * @return array<string, mixed> |
||
37 | */ |
||
38 | public function criteria(): array |
||
39 | { |
||
40 | return $this->criteria; |
||
41 | } |
||
42 | |||
43 | 5 | public function match(array $payload): bool |
|
44 | { |
||
45 | 5 | $match = false; |
|
46 | |||
47 | /** |
||
48 | * @var mixed $value |
||
49 | */ |
||
50 | 5 | foreach ($this->criteria as $key => $value) { |
|
51 | 4 | if (array_key_exists($key, $payload) && $value === $payload[$key]) { |
|
52 | 3 | $match = true; |
|
53 | 3 | continue; |
|
54 | } |
||
55 | |||
56 | 1 | return false; |
|
57 | } |
||
58 | |||
59 | 4 | return $match; |
|
60 | } |
||
61 | |||
62 | /** |
||
63 | * @return array<string, string|array> |
||
64 | */ |
||
65 | 1 | public function toArray(): array |
|
66 | { |
||
67 | return [ |
||
68 | 1 | 'id' => $this->id, |
|
69 | 1 | 'criteria' => $this->criteria, |
|
70 | ]; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @return array<string, string|array> |
||
75 | */ |
||
76 | public function jsonSerialize(): array |
||
79 | } |
||
80 | } |
||
81 |