Collection.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace Gogs\Lib {
  3. /**
  4. * Base class for collections. Implements basic
  5. * functions and typically used to return collections
  6. * which wont be a part of the "request package"
  7. *
  8. * @author Joachim M. Giaever (joachim[]giaever.org)
  9. */
  10. class Collection implements ArrayIterator {
  11. private $objs = array();
  12. /**
  13. * Set value(e) to the collection.
  14. *
  15. * If the value is an array it will overwrite
  16. * the whole object-array, aka everything.
  17. */
  18. public function set($val, $key = null) {
  19. if ($key == null && is_array($val))
  20. $this->objs = $val;
  21. else if ($key != null)
  22. $this->objs[$key] = $val;
  23. else
  24. array_push($this->objs, $val);
  25. }
  26. /**
  27. * @see ArrayIterator
  28. */
  29. public function by_key($idx) {
  30. return isset($this->objs[$idx]) ? $this->objs[$idx] : false;
  31. }
  32. /**
  33. * @see ArrayIterator
  34. */
  35. public function all() {
  36. return $this->objs;
  37. }
  38. /**
  39. * @see ArrayIterator
  40. */
  41. public function len() {
  42. return count($this->objs);
  43. }
  44. /**
  45. * @see ArrayIterator
  46. */
  47. public function next() {
  48. return next($this->objs);
  49. }
  50. /**
  51. * @see ArrayIterator
  52. */
  53. public function prev() {
  54. return prev($this->objs);
  55. }
  56. /**
  57. * @see ArrayIterator
  58. */
  59. public function current() {
  60. return current($this->objs);
  61. }
  62. /**
  63. * @see ArrayIterator
  64. */
  65. public function reset() {
  66. return reset($this->objs);
  67. }
  68. }
  69. }