1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. |
17
|
|
|
*/ |
18
|
|
|
namespace Elastification\Client\Serializer\Gateway; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Represents a gateway to an array. |
22
|
|
|
* |
23
|
|
|
* This one is used when using the {@see Elastification\Client\Serializer\NativeJsonSerializer}. |
24
|
|
|
* |
25
|
|
|
* @package Elastification\Client\Serializer\Gateway |
26
|
|
|
* @author Mario Mueller |
27
|
|
|
*/ |
28
|
|
|
class NativeArrayGateway implements GatewayInterface |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private $jsonData; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param array $jsonData The raw data. |
37
|
|
|
*/ |
38
|
192 |
|
function __construct(array $jsonData) |
|
|
|
|
39
|
|
|
{ |
40
|
192 |
|
$this->jsonData = $jsonData; |
41
|
192 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Whether a offset exists |
45
|
|
|
* |
46
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetexists.php |
47
|
|
|
* |
48
|
|
|
* @param mixed $offset An offset to check for. |
49
|
|
|
* |
50
|
|
|
* @return boolean true on success or false on failure. |
51
|
|
|
*/ |
52
|
10 |
|
public function offsetExists($offset) |
53
|
|
|
{ |
54
|
10 |
|
return isset($this->jsonData[$offset]); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Offset to retrieve |
59
|
|
|
* |
60
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetget.php |
61
|
|
|
* |
62
|
|
|
* @param mixed $offset The offset to retrieve. |
63
|
|
|
* |
64
|
|
|
* @return mixed Can return all value types. |
65
|
|
|
*/ |
66
|
143 |
View Code Duplication |
public function offsetGet($offset) |
|
|
|
|
67
|
|
|
{ |
68
|
143 |
|
if (isset($this->jsonData[$offset])) { |
69
|
142 |
|
return GatewayFactory::factory($this->jsonData[$offset]); |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
return null; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Offset to set |
77
|
|
|
* |
78
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetset.php |
79
|
|
|
* |
80
|
|
|
* @param mixed $offset The offset to assign the value to. |
81
|
|
|
* @param mixed $value The value to set. |
82
|
|
|
* |
83
|
|
|
* @return void |
84
|
|
|
*/ |
85
|
1 |
|
public function offsetSet($offset, $value) |
86
|
|
|
{ |
87
|
1 |
|
throw new \BadMethodCallException('The result set is immutable'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Offset to unset |
92
|
|
|
* |
93
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetunset.php |
94
|
|
|
* |
95
|
|
|
* @param mixed $offset The offset to unset. |
96
|
|
|
* |
97
|
|
|
* @return void |
98
|
|
|
*/ |
99
|
1 |
|
public function offsetUnset($offset) |
100
|
|
|
{ |
101
|
1 |
|
throw new \BadMethodCallException('The result set is immutable'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Return the current element |
106
|
|
|
* |
107
|
|
|
* @link http://php.net/manual/en/iterator.current.php |
108
|
|
|
* @return mixed Can return any type. |
109
|
|
|
*/ |
110
|
1 |
|
public function current() |
111
|
|
|
{ |
112
|
1 |
|
return current($this->jsonData); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Move forward to next element |
117
|
|
|
* |
118
|
|
|
* @link http://php.net/manual/en/iterator.next.php |
119
|
|
|
* @return void Any returned value is ignored. |
120
|
|
|
*/ |
121
|
1 |
|
public function next() |
122
|
|
|
{ |
123
|
1 |
|
next($this->jsonData); |
124
|
1 |
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Return the key of the current element |
128
|
|
|
* |
129
|
|
|
* @link http://php.net/manual/en/iterator.key.php |
130
|
|
|
* @return mixed scalar on success, or null on failure. |
131
|
|
|
*/ |
132
|
1 |
|
public function key() |
133
|
|
|
{ |
134
|
1 |
|
return key($this->jsonData); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Checks if current position is valid |
139
|
|
|
* |
140
|
|
|
* @link http://php.net/manual/en/iterator.valid.php |
141
|
|
|
* @return boolean The return value will be casted to boolean and then evaluated. |
142
|
|
|
*/ |
143
|
1 |
|
public function valid() |
144
|
|
|
{ |
145
|
1 |
|
return current($this->jsonData) ? true : false; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Rewind the Iterator to the first element |
150
|
|
|
* |
151
|
|
|
* @link http://php.net/manual/en/iterator.rewind.php |
152
|
|
|
* @return void Any returned value is ignored. |
153
|
|
|
*/ |
154
|
1 |
|
public function rewind() |
155
|
|
|
{ |
156
|
1 |
|
reset($this->jsonData); |
157
|
1 |
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Count elements of an object |
161
|
|
|
* |
162
|
|
|
* @link http://php.net/manual/en/countable.count.php |
163
|
|
|
* @return int The custom count as an integer. The return value is cast to an integer. |
164
|
|
|
*/ |
165
|
1 |
|
public function count() |
166
|
|
|
{ |
167
|
1 |
|
return sizeof($this->jsonData); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Returns the original value. |
172
|
|
|
* |
173
|
|
|
* @return mixed |
174
|
|
|
* @author Mario Mueller |
175
|
|
|
*/ |
176
|
80 |
|
public function getGatewayValue() |
177
|
|
|
{ |
178
|
80 |
|
return $this->jsonData; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.