Passed
Push — master ( ce8fe8...a3b3fc )
by Jasper
09:32
created

Jsonapi::toJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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