From 398a5b9e92c846872161c39b5bcd433a8f045158 Mon Sep 17 00:00:00 2001 From: rMalie Date: Tue, 6 Dec 2011 10:12:02 +0000 Subject: [PATCH] // Collections can be used as arrays --- classes/Collection.php | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/classes/Collection.php b/classes/Collection.php index 1f4cf959d..8f5c7944d 100644 --- a/classes/Collection.php +++ b/classes/Collection.php @@ -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'); + } } \ No newline at end of file