Collection.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace Gogs\API\Request {
  3. abstract class Collection extends Base implements \Gogs\Lib\ArrayIterator {
  4. private $objs = array();
  5. public function add($obj, $key = null) {
  6. if (!isset($key))
  7. array_push($this->objs, $obj);
  8. else
  9. $this->objs[$key] = $obj;
  10. }
  11. public function remove($any) {
  12. if (isset($this->objs[$any])) {
  13. unset($this->objs[$any]);
  14. return true;
  15. } else if (in_array($any, $this->objs)) {
  16. $key = array_search($any, $this->objs, true);
  17. return $this->remove($key);
  18. }
  19. return false;
  20. }
  21. public function all() {
  22. return $this->objs;
  23. }
  24. public function len() {
  25. return count($this->objs);
  26. }
  27. public function by_key($idx) {
  28. return isset($this->objs[$idx]) ? $this->objs[$idx] : false;
  29. }
  30. public function next() {
  31. return next($this->objs);
  32. }
  33. public function prev() {
  34. return prev($this->objs);
  35. }
  36. public function current() {
  37. return current($this->objs);
  38. }
  39. public function reset() {
  40. return reset($this->objs);
  41. }
  42. /**
  43. * Search for an object.
  44. *
  45. * @param array $params Parameters
  46. * @return Gogs\Lib\Collection
  47. */
  48. abstract public function search(array $params = array());
  49. }
  50. }
  51. ?>