Lines 92.00% 23 / 25
Functions and Methods 50.00% 2 / 4
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
Service 92.00% 23 / 25 50.00% 2 / 4 5.01 0.00% 0 / 1
 __construct 100.00% 10 / 10 100.00% 1 / 1 1
 ensureValid 50.00% 1 / 2 0.00% 0 / 1 2.50
 toArray 100.00% 12 / 12 100.00% 1 / 1 1
 jsonSerialize 0.00% 0 / 1 0.00% 0 / 1 2
1<?php
2
3declare(strict_types=1);
4
5namespace BO\Zmscitizenapi\Models;
6
7use BO\Zmscitizenapi\Models\Combinable;
8use BO\Zmsentities\Schema\Entity;
9use InvalidArgumentException;
10use JsonSerializable;
11
12class 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 ?int $rootParentId = null;
22    public ?bool $showOnStartPage = null;
23    public ?array $variantOverwrite = null;
24
25    public function __construct(
26        int $id,
27        string $name,
28        ?int $maxQuantity = null,
29        ?Combinable $combinable = null,
30        ?int $parentId = null,
31        ?int $variantId = null,
32        ?bool $showOnStartPage = null,
33        ?int $rootParentId = null,
34        ?array $variantOverwrite = null
35    ) {
36        $this->id = $id;
37        $this->name = $name;
38        $this->maxQuantity = $maxQuantity;
39        $this->combinable = $combinable;
40        $this->parentId = $parentId;
41        $this->variantId = $variantId;
42        $this->showOnStartPage = $showOnStartPage;
43        $this->rootParentId = $rootParentId ?? $id;
44        $this->variantOverwrite = $variantOverwrite;
45        $this->ensureValid();
46    }
47
48    /**
49     * @return void
50     */
51    private function ensureValid()
52    {
53        if (!$this->testValid()) {
54            throw new InvalidArgumentException("The provided data is invalid according to the schema.");
55        }
56    }
57
58    public function toArray(): array
59    {
60        $data = [
61            'id' => $this->id,
62            'name' => $this->name,
63            'maxQuantity' => $this->maxQuantity,
64            'combinable' => $this->combinable,
65            'parentId' => $this->parentId,
66            'variantId' => $this->variantId,
67            'rootParentId' => $this->rootParentId,
68            'showOnStartPage' => $this->showOnStartPage,
69            'variantOverwrite' => $this->variantOverwrite
70        ];
71        return $data;
72    }
73
74    #[\Override]
75    public function jsonSerialize(): mixed
76    {
77        return $this->toArray();
78    }
79}