DynamicModel::jsonSerialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
namespace SimpleCMS\DynamicUnit\Packages;
3
4
use Illuminate\Support\Collection;
5
6
class DynamicModel implements \JsonSerializable
7
{
8
9
    public function __construct(public int $id, public string $name, public string $value, public null|Collection $items)
10
    {
11
    }
12
13
14
    /**
15
     * toArray
16
     * @return array<string,null|string>>
17
     */
18
    public function toArray(): array
19
    {
20
        $data = [
21
            'id' => $this->id ?? 0,
22
            'name' => $this->name ?? null,
23
            'value' => $this->value ?? null,
24
            'items' => $this->items ? $this->items->toArray() : null
25
        ];
26
27
        return $data;
28
    }
29
30
    /**
31
     * jsonSerialize
32
     * @return mixed
33
     */
34
    public function jsonSerialize()
35
    {
36
        return $this->toArray();
37
    }
38
39
    /**
40
     * __toString
41
     * @return string
42
     */
43
    public function __toString(): string
44
    {
45
        return json_encode($this->toArray());
46
    }
47
}