Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
64.29% covered (warning)
64.29%
9 / 14
40.00% covered (danger)
40.00%
2 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ThinnedScopeList
64.29% covered (warning)
64.29%
9 / 14
40.00% covered (danger)
40.00%
2 / 5
12.69
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
 getScopes
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace BO\Zmscitizenapi\Models\Collections;
6
7use BO\Zmscitizenapi\Models\ThinnedScope;
8use BO\Zmsentities\Schema\Entity;
9use InvalidArgumentException;
10use JsonSerializable;
11
12class ThinnedScopeList extends Entity implements JsonSerializable
13{
14    public static $schema = "citizenapi/collections/thinnedScopeList.json";
15    public array $scopes = [];
16    public function __construct(array $scopes = [])
17    {
18        foreach ($scopes as $scope) {
19            try {
20                if (!$scope instanceof ThinnedScope) {
21                    throw new InvalidArgumentException("Element is not an instance of ThinnedScope.");
22                }
23                $this->scopes[] = $scope;
24            } catch (\Exception $e) {
25                \App::$log->warning('Invalid ThinnedScope skipped', ['exception' => $e->getMessage()]);
26            }
27        }
28
29        $this->ensureValid();
30    }
31
32    private function ensureValid()
33    {
34        if (!$this->testValid()) {
35            throw new InvalidArgumentException("The provided data is invalid according to the schema.");
36        }
37    }
38
39    public function toArray(): array
40    {
41        return [
42            'scopes' => array_map(fn(ThinnedScope $scope) => $scope->toArray(), $this->scopes),
43        ];
44    }
45
46    #[\Override]
47    public function jsonSerialize(): mixed
48    {
49        return $this->toArray();
50    }
51
52    public function getScopes(): array
53    {
54        return $this->scopes;
55    }
56}