1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace Swader\Diffbot\Entity;
|
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\ResponseInterface as Response;
|
6
|
|
|
use Swader\Diffbot\Abstracts\Entity;
|
7
|
|
|
|
8
|
|
|
class EntityIterator implements \Countable, \Iterator, \ArrayAccess
|
9
|
|
|
{
|
10
|
|
|
/** @var array */
|
11
|
|
|
protected $data;
|
12
|
|
|
/** @var int */
|
13
|
|
|
protected $cursor = -1;
|
14
|
|
|
/** @var Entity */
|
15
|
|
|
protected $current;
|
16
|
|
|
/** @var Response */
|
17
|
|
|
protected $response;
|
18
|
|
|
|
19
|
190 |
|
public function __construct(array $objects, Response $response)
|
20
|
|
|
{
|
21
|
190 |
|
$this->response = $response;
|
22
|
190 |
|
$this->data = $objects;
|
23
|
190 |
|
$this->next();
|
24
|
190 |
|
}
|
25
|
|
|
|
26
|
|
|
/**
|
27
|
|
|
* Returns the original response
|
28
|
|
|
* @return Response
|
29
|
|
|
*/
|
30
|
3 |
|
public function getResponse()
|
31
|
|
|
{
|
32
|
3 |
|
return $this->response;
|
33
|
|
|
}
|
34
|
|
|
|
35
|
5 |
|
public function count()
|
36
|
|
|
{
|
37
|
5 |
|
return count($this->data);
|
38
|
|
|
}
|
39
|
|
|
|
40
|
173 |
|
public function rewind()
|
41
|
|
|
{
|
42
|
173 |
|
if ($this->cursor > 0) {
|
43
|
1 |
|
$this->cursor = -1;
|
44
|
1 |
|
$this->next();
|
45
|
1 |
|
}
|
46
|
173 |
|
}
|
47
|
|
|
|
48
|
94 |
|
public function key()
|
49
|
|
|
{
|
50
|
94 |
|
return $this->cursor;
|
51
|
|
|
}
|
52
|
|
|
|
53
|
178 |
|
public function current()
|
54
|
|
|
{
|
55
|
178 |
|
while(!$this->offsetExists($this->cursor)) {
|
56
|
|
|
$this->next();
|
57
|
|
|
}
|
58
|
178 |
|
return $this->data[$this->cursor];
|
59
|
|
|
}
|
60
|
|
|
|
61
|
190 |
|
public function next()
|
62
|
|
|
{
|
63
|
190 |
|
$this->cursor++;
|
64
|
190 |
|
}
|
65
|
|
|
|
66
|
173 |
|
public function valid()
|
67
|
|
|
{
|
68
|
173 |
|
if (count($this->data)) {
|
69
|
173 |
|
return ($this->cursor <= max(array_keys($this->data)));
|
70
|
|
|
} else {
|
71
|
|
|
return false;
|
72
|
|
|
}
|
73
|
|
|
}
|
74
|
|
|
|
75
|
5 |
|
protected function _getZerothEntity()
|
76
|
|
|
{
|
77
|
5 |
|
return ($this->cursor == -1) ? $this->data[0] : $this->current();
|
78
|
|
|
}
|
79
|
|
|
|
80
|
6 |
|
public function __call($name, $args)
|
81
|
|
|
{
|
82
|
6 |
|
$isGetter = substr($name, 0, 3) == 'get';
|
83
|
|
|
|
84
|
6 |
|
if ($isGetter) {
|
85
|
5 |
|
$zeroth = $this->_getZerothEntity();
|
86
|
5 |
|
if (method_exists($this->_getZerothEntity(), $name)) {
|
87
|
5 |
|
$rv = $zeroth->$name(...$args);
|
88
|
5 |
|
} else {
|
89
|
|
|
$property = lcfirst(substr($name, 3, strlen($name) - 3));
|
90
|
|
|
$rv = $zeroth->$property;
|
91
|
|
|
}
|
92
|
|
|
|
93
|
5 |
|
return $rv;
|
94
|
|
|
}
|
95
|
|
|
|
96
|
1 |
|
throw new \BadMethodCallException('No such method: ' . $name);
|
97
|
|
|
}
|
98
|
|
|
|
99
|
1 |
|
public function __get($name)
|
100
|
|
|
{
|
101
|
1 |
|
$entity = $this->_getZerothEntity();
|
102
|
|
|
|
103
|
1 |
|
return $entity->$name;
|
104
|
|
|
}
|
105
|
|
|
|
106
|
|
|
/**
|
107
|
|
|
* (PHP 5 >= 5.0.0)<br/>
|
108
|
|
|
* Whether a offset exists
|
109
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetexists.php
|
110
|
|
|
* @param mixed $offset <p>
|
111
|
|
|
* An offset to check for.
|
112
|
|
|
* </p>
|
113
|
|
|
* @return boolean true on success or false on failure.
|
114
|
|
|
* </p>
|
115
|
|
|
* <p>
|
116
|
|
|
* The return value will be casted to boolean if non-boolean was returned.
|
117
|
|
|
*/
|
118
|
178 |
|
public function offsetExists($offset)
|
119
|
|
|
{
|
120
|
178 |
|
return (isset($this->data[$offset]));
|
121
|
|
|
}
|
122
|
|
|
|
123
|
|
|
/**
|
124
|
|
|
* (PHP 5 >= 5.0.0)<br/>
|
125
|
|
|
* Offset to retrieve
|
126
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetget.php
|
127
|
|
|
* @param mixed $offset <p>
|
128
|
|
|
* The offset to retrieve.
|
129
|
|
|
* </p>
|
130
|
|
|
* @return mixed Can return all value types.
|
131
|
|
|
*/
|
132
|
|
|
public function offsetGet($offset)
|
133
|
|
|
{
|
134
|
|
|
if ($this->offsetExists($offset)) {
|
135
|
|
|
return $this->data[$offset];
|
136
|
|
|
}
|
137
|
|
|
throw new \OutOfBoundsException("Offset '$offset' not present");
|
138
|
|
|
}
|
139
|
|
|
|
140
|
|
|
/**
|
141
|
|
|
* (PHP 5 >= 5.0.0)<br/>
|
142
|
|
|
* Offset to set
|
143
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetset.php
|
144
|
|
|
* @param mixed $offset <p>
|
145
|
|
|
* The offset to assign the value to.
|
146
|
|
|
* </p>
|
147
|
|
|
* @param mixed $value <p>
|
148
|
|
|
* The value to set.
|
149
|
|
|
* </p>
|
150
|
|
|
* @return void
|
151
|
|
|
*/
|
152
|
|
|
public function offsetSet($offset, $value)
|
153
|
|
|
{
|
154
|
|
|
throw new \BadMethodCallException(
|
155
|
|
|
'Resultset is read only'
|
156
|
|
|
);
|
157
|
|
|
}
|
158
|
|
|
|
159
|
|
|
/**
|
160
|
|
|
* (PHP 5 >= 5.0.0)<br/>
|
161
|
|
|
* Offset to unset
|
162
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetunset.php
|
163
|
|
|
* @param mixed $offset <p>
|
164
|
|
|
* The offset to unset.
|
165
|
|
|
* </p>
|
166
|
|
|
* @return void
|
167
|
|
|
*/
|
168
|
|
|
public function offsetUnset($offset)
|
169
|
|
|
{
|
170
|
|
|
unset($this->data[$offset]);
|
171
|
|
|
}
|
172
|
|
|
} |