BaseModel::jsonSerialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Inspirum\Arrayable;
6
7
use function json_encode;
8
use const JSON_THROW_ON_ERROR;
9
10
/**
11
 * @template TKey of array-key
12
 * @template TValue
13
 * @implements \Inspirum\Arrayable\Model<TKey,TValue>
14
 */
15
abstract class BaseModel implements Model
16
{
17
    /**
18
     * @return array<TKey,TValue>
19
     */
20
    abstract public function __toArray(): array;
21
22
    /**
23
     * @return array<TKey,TValue>
24
     */
25 2
    public function toArray(): array
26
    {
27 2
        return $this->__toArray();
28
    }
29
30
    /**
31
     * @return array<TKey,TValue>
32
     */
33 2
    public function jsonSerialize(): array
34
    {
35 2
        return $this->__toArray();
36
    }
37
38
    /**
39
     * @throws \JsonException
40
     */
41 2
    public function __toString(): string
42
    {
43 2
        return json_encode($this->jsonSerialize(), JSON_THROW_ON_ERROR);
44
    }
45
}
46