Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
88.89% covered (warning)
88.89%
16 / 18
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Service
88.89% covered (warning)
88.89%
16 / 18
50.00% covered (danger)
50.00%
2 / 4
5.03
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 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%
8 / 8
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
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/** @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    public ?int $parentId = null;
29    public ?int $variantId = null;
30/**
31     * Constructor.
32     *
33     * @param int $id
34     * @param string $name
35     * @param int|null $maxQuantity
36     * @param int|null $parentId
37     * @param int|null $variantId
38 */
39    public function __construct(int $id, string $name, ?int $maxQuantity = null, ?Combinable $combinable = null, ?int $parentId = null, ?int $variantId = null)
40    {
41        $this->id = $id;
42        $this->name = $name;
43        $this->maxQuantity = $maxQuantity;
44        $this->combinable = $combinable;
45        $this->parentId = $parentId;
46        $this->variantId = $variantId;
47        $this->ensureValid();
48    }
49
50    private function ensureValid()
51    {
52        if (!$this->testValid()) {
53            throw new InvalidArgumentException("The provided data is invalid according to the schema.");
54        }
55    }
56
57    /**
58     * Converts the model data back into an array for serialization.
59     *
60     * @return array
61     */
62    public function toArray(): array
63    {
64        return [
65            'id'          => $this->id,
66            'name'        => $this->name,
67            'maxQuantity' => $this->maxQuantity,
68            'combinable' => $this->combinable,
69            'parent_id'   => $this->parentId,
70            'variant_id'  => $this->variantId
71        ];
72    }
73
74    public function jsonSerialize(): mixed
75    {
76        return $this->toArray();
77    }
78}