Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
61.54% covered (warning)
61.54%
8 / 13
25.00% covered (danger)
25.00%
1 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
OfficeServiceRelationList
61.54% covered (warning)
61.54%
8 / 13
25.00% covered (danger)
25.00%
1 / 4
11.64
0.00% covered (danger)
0.00%
0 / 1
 __construct
57.14% covered (warning)
57.14%
4 / 7
0.00% covered (danger)
0.00%
0 / 1
5.26
 ensureValid
50.00% covered (danger)
50.00%
1 / 2
0.00% covered (danger)
0.00%
0 / 1
2.50
 toArray
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 jsonSerialize
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace BO\Zmscitizenapi\Models\Collections;
6
7use BO\Zmscitizenapi\Models\OfficeServiceRelation;
8use BO\Zmsentities\Schema\Entity;
9use InvalidArgumentException;
10use JsonSerializable;
11
12class 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                \App::$log->warning('Invalid OfficeServiceRelation skipped', ['exception' => $e->getMessage()]);
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            'relations' => array_map(fn(OfficeServiceRelation $relation) => $relation->toArray(), $this->relations),
45        ];
46    }
47
48    #[\Override]
49    public function jsonSerialize(): mixed
50    {
51        return $this->toArray();
52    }
53}