Lines 92.30% 12 / 13
Methods 80.00% 4 / 5
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 __construct 100.00% 4 / 4 100.00% 1 / 1 3
 ensureValid 50.00% 1 / 2 0.00% 0 / 1 2.50
 getCombinations 100.00% 1 / 1 100.00% 1 / 1 1
 toArray 100.00% 5 / 5 100.00% 1 / 1 2
 jsonSerialize 100.00% 1 / 1 100.00% 1 / 1 1
11class Combinable extends Entity implements JsonSerializable
12{
13    public static $schema = 'citizenapi/combinable.json';
14    private array $combinations = [];
15    private array $order = [];
16
17    public function __construct(array $combinations = [])
18    {
19        $this->order = array_keys($combinations);
20        foreach ($combinations as $id => $providerIds) {
21            $this->combinations[(string)$id] = is_array($providerIds) ? array_map('intval', $providerIds) : [];
22        }
23
24        $this->ensureValid();
25    }
26
27    /**
28     * @return void
29     */
30    private function ensureValid()
31    {
32        if (!$this->testValid()) {
33            throw new InvalidArgumentException("The provided data is invalid according to the schema.");
34        }
35    }
36
37    public function getCombinations(): array
38    {
39        return $this->combinations;
40    }
41
42    public function toArray(): array
43    {
44        $result = [];
45        foreach ($this->order as $index => $id) {
46            $orderKey = (string)($index + 1);
47            $result[$orderKey] = [$id => $this->combinations[$id]];
48        }
49        return $result;
50    }
51
52    #[\Override]
53    public function jsonSerialize(): mixed
54    {
55        return $this->toArray();
56    }
57}