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
ServiceList
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\Service;
8use BO\Zmsentities\Schema\Entity;
9use InvalidArgumentException;
10use JsonSerializable;
11
12class ServiceList extends Entity implements JsonSerializable
13{
14    public static $schema = "citizenapi/collections/serviceList.json";
15/** @var Service[] */
16    public array $services = [];
17    public function __construct(array $services = [])
18    {
19        foreach ($services as $service) {
20            try {
21                if (!$service instanceof Service) {
22                    throw new InvalidArgumentException("Element is not an instance of Service.");
23                }
24                $this->services[] = $service;
25            } catch (\Exception $e) {
26                \App::$log->warning('Invalid Service skipped', ['exception' => $e->getMessage()]);
27            }
28        }
29
30        $this->ensureValid();
31    }
32
33    private function ensureValid()
34    {
35        if (!$this->testValid()) {
36            throw new InvalidArgumentException("The provided data is invalid according to the schema.");
37        }
38    }
39
40    /**
41     * Converts the service list to an array for serialization.
42     *
43     * @return array
44     */
45    public function toArray(): array
46    {
47        return [
48            "services" => array_map(fn(Service $service) => $service->toArray(), $this->services)
49        ];
50    }
51
52    #[\Override]
53    public function jsonSerialize(): mixed
54    {
55        return $this->toArray();
56    }
57}