Passed
Push — master ( 983e5f...18069b )
by Jasper
03:15
created

Jsonapi   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 89.47%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 68
ccs 17
cts 19
cp 0.8947
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getVersion() 0 3 1
A toArray() 0 13 3
A toJson() 0 3 1
A jsonSerialize() 0 4 1
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