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    private function ensureValid()
28    {
29        if (!$this->testValid()) {
30            throw new InvalidArgumentException("The provided data is invalid according to the schema.");
31        }
32    }
33
34    public function getCombinations(): array
35    {
36        return $this->combinations;
37    }
38
39    public function toArray(): array
40    {
41        $result = [];
42        foreach ($this->order as $index => $id) {
43            $orderKey = (string)($index + 1);
44            $result[$orderKey] = [$id => $this->combinations[$id]];
45        }
46        return $result;
47    }
48
49    #[\Override]
50    public function jsonSerialize(): mixed
51    {
52        return $this->toArray();
53    }
54}