Total Complexity | 10 |
Total Lines | 76 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | <?php |
||
9 | final class StrictMatchingSegment implements Segment |
||
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 | 15 | public function __construct(string $id, array $criteria) |
|
28 | 15 | } |
|
29 | |||
30 | 7 | public function id(): string |
|
33 | } |
||
34 | |||
35 | 7 | public function type(): string |
|
36 | { |
||
37 | 7 | return self::NAME; |
|
38 | } |
||
39 | |||
40 | /** |
||
41 | * @return array<string, mixed> |
||
42 | */ |
||
43 | 7 | public function criteria(): array |
|
44 | { |
||
45 | 7 | return $this->criteria; |
|
46 | } |
||
47 | |||
48 | 12 | public function match(array $payload): bool |
|
49 | { |
||
50 | 12 | $match = false; |
|
51 | |||
52 | /** |
||
53 | * @var mixed $value |
||
54 | */ |
||
55 | 12 | foreach ($this->criteria as $key => $value) { |
|
56 | 10 | if (array_key_exists($key, $payload) && $value === $payload[$key]) { |
|
57 | 6 | $match = true; |
|
58 | 6 | continue; |
|
59 | } |
||
60 | |||
61 | 4 | return false; |
|
62 | } |
||
63 | |||
64 | 8 | return $match; |
|
65 | } |
||
66 | |||
67 | /** |
||
68 | * @return array<string, string|array> |
||
69 | */ |
||
70 | 8 | public function toArray(): array |
|
71 | { |
||
72 | return [ |
||
73 | 8 | 'id' => $this->id, |
|
74 | 8 | 'type' => self::NAME, |
|
75 | 8 | 'criteria' => $this->criteria, |
|
76 | ]; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * @return array<string, string|array> |
||
81 | */ |
||
82 | 7 | public function jsonSerialize(): array |
|
85 | } |
||
86 | } |
||
87 |