Passed
Push — 1.0.x ( 582123...077e14 )
by Koldo
02:53
created

StrictMatchingSegment::id()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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;
8
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)
25
    {
26 15
        $this->id = $id;
27 15
        $this->criteria = $criteria;
28 15
    }
29
30 7
    public function id(): string
31
    {
32 7
        return $this->id;
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
83
    {
84 7
        return $this->toArray();
85
    }
86
}
87