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

IdentitySegment::jsonSerialize()   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 1
Bugs 0 Features 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 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;
8
9
final class IdentitySegment implements Segment
10
{
11
    public const NAME = 'identity_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 4
    public function id(): string
31
    {
32 4
        return $this->id;
33
    }
34
35 4
    public function type(): string
36
    {
37 4
        return self::NAME;
38
    }
39
40
    /**
41
     * @return array<string, mixed>
42
     */
43 4
    public function criteria(): array
44
    {
45 4
        return $this->criteria;
46
    }
47
48 6
    public function match(array $payload): bool
49
    {
50 6
        if (false === array_key_exists('identity_id', $payload)) {
51 1
            return false;
52
        }
53
54
        /** @var string $identityId */
55 5
        $identityId = $payload['identity_id'];
56
57 5
        return in_array($identityId, $this->criteria, true);
58
    }
59
60
    /**
61
     * @return array<string, string|array>
62
     */
63 5
    public function toArray(): array
64
    {
65
        return [
66 5
            'id' => $this->id,
67 5
            'type' => self::NAME,
68 5
            'criteria' => $this->criteria,
69
        ];
70
    }
71
72
    /**
73
     * @return array<string, string|array>
74
     */
75 4
    public function jsonSerialize(): array
76
    {
77 4
        return $this->toArray();
78
    }
79
}
80