Created
October 25, 2012 22:18
-
-
Save Hounddog/3955821 to your computer and use it in GitHub Desktop.
Repository Collection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace EdpGithub\Collection; | |
use Doctrine\Common\Collections\Collection; | |
use Doctrine\Common\Collections\ArrayCollection; | |
use Doctrine\Common\Collections\Selectable; | |
use EdpGithub\Http\Client; | |
class RepositoryCollection implements Collection, Selectable | |
{ | |
/** | |
* @var client | |
*/ | |
protected $httpClient; | |
/** | |
* @var string | |
*/ | |
protected $path; | |
/** | |
* @var array | |
*/ | |
protected $parameters; | |
/** | |
* @var array | |
*/ | |
protected $headers; | |
/** | |
* @var array | |
*/ | |
protected $elements = array(); | |
/** | |
* @var array | |
*/ | |
protected $snapshot = array(); | |
protected $isDirty = false; | |
protected $initialized = false; | |
public function __construct(Client $httpClient, $path, array $parameters =array(), array $headers = array()) | |
{ | |
$this->httpClient = $httpClient; | |
$this->path = $path; | |
$this->parameters = $parameters; | |
$this->headers = $headers; | |
} | |
public function initialize() | |
{ | |
if ($this->initialized) { | |
return; | |
} | |
$this->parameters['page'] = 1; | |
$response = $this->httpClient->get($this->path, $this->parameters, $this->headers); | |
//get pagination from response | |
$content = $response->getBody(); //returns json | |
$this->takeSnapshot(); | |
foreach($repositories as $repo){ | |
$this->add($repo); | |
} | |
$this->intialized = true; | |
} | |
public function takeSnapshot() | |
{ | |
$this->snapshot = $this->toArray() | |
$this->isDirty = false; | |
} | |
public function loadOffset() | |
{ | |
$repositories = $this->httpClient->get($this->path, $this->parameters, $this->headers); | |
$this->takeSnapshot(); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function add($element) | |
{ | |
$this->element[] = $element; | |
$this->changed(); | |
return true; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function clear() | |
{ | |
$this->elements = array(); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function contains($element) | |
{ | |
if(array_search($element, $this->element)) { | |
return true; | |
} | |
return false; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function isEmpty() | |
{ | |
return empty($this->elements); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function remove($key) | |
{ | |
if($this->containsKey($key)) { | |
unset($this->elements[$key]); | |
return true; | |
} | |
return false; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function removeElement($element) | |
{ | |
$key = $this->indexOf($element); | |
if($key) { | |
unset($this->elements[$key]); | |
return true; | |
} | |
return false; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function containsKey($key) | |
{ | |
if(isset($this->elements[$key])){ | |
return true; | |
} | |
return false; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function get($key) | |
{ | |
return $this->elements[$key]; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getKeys() | |
{ | |
return array_keys($this->elements); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getValues() | |
{ | |
return $this->elements; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function set($key, $value) | |
{ | |
$this->elements[$key] = $value; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function toArray() | |
{ | |
return $this->elements; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function first() | |
{ | |
$this->initialize(); | |
return reset($this->elements); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function last() | |
{ | |
$this->initialize(); | |
return end($this->elements); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function key() | |
{ | |
// code here | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function current() | |
{ | |
$this->initialize(); | |
return current($this->elements); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function next() | |
{ | |
$this->initialize(); | |
return next($this->elements); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function exists(Closure $p) | |
{ | |
// code here | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function filter(Closure $p) | |
{ | |
# code... | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function forAll(Closure $p) | |
{ | |
# code... | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function map(Closure $func) | |
{ | |
# code... | |
} | |
public function partition(Closure $p) | |
{ | |
# code... | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function indexOf($element) | |
{ | |
return array_search($element, $this->elements); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function slice($offset, $length = null) | |
{ | |
return array_slice($this->elements, $offset, $length, true); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function count() | |
{ | |
return count($this->elements); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getIterator() | |
{ | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function offsetExists($offset) | |
{ | |
return $this->containsKey($offset); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function offsetGet($offset) | |
{ | |
if($this->offsetExists($offset)) { | |
return $this->get($offset); | |
} | |
$this->loadOffset(); | |
if($this->offsetExists($offset)) { | |
return $this->get($offset); | |
} | |
return false; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function offsetSet($offset, $value) | |
{ | |
if(!$this->offsetExists($offset)){ | |
return $this->add($value); | |
} | |
return $this->set($offset, $value); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function offsetUnset($offset) | |
{ | |
return $this->remove($offset); | |
} | |
public function matching(Criteria $criteria) | |
{ | |
// code here... | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace EdpGithub\Collection; | |
use EdpGithub\Http\Client; | |
use Closure, Iterator; | |
class RepositoryCollection implements Iterator | |
{ | |
/** | |
* @var client | |
*/ | |
protected $httpClient; | |
/** | |
* @var string | |
*/ | |
protected $path; | |
/** | |
* @var array | |
*/ | |
protected $parameters; | |
/** | |
* @var array | |
*/ | |
protected $headers; | |
/** | |
* @var array | |
*/ | |
protected $elements = array(); | |
protected $pagination = null; | |
protected $perPage = 10; | |
protected $page = 1; | |
public function __construct(Client $httpClient, $path, array $parameters =array(), array $headers = array()) | |
{ | |
$this->httpClient = $httpClient; | |
$this->path = $path; | |
$this->parameters['per_page'] = $this->perPage; | |
$this->headers = $headers; | |
} | |
private function loadPage($page) | |
{ | |
if($this->pagination == null) { | |
$this->parameters['page'] = 1; | |
$elements = $this->fetch(); | |
$offset = 0; | |
foreach($elements as $element) { | |
$this->add($offset++, $element); | |
} | |
} | |
if($page > $this->pagination['last']) { | |
return false; | |
} | |
if($page != 1) { | |
$this->parameters['page'] = $page; | |
$elements = $this->fetch(); | |
} | |
$offset = (($page-1) * $this->perPage); | |
foreach($elements as $element) { | |
$this->add($offset++, $element); | |
} | |
return true; | |
} | |
public function loadOffset($offset) | |
{ | |
echo 'offset:'.$offset; | |
return false; | |
} | |
private function fetch() | |
{ | |
$response = $this->httpClient->get($this->path, $this->parameters, $this->headers); | |
$this->getPagination($response); | |
$elements = json_decode($response->getBody()); | |
return $elements; | |
} | |
public function page($page) | |
{ | |
$offsetStart = (($page-1) * $this->perPage); | |
$limit = $this->perPage -1; | |
$elements = array(); | |
for($offset=$offsetStart,$i=0;$i<=$limit; $i++, $offset++){ | |
if(!$this->containsKey($offset)) { | |
if($this->loadPage($page)) { | |
$elements[] = $this->get($offset); | |
} else { | |
break; | |
} | |
} else { | |
$elements[] = $this->get($offset); | |
} | |
} | |
return $elements; | |
} | |
public function add($offset, $element) | |
{ | |
$this->elements[$offset] = $element; | |
} | |
private function getPagination($response) | |
{ | |
$this->pagination['last'] = 1; | |
$headers= $response->getHeaders(); | |
if($headers->has('Link')) { | |
$header = $headers->get('Link')->getFieldValue(); | |
if (empty($header)) { | |
return null; | |
} | |
$pagination = array(); | |
foreach (explode(',', $header) as $link) { | |
preg_match('/<(.*)>; rel="(.*)"/i', trim($link, ','), $match); | |
if (3 === count($match)) { | |
$pagination[$match[2]] = $match[1]; | |
} | |
} | |
if(isset($pagination['last'])) { | |
$url = parse_url($pagination['last']); | |
parse_str($url['query'], $query); | |
$this->pagination['last'] = $query['page']; | |
} | |
} | |
} | |
public function rewind() | |
{ | |
reset($this->elements); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function current() | |
{ | |
return current($this->elements); | |
} | |
public function get($key) | |
{ | |
return $this->elements[$key]; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function key() | |
{ | |
return key($this->elements); | |
} | |
public function first() | |
{ | |
return $this->rewind(); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function next() | |
{ | |
return next($this->elements); | |
} | |
public function prev() | |
{ | |
return prev($this->elements); | |
} | |
public function getIterator() | |
{ | |
$this->rewind(); | |
$this->page = 1; | |
return $this; | |
} | |
public function valid() | |
{ | |
if(!$this->current()) { | |
return $this->loadPage(++$this->page); | |
} | |
return true; | |
} | |
public function containsKey($key) | |
{ | |
return array_key_exists($key, $this->elements); | |
} | |
public function count() | |
{ | |
return count($this->elements); | |
} | |
public function indexOf($element) | |
{ | |
return array_search($element, $this->elements); | |
} | |
public function removeElement($element) { | |
$key = $this->indexOf($element); | |
if($key) { | |
unset($this->elements[$key]); | |
return true; | |
} | |
return false; | |
} | |
public function toArray() | |
{ | |
return $this->elements; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment