Meta::offsetGet()   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 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 2
b 0
f 0
nc 1
nop 1
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
10
class Meta implements \ArrayAccess, \JsonSerializable, Arrayable, Jsonable
11
{
12
    /**
13
     * @var array
14
     */
15
    protected $data = [];
16
17 184
    public function __construct(array $data)
18
    {
19 184
        $this->data = $data;
20 92
    }
21
22
    /**
23
     * @return bool
24
     */
25 4
    public function __isset($key)
26
    {
27 4
        return $this->offsetExists($key);
28
    }
29
30
    /**
31
     * @return mixed
32
     */
33 24
    public function __get($key)
34
    {
35 24
        return $this->offsetGet($key);
36
    }
37
38 4
    public function __unset($key)
39
    {
40 4
        $this->offsetUnset($key);
41 2
    }
42
43 4
    public function __set($key, $value)
44
    {
45 4
        $this->offsetSet($key, $value);
46 2
    }
47
48
    /**
49
     * {@inheritdoc}
50
     *
51
     * @param  mixed  $offset
52
     * @return bool
53
     */
54 8
    #[\ReturnTypeWillChange]
55
    public function offsetExists($offset)
56
    {
57 8
        return isset($this->data[$offset]);
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     *
63
     * @param  mixed  $offset
64
     * @return mixed
65
     */
66 36
    #[\ReturnTypeWillChange]
67
    public function offsetGet($offset)
68
    {
69 36
        return $this->data[$offset] ?? null;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     *
75
     * @param  mixed  $offset
76
     * @param  mixed  $value
77
     */
78 8
    #[\ReturnTypeWillChange]
79
    public function offsetSet($offset, $value)
80
    {
81 8
        $this->data[$offset] = $value;
82 4
    }
83
84
    /**
85
     * {@inheritdoc}
86
     *
87
     * @param  mixed  $offset
88
     */
89 8
    #[\ReturnTypeWillChange]
90
    public function offsetUnset($offset)
91
    {
92 8
        unset($this->data[$offset]);
93 4
    }
94
95
    /**
96
     * {@inheritdoc}
97
     *
98
     * @return array
99
     */
100 56
    public function toArray()
101
    {
102 56
        return $this->data;
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     *
108
     * @param  int  $options
109
     * @return false|string
110
     */
111 4
    public function toJson($options = 0)
112
    {
113 4
        return json_encode($this->jsonSerialize(), JSON_THROW_ON_ERROR | $options);
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     *
119
     * @return object
120
     */
121 8
    #[\ReturnTypeWillChange]
122
    public function jsonSerialize()
123
    {
124 8
        return (object) $this->toArray();
125
    }
126
}
127