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
|
|
|
* @package Elastification\Client\Serializer\Gateway |
22
|
|
|
* @author Mario Mueller |
23
|
|
|
*/ |
24
|
|
|
class NativeObjectGateway implements GatewayInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var object |
28
|
|
|
*/ |
29
|
|
|
private $jsonData; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var integer |
33
|
|
|
*/ |
34
|
|
|
private $propertyCount; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array<integer,integer|string> |
38
|
|
|
*/ |
39
|
|
|
private $properties; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var integer |
43
|
|
|
*/ |
44
|
|
|
private $currentPointer = 0; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param object $jsonData |
48
|
|
|
*/ |
49
|
158 |
|
function __construct($jsonData) |
|
|
|
|
50
|
|
|
{ |
51
|
158 |
|
$this->jsonData = $jsonData; |
52
|
158 |
|
$this->properties = array_keys(get_object_vars($jsonData)); |
53
|
158 |
|
$this->propertyCount = sizeof($this->properties); |
54
|
158 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* (PHP 5 >= 5.0.0)<br/> |
58
|
|
|
* Whether a offset exists |
59
|
|
|
* |
60
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetexists.php |
61
|
|
|
* |
62
|
|
|
* @param mixed $offset <p> |
63
|
|
|
* An offset to check for. |
64
|
|
|
* </p> |
65
|
|
|
* |
66
|
|
|
* @return boolean true on success or false on failure. |
67
|
|
|
* </p> |
68
|
|
|
* <p> |
69
|
|
|
* The return value will be casted to boolean if non-boolean was returned. |
70
|
|
|
*/ |
71
|
7 |
|
public function offsetExists($offset) |
72
|
|
|
{ |
73
|
7 |
|
return isset($this->jsonData->{$offset}); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* (PHP 5 >= 5.0.0)<br/> |
78
|
|
|
* Offset to retrieve |
79
|
|
|
* |
80
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetget.php |
81
|
|
|
* |
82
|
|
|
* @param mixed $offset <p> |
83
|
|
|
* The offset to retrieve. |
84
|
|
|
* </p> |
85
|
|
|
* |
86
|
|
|
* @return mixed Can return all value types. |
87
|
|
|
*/ |
88
|
144 |
View Code Duplication |
public function offsetGet($offset) |
|
|
|
|
89
|
|
|
{ |
90
|
144 |
|
if (property_exists($this->jsonData, $offset)) { |
91
|
143 |
|
return GatewayFactory::factory($this->jsonData->$offset); |
92
|
|
|
} |
93
|
|
|
|
94
|
1 |
|
return null; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* (PHP 5 >= 5.0.0)<br/> |
99
|
|
|
* Offset to set |
100
|
|
|
* |
101
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetset.php |
102
|
|
|
* |
103
|
|
|
* @param mixed $offset <p> |
104
|
|
|
* The offset to assign the value to. |
105
|
|
|
* </p> |
106
|
|
|
* @param mixed $value <p> |
107
|
|
|
* The value to set. |
108
|
|
|
* </p> |
109
|
|
|
* |
110
|
|
|
* @return void |
111
|
|
|
*/ |
112
|
1 |
|
public function offsetSet($offset, $value) |
113
|
|
|
{ |
114
|
1 |
|
throw new \BadMethodCallException('The result set is immutable'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* (PHP 5 >= 5.0.0)<br/> |
119
|
|
|
* Offset to unset |
120
|
|
|
* |
121
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetunset.php |
122
|
|
|
* |
123
|
|
|
* @param mixed $offset <p> |
124
|
|
|
* The offset to unset. |
125
|
|
|
* </p> |
126
|
|
|
* |
127
|
|
|
* @return void |
128
|
|
|
*/ |
129
|
1 |
|
public function offsetUnset($offset) |
130
|
|
|
{ |
131
|
1 |
|
throw new \BadMethodCallException('The result set is immutable'); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* (PHP 5 >= 5.0.0)<br/> |
136
|
|
|
* Return the current element |
137
|
|
|
* |
138
|
|
|
* @link http://php.net/manual/en/iterator.current.php |
139
|
|
|
* @return mixed Can return any type. |
140
|
|
|
*/ |
141
|
1 |
|
public function current() |
142
|
|
|
{ |
143
|
1 |
|
$property = $this->properties[$this->currentPointer]; |
144
|
|
|
|
145
|
1 |
|
return $this->jsonData->$property; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* (PHP 5 >= 5.0.0)<br/> |
150
|
|
|
* Move forward to next element |
151
|
|
|
* |
152
|
|
|
* @link http://php.net/manual/en/iterator.next.php |
153
|
|
|
* @return void Any returned value is ignored. |
154
|
|
|
*/ |
155
|
1 |
|
public function next() |
156
|
|
|
{ |
157
|
1 |
|
$this->currentPointer = $this->currentPointer + 1; |
158
|
1 |
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* (PHP 5 >= 5.0.0)<br/> |
162
|
|
|
* Return the key of the current element |
163
|
|
|
* |
164
|
|
|
* @link http://php.net/manual/en/iterator.key.php |
165
|
|
|
* @return mixed scalar on success, or null on failure. |
166
|
|
|
*/ |
167
|
1 |
|
public function key() |
168
|
|
|
{ |
169
|
1 |
|
return $this->properties[$this->currentPointer]; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* (PHP 5 >= 5.0.0)<br/> |
174
|
|
|
* Checks if current position is valid |
175
|
|
|
* |
176
|
|
|
* @link http://php.net/manual/en/iterator.valid.php |
177
|
|
|
* @return boolean The return value will be casted to boolean and then evaluated. |
178
|
|
|
* Returns true on success or false on failure. |
179
|
|
|
*/ |
180
|
1 |
|
public function valid() |
181
|
|
|
{ |
182
|
1 |
|
return isset($this->properties[$this->currentPointer]); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* (PHP 5 >= 5.0.0)<br/> |
187
|
|
|
* Rewind the Iterator to the first element |
188
|
|
|
* |
189
|
|
|
* @link http://php.net/manual/en/iterator.rewind.php |
190
|
|
|
* @return void Any returned value is ignored. |
191
|
|
|
*/ |
192
|
1 |
|
public function rewind() |
193
|
|
|
{ |
194
|
1 |
|
$this->currentPointer = 0; |
195
|
1 |
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* (PHP 5 >= 5.1.0)<br/> |
199
|
|
|
* Count elements of an object |
200
|
|
|
* |
201
|
|
|
* @link http://php.net/manual/en/countable.count.php |
202
|
|
|
* @return int The custom count as an integer. |
203
|
|
|
* </p> |
204
|
|
|
* <p> |
205
|
|
|
* The return value is cast to an integer. |
206
|
|
|
*/ |
207
|
1 |
|
public function count() |
208
|
|
|
{ |
209
|
1 |
|
return $this->propertyCount; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Returns the original value. |
214
|
|
|
* |
215
|
|
|
* @return mixed |
216
|
|
|
* @author Mario Mueller |
217
|
|
|
*/ |
218
|
4 |
|
public function getGatewayValue() |
219
|
|
|
{ |
220
|
4 |
|
return $this->jsonData; |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
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.