Links   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
eloc 21
c 1
b 0
f 0
dl 0
loc 121
ccs 32
cts 32
cp 1
rs 10

12 Methods

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