Lines 61.53% 8 / 13
Functions and Methods 25.00% 1 / 4
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
OfficeServiceRelationList 61.53% 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
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    public array $relations = [];
16    public function __construct(array $relations = [])
17    {
18
19        foreach ($relations as $relation) {
20            try {
21                if (!$relation instanceof OfficeServiceRelation) {
22                    throw new InvalidArgumentException("Element is not an instance of OfficeServiceRelation.");
23                }
24                $this->relations[] = $relation;
25            } catch (\Exception $e) {
26                \App::$log->warning('Invalid OfficeServiceRelation skipped', ['exception' => $e->getMessage()]);
27            }
28        }
29
30        $this->ensureValid();
31    }
32
33    /**
34     * @return void
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    public function toArray(): array
44    {
45        return [
46            'relations' => array_map(fn(OfficeServiceRelation $relation) => $relation->toArray(), $this->relations),
47        ];
48    }
49
50    #[\Override]
51    public function jsonSerialize(): mixed
52    {
53        return $this->toArray();
54    }
55}