Passed
Push — master ( 983e5f...18069b )
by Jasper
03:15
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
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 JsonSerializable;
10
use Swis\JsonApi\Client\Concerns\HasMeta;
11
12
class Jsonapi implements Arrayable, Jsonable, JsonSerializable
13
{
14 1
    use HasMeta;
15
16
    /**
17
     * @var string|null
18
     */
19
    protected $version;
20
21
    /**
22
     * @param string|null                    $version
23
     * @param \Swis\JsonApi\Client\Meta|null $meta
24
     */
25 15
    public function __construct(string $version = null, Meta $meta = null)
26
    {
27 15
        $this->version = $version;
28 15
        $this->meta = $meta;
29 15
    }
30
31
    /**
32
     * @return string|null
33
     */
34 9
    public function getVersion()
35
    {
36 9
        return $this->version;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     *
42
     * @return array
43
     */
44 6
    public function toArray(): array
45
    {
46 6
        $array = [];
47
48 6
        if ($this->getVersion() !== null) {
49 3
            $array['version'] = $this->getVersion();
50
        }
51
52 6
        if ($this->getMeta() !== null) {
53 3
            $array['meta'] = $this->getMeta()->toArray();
54
        }
55
56 6
        return $array;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     *
62
     * @param int $options
63
     *
64
     * @return false|string
65
     */
66
    public function toJson($options = 0)
67
    {
68
        return json_encode($this->jsonSerialize(), JSON_THROW_ON_ERROR | $options);
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     *
74
     * @return object
75
     */
76 1
    #[\ReturnTypeWillChange]
77 2
    public function jsonSerialize()
78
    {
79 3
        return (object) $this->toArray();
80
    }
81
}
82