Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
85.71% |
12 / 14 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
Service | |
85.71% |
12 / 14 |
|
50.00% |
2 / 4 |
5.07 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
ensureValid | |
50.00% |
1 / 2 |
|
0.00% |
0 / 1 |
2.50 | |||
toArray | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
jsonSerialize | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace BO\Zmscitizenapi\Models; |
6 | |
7 | use BO\Zmscitizenapi\Models\Combinable; |
8 | use BO\Zmsentities\Schema\Entity; |
9 | use InvalidArgumentException; |
10 | use JsonSerializable; |
11 | |
12 | class Service extends Entity implements JsonSerializable |
13 | { |
14 | public static $schema = 'citizenapi/service.json'; |
15 | /** @var int */ |
16 | public int $id; |
17 | /** @var string */ |
18 | public string $name; |
19 | /** |
20 | * Example property for maximum quantity, if relevant. |
21 | * Adjust or remove as needed. |
22 | * |
23 | * @var int|null |
24 | */ |
25 | public ?int $maxQuantity = null; |
26 | /** @var Combinable */ |
27 | public ?Combinable $combinable = null; |
28 | /** |
29 | * Constructor. |
30 | * |
31 | * @param int $id |
32 | * @param string $name |
33 | * @param int|null $maxQuantity |
34 | */ |
35 | public function __construct(int $id, string $name, ?int $maxQuantity = null, ?Combinable $combinable = null) |
36 | { |
37 | $this->id = $id; |
38 | $this->name = $name; |
39 | $this->maxQuantity = $maxQuantity; |
40 | $this->combinable = $combinable; |
41 | $this->ensureValid(); |
42 | } |
43 | |
44 | private function ensureValid() |
45 | { |
46 | if (!$this->testValid()) { |
47 | throw new InvalidArgumentException("The provided data is invalid according to the schema."); |
48 | } |
49 | } |
50 | |
51 | /** |
52 | * Converts the model data back into an array for serialization. |
53 | * |
54 | * @return array |
55 | */ |
56 | public function toArray(): array |
57 | { |
58 | return [ |
59 | 'id' => $this->id, |
60 | 'name' => $this->name, |
61 | 'maxQuantity' => $this->maxQuantity, |
62 | 'combinable' => $this->combinable |
63 | ]; |
64 | } |
65 | |
66 | public function jsonSerialize(): mixed |
67 | { |
68 | return $this->toArray(); |
69 | } |
70 | } |