Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
61.54% |
8 / 13 |
|
25.00% |
1 / 4 |
CRAP | |
0.00% |
0 / 1 |
OfficeList | |
61.54% |
8 / 13 |
|
25.00% |
1 / 4 |
11.64 | |
0.00% |
0 / 1 |
__construct | |
57.14% |
4 / 7 |
|
0.00% |
0 / 1 |
5.26 | |||
ensureValid | |
50.00% |
1 / 2 |
|
0.00% |
0 / 1 |
2.50 | |||
toArray | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
jsonSerialize | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace BO\Zmscitizenapi\Models\Collections; |
6 | |
7 | use BO\Zmscitizenapi\Models\Office; |
8 | use BO\Zmsentities\Schema\Entity; |
9 | use InvalidArgumentException; |
10 | use JsonSerializable; |
11 | |
12 | class OfficeList extends Entity implements JsonSerializable |
13 | { |
14 | public static $schema = "citizenapi/collections/officeList.json"; |
15 | /** @var Office[] */ |
16 | public array $offices = []; |
17 | public function __construct(array $offices = []) |
18 | { |
19 | foreach ($offices as $office) { |
20 | try { |
21 | if (!$office instanceof Office) { |
22 | throw new InvalidArgumentException("Element is not an instance of Office."); |
23 | } |
24 | $this->offices[] = $office; |
25 | } catch (\Exception $e) { |
26 | error_log("Invalid Office encountered: " . $e->getMessage()); |
27 | //Gracefully handle |
28 | } |
29 | } |
30 | |
31 | $this->ensureValid(); |
32 | } |
33 | |
34 | private function ensureValid() |
35 | { |
36 | if (!$this->testValid()) { |
37 | throw new InvalidArgumentException("The provided data is invalid according to the schema."); |
38 | } |
39 | } |
40 | |
41 | public function toArray(): array |
42 | { |
43 | return [ |
44 | 'offices' => array_map(fn(Office $office) => $office->toArray(), $this->offices), |
45 | ]; |
46 | } |
47 | |
48 | public function jsonSerialize(): mixed |
49 | { |
50 | return $this->toArray(); |
51 | } |
52 | } |