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
15    /** @var array<string, array<int>> */
16    private array $combinations = [];
17
18    /** @var array<string> */
19    private array $order = [];
20
21    /**
22     * Constructor.
23     *
24     * @param array $combinations An associative array of combinations (serviceId => providerIds).
25     */
26    public function __construct(array $combinations = [])
27    {
28        $this->order = array_keys($combinations);
29        foreach ($combinations as $id => $providerIds) {
30            $this->combinations[(string)$id] = is_array($providerIds) ? array_map('intval', $providerIds) : [];
31        }
32
33        $this->ensureValid();
34    }
35
36    private function ensureValid()
37    {
38        if (!$this->testValid()) {
39            throw new InvalidArgumentException("The provided data is invalid according to the schema.");
40        }
41    }
42
43    /**
44     * Get the combinations array in original format (for internal use).
45     *
46     * @return array<string, array<int>> The combinations as an associative array.
47     */
48    public function getCombinations(): array
49    {
50        return $this->combinations;
51    }
52
53    /**
54     * Converts the model data back into an array for serialization.
55     * Returns the data in numbered format:
56     * {
57     *   "1": { "serviceId": [providers] },
58     *   "2": { "serviceId": [providers] },
59     *   ...
60     * }
61     *
62     * @return array The combinations with order preserved using numbered keys.
63     */
64    public function toArray(): array
65    {
66        $result = [];
67        foreach ($this->order as $index => $id) {
68            $orderKey = (string)($index + 1);
69            $result[$orderKey] = [$id => $this->combinations[$id]];
70        }
71        return $result;
72    }
73
74    public function jsonSerialize(): mixed
75    {
76        return $this->toArray();
77    }
78}