Skip to content

Instantly share code, notes, and snippets.

@greg606
Created July 13, 2011 11:49
Show Gist options
  • Save greg606/1080163 to your computer and use it in GitHub Desktop.
Save greg606/1080163 to your computer and use it in GitHub Desktop.
<?php
class User_Model_Users implements Iterator, Countable {
protected $_count;
protected $_gateway;
protected $_resultSet;
public function __construct($results, $gateway) {
$this->setGateway ( $gateway );
$this->_resultSet = $results;
}
public function setGateway(User_Model_UserGateway $gateway) {
$this->_gateway = $gateway;
return $this;
}
public function getGateway() {
return $this->_gateway;
}
public function count() {
if (null === $this->_count) {
$this->_count = count ( $this->_resultSet );
}
return $this->_count;
}
public function current() {
if ($this->_resultSet instanceof Iterator) {
$key = $this->_resultSet->key();
} else {
$key = key ( $this->_resultSet );
}
$result = $this->_resultSet [$key];
if (! $result instanceof User_Model_User) {
$gateway = $this->getGateway();
$result = $gateway->createUser ( $result );
$this->_resultSet [$key] = $result;
}
return $result;
}
public function key() {
return key ( $this ->_resultSet );
}
public function next() {
return next ( $this->_resultSet );
}
public function rewind() {
return reset ( $this->_resultSet );
}
public function valid() {
return ( bool ) $this->current ();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment