Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.31% covered (success)
92.31%
12 / 13
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Combinable
92.31% covered (success)
92.31%
12 / 13
80.00% covered (warning)
80.00%
4 / 5
9.04
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 ensureValid
50.00% covered (danger)
50.00%
1 / 2
0.00% covered (danger)
0.00%
0 / 1
2.50
 getCombinations
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toArray
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 jsonSerialize
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace BO\Zmscitizenapi\Models;
6
7use BO\Zmsentities\Schema\Entity;
8use InvalidArgumentException;
9use JsonSerializable;
10
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}