Open Journal Systems  3.3.0
ResourceIterator.php
1 <?php
2 
4 
7 
9 {
11  protected $command;
12 
14  protected $originalCommand;
15 
17  protected $resources;
18 
20  protected $retrievedCount = 0;
21 
23  protected $iteratedCount = 0;
24 
26  protected $nextToken = false;
27 
29  protected $pageSize;
30 
32  protected $limit;
33 
35  protected $requestCount = 0;
36 
38  protected $data = array();
39 
41  protected $invalid;
42 
43  public static function getAllEvents()
44  {
45  return array(
46  // About to issue another command to get more results
47  'resource_iterator.before_send',
48  // Issued another command to get more results
49  'resource_iterator.after_send'
50  );
51  }
52 
60  public function __construct(CommandInterface $command, array $data = array())
61  {
62  // Clone the command to keep track of the originating command for rewind
63  $this->originalCommand = $command;
64 
65  // Parse options from the array of options
66  $this->data = $data;
67  $this->limit = array_key_exists('limit', $data) ? $data['limit'] : 0;
68  $this->pageSize = array_key_exists('page_size', $data) ? $data['page_size'] : false;
69  }
70 
76  public function toArray()
77  {
78  return iterator_to_array($this, false);
79  }
80 
81  public function setLimit($limit)
82  {
83  $this->limit = $limit;
84  $this->resetState();
85 
86  return $this;
87  }
88 
89  public function setPageSize($pageSize)
90  {
91  $this->pageSize = $pageSize;
92  $this->resetState();
93 
94  return $this;
95  }
96 
104  public function get($key)
105  {
106  return array_key_exists($key, $this->data) ? $this->data[$key] : null;
107  }
108 
117  public function set($key, $value)
118  {
119  $this->data[$key] = $value;
120 
121  return $this;
122  }
123 
124  public function current()
125  {
126  return $this->resources ? current($this->resources) : false;
127  }
128 
129  public function key()
130  {
131  return max(0, $this->iteratedCount - 1);
132  }
133 
134  public function count()
135  {
136  return $this->retrievedCount;
137  }
138 
144  public function getRequestCount()
145  {
146  return $this->requestCount;
147  }
148 
152  public function rewind()
153  {
154  // Use the original command
155  $this->command = clone $this->originalCommand;
156  $this->resetState();
157  $this->next();
158  }
159 
160  public function valid()
161  {
162  return !$this->invalid && (!$this->resources || $this->current() || $this->nextToken)
163  && (!$this->limit || $this->iteratedCount < $this->limit + 1);
164  }
165 
166  public function next()
167  {
168  $this->iteratedCount++;
169 
170  // Check if a new set of resources needs to be retrieved
171  $sendRequest = false;
172  if (!$this->resources) {
173  $sendRequest = true;
174  } else {
175  // iterate over the internal array
176  $current = next($this->resources);
177  $sendRequest = $current === false && $this->nextToken && (!$this->limit || $this->iteratedCount < $this->limit + 1);
178  }
179 
180  if ($sendRequest) {
181 
182  $this->dispatch('resource_iterator.before_send', array(
183  'iterator' => $this,
184  'resources' => $this->resources
185  ));
186 
187  // Get a new command object from the original command
188  $this->command = clone $this->originalCommand;
189  // Send a request and retrieve the newly loaded resources
190  $this->resources = $this->sendRequest();
191  $this->requestCount++;
192 
193  // If no resources were found, then the last request was not needed
194  // and iteration must stop
195  if (empty($this->resources)) {
196  $this->invalid = true;
197  } else {
198  // Add to the number of retrieved resources
199  $this->retrievedCount += count($this->resources);
200  // Ensure that we rewind to the beginning of the array
201  reset($this->resources);
202  }
203 
204  $this->dispatch('resource_iterator.after_send', array(
205  'iterator' => $this,
206  'resources' => $this->resources
207  ));
208  }
209  }
210 
216  public function getNextToken()
217  {
218  return $this->nextToken;
219  }
220 
227  protected function calculatePageSize()
228  {
229  if ($this->limit && $this->iteratedCount + $this->pageSize > $this->limit) {
230  return 1 + ($this->limit - $this->iteratedCount);
231  }
232 
233  return (int) $this->pageSize;
234  }
235 
239  protected function resetState()
240  {
241  $this->iteratedCount = 0;
242  $this->retrievedCount = 0;
243  $this->nextToken = false;
244  $this->resources = null;
245  $this->invalid = false;
246  }
247 
253  abstract protected function sendRequest();
254 }
Guzzle\Service\Resource\ResourceIterator\$originalCommand
$originalCommand
Definition: ResourceIterator.php:20
Guzzle\Service\Resource\ResourceIterator\current
current()
Definition: ResourceIterator.php:157
Guzzle\Service\Resource\ResourceIterator\$requestCount
$requestCount
Definition: ResourceIterator.php:62
Guzzle\Service\Resource\ResourceIterator\getAllEvents
static getAllEvents()
Definition: ResourceIterator.php:76
Guzzle\Service\Resource\ResourceIterator\setPageSize
setPageSize($pageSize)
Definition: ResourceIterator.php:122
Guzzle\Service\Resource\ResourceIterator\rewind
rewind()
Definition: ResourceIterator.php:185
Guzzle\Service\Resource\ResourceIterator\$retrievedCount
$retrievedCount
Definition: ResourceIterator.php:32
Guzzle\Service\Resource\ResourceIterator\setLimit
setLimit($limit)
Definition: ResourceIterator.php:114
Guzzle\Service\Resource\ResourceIterator\$iteratedCount
$iteratedCount
Definition: ResourceIterator.php:38
Guzzle\Service\Resource\ResourceIterator\__construct
__construct(CommandInterface $command, array $data=array())
Definition: ResourceIterator.php:93
Guzzle\Service\Resource\ResourceIterator\sendRequest
sendRequest()
Guzzle\Service\Resource\ResourceIterator\$invalid
$invalid
Definition: ResourceIterator.php:74
Guzzle\Service\Resource\ResourceIterator\$pageSize
$pageSize
Definition: ResourceIterator.php:50
Guzzle\Service\Resource\ResourceIterator
Definition: ResourceIterator.php:8
Guzzle\Service\Resource\ResourceIteratorInterface
Definition: ResourceIteratorInterface.php:11
Guzzle\Service\Resource\ResourceIterator\$nextToken
$nextToken
Definition: ResourceIterator.php:44
Guzzle\Service\Resource\ResourceIterator\$limit
$limit
Definition: ResourceIterator.php:56
Guzzle\Service\Resource\ResourceIterator\valid
valid()
Definition: ResourceIterator.php:193
Guzzle\Service\Command\CommandInterface
Definition: CommandInterface.php:17
Guzzle\Service\Resource\ResourceIterator\calculatePageSize
calculatePageSize()
Definition: ResourceIterator.php:260
Guzzle\Service\Resource\ResourceIterator\$resources
$resources
Definition: ResourceIterator.php:26
Guzzle\Service\Resource\ResourceIterator\resetState
resetState()
Definition: ResourceIterator.php:272
Guzzle\Service\Resource\ResourceIterator\next
next()
Definition: ResourceIterator.php:199
Guzzle\Service\Resource\ResourceIterator\toArray
toArray()
Definition: ResourceIterator.php:109
Guzzle\Service\Resource\ResourceIterator\key
key()
Definition: ResourceIterator.php:162
Guzzle\Service\Resource
Definition: AbstractResourceIteratorFactory.php:3
Guzzle\Service\Resource\ResourceIterator\$data
$data
Definition: ResourceIterator.php:68
Guzzle\Service\Resource\ResourceIterator\count
count()
Definition: ResourceIterator.php:167
Guzzle\Service\Resource\ResourceIterator\getNextToken
getNextToken()
Definition: ResourceIterator.php:249
Guzzle\Common\AbstractHasDispatcher\dispatch
dispatch($eventName, array $context=array())
Definition: AbstractHasDispatcher.php:41
Guzzle\Common\AbstractHasDispatcher
Definition: AbstractHasDispatcher.php:12
Guzzle\Service\Resource\ResourceIterator\$command
$command
Definition: ResourceIterator.php:14
Guzzle\Service\Resource\ResourceIterator\getRequestCount
getRequestCount()
Definition: ResourceIterator.php:177