123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- <?php
- namespace Gogs\API\Request {
- /**
- * Collection is a collection of data of one type.
- *
- * @see Users
- * @author Joachim M. Giaever (joachim[]giaever.org)
- * @version 0.1
- */
- abstract class Collection extends Base implements \Gogs\Lib\ArrayIterator {
- private $objs = array();
- /**
- * Add an object to the collection.
- *
- * When adding a key the object will be stored
- * on the particual key, also overwriting existing data.
- *
- * @param mixed $obj Element to store
- * @param mixed $key Index key to store on
- * @return mixed|int The index key. If key is null the returned value will be an integer.
- */
- public function add($obj, $key = null) {
- if (!isset($key))
- array_push($this->objs, $obj);
- else
- $this->objs[$key] = $obj;
- return $key == null ? $this->len - 1 : $key;
- }
- /**
- * Remove an element in collection.
- *
- * The function will first look for the element as a
- * index key, but if its not found it will look for the
- * element as a value.
- *
- * Deep functions only when the value is given and not the key.
- *
- * @param mixed $any Index key or element value
- * @param bool $deep Delete every item and not just the first
- * @return bool
- */
- public function remove($any, bool $deep = true) {
- if (isset($this->objs[$any])) {
- unset($this->objs[$any]);
- return true;
- } else if (in_array($any, $this->objs)) {
- $key = array_search($any, $this->objs, true);
- // No need to add deep ($key deletion)
- $val = $this->remove($key);
- if ($val && $deep) // Delete every object
- $this->remove($any, $deep);
- return $val;
- }
- return false;
- }
- /**
- * @see \Gogs\Lib\ArrayIterator
- */
- public function all() {
- return $this->objs;
- }
- /**
- * @see \Gogs\Lib\ArrayIterator
- */
- public function len() {
- return count($this->objs);
- }
- /**
- * @see \Gogs\Lib\ArrayIterator
- */
- public function by_key($idx) {
- return isset($this->objs[$idx]) ? $this->objs[$idx] : false;
- }
- /**
- * @see \Gogs\Lib\ArrayIterator
- */
- public function next() {
- return next($this->objs);
- }
- /**
- * @see \Gogs\Lib\ArrayIterator
- */
- public function prev() {
- return prev($this->objs);
- }
- /**
- * @see \Gogs\Lib\ArrayIterator
- */
- public function current() {
- return current($this->objs);
- }
- /**
- * @see \Gogs\Lib\ArrayIterator
- */
- public function reset() {
- return reset($this->objs);
- }
- /**
- * Search for an object.
- *
- * @param array $params Parameters
- * @return \Gogs\Lib\Collection
- */
- abstract public function search(array $params = array());
- }
- }
- ?>
|