Page MenuHomePhabricator
Paste P2120

ConduitIterator.php
ActivePublic

Authored by joshuaspence on Jul 18 2019, 10:33 PM.
Tags
None
Referenced Files
F6602178: raw.txt
Jul 18 2019, 10:33 PM
Subscribers
None
<?php
final class ConduitIterator implements Iterator {
private $client;
private $method;
private $parameters;
private $current;
private $cursor;
public function __construct(ConduitClient $client, string $method, array $parameters) {
$this->client = $client;
$this->method = $method;
$this->parameters = $parameters;
}
private function execute(): void {
$parameters = $this->parameters;
if ($this->cursor !== null) {
$parameters['after'] = $this->cursor['after'];
}
$response = $this->client->callMethodSynchronous($this->method, $parameters);
$this->current = new ArrayIterator($response['data']);
$this->cursor = $response['cursor'];
}
private function getResults(): ArrayIterator {
if ($this->current === null) {
$this->execute();
}
return $this->current;
}
/* -( Iterator )----------------------------------------------------------- */
public function current() {
return $this->getResults()->current();
}
public function key() {
return $this->getResults()->key();
}
public function next(): void {
$this->getResults()->next();
}
public function rewind(): void {
$this->current = null;
$this->cursor = null;
}
public function valid(): bool {
$valid = $this->getResults()->valid();
if (!$valid) {
if ($this->cursor['after'] === null) {
return false;
}
$this->execute();
return true;
}
return $valid;
}
}