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