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