Lines
42.85%
6 / 14
Methods
20.00%
1 / 5
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 57.14% | 4 / 7 | 0.00% | 0 / 1 | 5.26 | ||
| ensureValid | 50.00% | 1 / 2 | 0.00% | 0 / 1 | 2.50 | ||
| toArray | 0.00% | 0 / 3 | 0.00% | 0 / 1 | 2 | ||
| jsonSerialize | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | ||
| getScopes | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| 12 | class 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 | /** | |
| 33 | * @return void | |
| 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 | 'scopes' => array_map(fn(ThinnedScope $scope) => $scope->toArray(), $this->scopes), | |
| 46 | ]; | |
| 47 | } | |
| 48 | ||
| 49 | #[\Override] | |
| 50 | public function jsonSerialize(): mixed | |
| 51 | { | |
| 52 | return $this->toArray(); | |
| 53 | } | |
| 54 | ||
| 55 | public function getScopes(): array | |
| 56 | { | |
| 57 | return $this->scopes; | |
| 58 | } | |
| 59 | } |