Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.00% |
18 / 20 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| Service | |
90.00% |
18 / 20 |
|
50.00% |
2 / 4 |
5.03 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| ensureValid | |
50.00% |
1 / 2 |
|
0.00% |
0 / 1 |
2.50 | |||
| toArray | |
100.00% |
9 / 9 |
|
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 | public int $id; |
| 16 | public string $name; |
| 17 | public ?int $maxQuantity = null; |
| 18 | public ?Combinable $combinable = null; |
| 19 | public ?int $parentId = null; |
| 20 | public ?int $variantId = null; |
| 21 | public ?bool $showOnStartPage = null; |
| 22 | |
| 23 | public function __construct(int $id, string $name, ?int $maxQuantity = null, ?Combinable $combinable = null, ?int $parentId = null, ?int $variantId = null, ?bool $showOnStartPage = null) |
| 24 | { |
| 25 | $this->id = $id; |
| 26 | $this->name = $name; |
| 27 | $this->maxQuantity = $maxQuantity; |
| 28 | $this->combinable = $combinable; |
| 29 | $this->parentId = $parentId; |
| 30 | $this->variantId = $variantId; |
| 31 | $this->showOnStartPage = $showOnStartPage; |
| 32 | $this->ensureValid(); |
| 33 | } |
| 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 | /** |
| 43 | * Converts the model data back into an array for serialization. |
| 44 | * |
| 45 | * @return array |
| 46 | */ |
| 47 | public function toArray(): array |
| 48 | { |
| 49 | return [ |
| 50 | 'id' => $this->id, |
| 51 | 'name' => $this->name, |
| 52 | 'maxQuantity' => $this->maxQuantity, |
| 53 | 'combinable' => $this->combinable, |
| 54 | 'parent_id' => $this->parentId, |
| 55 | 'variant_id' => $this->variantId, |
| 56 | 'showOnStartPage' => $this->showOnStartPage |
| 57 | ]; |
| 58 | } |
| 59 | |
| 60 | public function jsonSerialize(): mixed |
| 61 | { |
| 62 | return $this->toArray(); |
| 63 | } |
| 64 | } |