Skip to content
Snippets Groups Projects
Commit 0b119477 authored by Tim Steiner's avatar Tim Steiner
Browse files

Added some unility methods to Unl_Model_Collection

parent 2f5e71d1
No related branches found
No related tags found
No related merge requests found
......@@ -2,68 +2,68 @@
class Unl_Model_Collection implements Iterator, Countable, ArrayAccess
{
/**
* name of the class of model this collection will be storing
*/
protected $_modelClass;
/**
* an array of the models in this collection
*/
protected $_models = array();
public function __construct($modelClass)
{
$this->_modelClass = $modelClass;
}
// Functions to implement the Iterator interface (allows foreach)
public function current()
{
return current($this->_models);
}
public function key()
{
return key($this->_models);
}
public function next()
{
return next($this->_models);
}
public function rewind()
{
return reset($this->_models);
}
public function valid()
{
return (bool) $this->current();
}
// Function to implement the Countable interface (allows count())
public function count()
{
return count($this->_models);
}
// Functions to implement ArrayAccess interface (allows $instance['key'])
public function offsetExists($offset)
{
return array_key_exists($offset, $this->_models);
}
public function offsetGet($offset)
{
return $this->_models[$offset];
}
public function offsetSet($offset, $value)
{
if (!$value instanceof $this->_modelClass) {
......@@ -74,19 +74,19 @@ class Unl_Model_Collection implements Iterator, Countable, ArrayAccess
throw new Exception('Cannot add a non-ojbect to a collection of "' . $this->_modelClass . '" objects');
}
}
if ($offset === null) {
return ($this->_models[] = $value);
} else {
return ($this->_models[$offset] = $value);
}
}
public function offsetUnset($offset)
{
unset($this->_models[$offset]);
}
public function merge(Unl_Model_Collection $collection)
{
foreach ($collection as $model) {
......@@ -94,7 +94,7 @@ class Unl_Model_Collection implements Iterator, Countable, ArrayAccess
$this->_models[$modelId] = $model;
}
}
/**
* Removes and returns the last element from the collection
*
......@@ -104,7 +104,7 @@ class Unl_Model_Collection implements Iterator, Countable, ArrayAccess
{
return array_pop($this->_models);
}
public function getId()
{
$ids = array();
......@@ -113,5 +113,33 @@ class Unl_Model_Collection implements Iterator, Countable, ArrayAccess
}
return $ids;
}
public function orderBy($method)
{
$valuesToSortOn = array();
$modelKeys = array();
foreach ($this->_models as $key => $model)
{
$valuesToSortOn[] = $model->$method();
$modelKeys[] = $key;
}
$valuesToSortOn = array_map('strtolower', $valuesToSortOn);
array_multisort($valuesToSortOn, $modelKeys);
$sortedModels = array();
foreach ($modelKeys as $modelKey) {
$sortedModels[$modelKey] = $this->_models[$modelKey];
}
$this->_models = $sortedModels;
}
public function arrayFromMethod($method)
{
$data = array();
foreach ($this->_models as $model)
{
$data[$model->getId()] = $model->$method();
}
return $data;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment