Jsonapi::jsonSerialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Swis\JsonApi\Client;
6
7
use Illuminate\Contracts\Support\Arrayable;
8
use Illuminate\Contracts\Support\Jsonable;
9
use Swis\JsonApi\Client\Concerns\HasMeta;
10
11
class Jsonapi implements \JsonSerializable, Arrayable, Jsonable
12
{
13
    use HasMeta;
14
15
    /**
16
     * @var string|null
17
     */
18
    protected $version;
19
20 20
    public function __construct(?string $version = null, ?Meta $meta = null)
21
    {
22 20
        $this->version = $version;
23 20
        $this->meta = $meta;
24 10
    }
25
26
    /**
27
     * @return string|null
28
     */
29 12
    public function getVersion()
30
    {
31 12
        return $this->version;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 8
    public function toArray(): array
38
    {
39 8
        $array = [];
40
41 8
        if ($this->getVersion() !== null) {
42 4
            $array['version'] = $this->getVersion();
43
        }
44
45 8
        if ($this->getMeta() !== null) {
46 4
            $array['meta'] = $this->getMeta()->toArray();
47
        }
48
49 8
        return $array;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     *
55
     * @param  int  $options
56
     * @return false|string
57
     */
58
    public function toJson($options = 0)
59
    {
60
        return json_encode($this->jsonSerialize(), JSON_THROW_ON_ERROR | $options);
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     *
66
     * @return object
67
     */
68 4
    #[\ReturnTypeWillChange]
69
    public function jsonSerialize()
70
    {
71 4
        return (object) $this->toArray();
72
    }
73
}
74