// Collections can be used as arrays

This commit is contained in:
rMalie
2011-12-06 10:12:02 +00:00
parent 9bc2b20bbf
commit 398a5b9e92
+37 -1
View File
@@ -30,7 +30,7 @@
*
* @since 1.5.0
*/
class CollectionCore implements Iterator, Countable
class CollectionCore implements Iterator, ArrayAccess, Countable
{
/**
* @var string Object class name
@@ -202,4 +202,40 @@ class CollectionCore implements Iterator, Countable
$this->getAll();
return count($this->results);
}
/**
* Check if a result exist
*
* @param $offset
* @return bool
*/
public function offsetExists($offset)
{
$this->getAll();
return isset($this->results[$offset]);
}
/**
* Get a result by offset
*
* @param $offset
* @return ObjectModel
*/
public function offsetGet($offset)
{
$this->getAll();
if (!isset($this->results[$offset]))
throw new PrestashopException('Unknown offset '.$offset.' for collection '.$this->classname);
return $this->results[$offset];
}
public function offsetSet($offset, $value)
{
throw new PrestashopException('It is forbidden to override a result in a collection');
}
public function offsetUnset($offset)
{
throw new PrestashopException('It is forbidden to unset a result in a collection');
}
}