Passed
Push — 1.0.x ( 54a243...6cfe9a )
by Koldo
02:32
created

Segment::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 1
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 Segment implements ISegment
10
{
11
    private string $id;
12
    /**
13
     * @var array<string, mixed>
14
     */
15
    private array $criteria;
16
17
    /**
18
     * Segment constructor.
19
     *
20
     * @param string               $id
21
     * @param array<string, mixed> $criteria
22
     */
23 7
    public function __construct(string $id, array $criteria)
24
    {
25 7
        $this->id = $id;
26 7
        $this->criteria = $criteria;
27 7
    }
28
29
    public function id(): string
30
    {
31
        return $this->id;
32
    }
33
34
    /**
35
     * @return array<string, mixed>
36
     */
37
    public function criteria(): array
38
    {
39
        return $this->criteria;
40
    }
41
42 5
    public function match(array $payload): bool
43
    {
44 5
        $match = false;
45
46
        /**
47
 * @var mixed $value
48
*/
49 5
        foreach ($this->criteria as $key => $value) {
50 4
            if (array_key_exists($key, $payload) && $value === $payload[$key]) {
51 3
                $match = true;
52 3
                continue;
53
            }
54
55 1
            return false;
56
        }
57
58 4
        return $match;
59
    }
60
61
    /**
62
     * @return array<string, string|array>
63
     */
64 1
    public function toArray(): array
65
    {
66
        return [
67 1
            'id' => $this->id,
68 1
            'criteria' => $this->criteria,
69
        ];
70
    }
71
72
    /**
73
     * @return array<string, string|array>
74
     */
75
    public function jsonSerialize(): array
76
    {
77
        return $this->toArray();
78
    }
79
}
80