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 @@ ...@@ -2,68 +2,68 @@
class Unl_Model_Collection implements Iterator, Countable, ArrayAccess class Unl_Model_Collection implements Iterator, Countable, ArrayAccess
{ {
/** /**
* name of the class of model this collection will be storing * name of the class of model this collection will be storing
*/ */
protected $_modelClass; protected $_modelClass;
/** /**
* an array of the models in this collection * an array of the models in this collection
*/ */
protected $_models = array(); protected $_models = array();
public function __construct($modelClass) public function __construct($modelClass)
{ {
$this->_modelClass = $modelClass; $this->_modelClass = $modelClass;
} }
// Functions to implement the Iterator interface (allows foreach) // Functions to implement the Iterator interface (allows foreach)
public function current() public function current()
{ {
return current($this->_models); return current($this->_models);
} }
public function key() public function key()
{ {
return key($this->_models); return key($this->_models);
} }
public function next() public function next()
{ {
return next($this->_models); return next($this->_models);
} }
public function rewind() public function rewind()
{ {
return reset($this->_models); return reset($this->_models);
} }
public function valid() public function valid()
{ {
return (bool) $this->current(); return (bool) $this->current();
} }
// Function to implement the Countable interface (allows count()) // Function to implement the Countable interface (allows count())
public function count() public function count()
{ {
return count($this->_models); return count($this->_models);
} }
// Functions to implement ArrayAccess interface (allows $instance['key']) // Functions to implement ArrayAccess interface (allows $instance['key'])
public function offsetExists($offset) public function offsetExists($offset)
{ {
return array_key_exists($offset, $this->_models); return array_key_exists($offset, $this->_models);
} }
public function offsetGet($offset) public function offsetGet($offset)
{ {
return $this->_models[$offset]; return $this->_models[$offset];
} }
public function offsetSet($offset, $value) public function offsetSet($offset, $value)
{ {
if (!$value instanceof $this->_modelClass) { if (!$value instanceof $this->_modelClass) {
...@@ -74,19 +74,19 @@ class Unl_Model_Collection implements Iterator, Countable, ArrayAccess ...@@ -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'); throw new Exception('Cannot add a non-ojbect to a collection of "' . $this->_modelClass . '" objects');
} }
} }
if ($offset === null) { if ($offset === null) {
return ($this->_models[] = $value); return ($this->_models[] = $value);
} else { } else {
return ($this->_models[$offset] = $value); return ($this->_models[$offset] = $value);
} }
} }
public function offsetUnset($offset) public function offsetUnset($offset)
{ {
unset($this->_models[$offset]); unset($this->_models[$offset]);
} }
public function merge(Unl_Model_Collection $collection) public function merge(Unl_Model_Collection $collection)
{ {
foreach ($collection as $model) { foreach ($collection as $model) {
...@@ -94,7 +94,7 @@ class Unl_Model_Collection implements Iterator, Countable, ArrayAccess ...@@ -94,7 +94,7 @@ class Unl_Model_Collection implements Iterator, Countable, ArrayAccess
$this->_models[$modelId] = $model; $this->_models[$modelId] = $model;
} }
} }
/** /**
* Removes and returns the last element from the collection * Removes and returns the last element from the collection
* *
...@@ -104,7 +104,7 @@ class Unl_Model_Collection implements Iterator, Countable, ArrayAccess ...@@ -104,7 +104,7 @@ class Unl_Model_Collection implements Iterator, Countable, ArrayAccess
{ {
return array_pop($this->_models); return array_pop($this->_models);
} }
public function getId() public function getId()
{ {
$ids = array(); $ids = array();
...@@ -113,5 +113,33 @@ class Unl_Model_Collection implements Iterator, Countable, ArrayAccess ...@@ -113,5 +113,33 @@ class Unl_Model_Collection implements Iterator, Countable, ArrayAccess
} }
return $ids; 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