Passed
Push — 1.0.x ( 16fd64...da3a7c )
by Koldo
02:35
created

StrictMatchingSegment::id()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Pheature\Model\Toggle;
6
7
use Pheature\Core\Toggle\Read\Segment as ISegment;
8
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
31
    {
32
        return $this->id;
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
77
    {
78
        return $this->toArray();
79
    }
80
}
81