Passed
Push — master ( 975c2f...d51421 )
by Jasper
13:00 queued 01:21
created

Jsonapi::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
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 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 3
    public function jsonSerialize()
77
    {
78 3
        return (object) $this->toArray();
79
    }
80
}
81