Collection.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. * @version 0.1.3
  10. */
  11. class Collection implements ArrayIterator {
  12. private $objs = array();
  13. public function __construct(array $arr = array()) {
  14. $this->objs = $arr;
  15. }
  16. /**
  17. * Set value(e) to the collection.
  18. *
  19. * If the value is an array it will overwrite
  20. * the whole object-array, aka everything.
  21. */
  22. public function set($val, $key = null) {
  23. if ($key == null && is_array($val))
  24. $this->objs = $val;
  25. else if ($key != null)
  26. $this->objs[$key] = $val;
  27. else
  28. array_push($this->objs, $val);
  29. }
  30. /**
  31. * @see ArrayIterator
  32. */
  33. public function by_key($idx) {
  34. return isset($this->objs[$idx]) ? $this->objs[$idx] : false;
  35. }
  36. public function copy() {
  37. return new Collection($this->all());
  38. }
  39. /**
  40. * @see ArrayIterator
  41. */
  42. public function all() {
  43. return $this->objs;
  44. }
  45. /**
  46. * @see ArrayIterator
  47. */
  48. public function len() {
  49. return count($this->objs);
  50. }
  51. /**
  52. * @see ArrayIterator
  53. */
  54. public function next() {
  55. return next($this->objs);
  56. }
  57. /**
  58. * @see ArrayIterator
  59. */
  60. public function prev() {
  61. return prev($this->objs);
  62. }
  63. /**
  64. * @see ArrayIterator
  65. */
  66. public function current() {
  67. return current($this->objs);
  68. }
  69. /**
  70. * @see ArrayIterator
  71. */
  72. public function reset() {
  73. return reset($this->objs);
  74. }
  75. /**
  76. * @see \Gogs\Lib\ArrayIterator
  77. */
  78. public function sort(callable $f) {
  79. if ($f == "" || $f == "ksort")
  80. return ksort($this->objs) ? $this : false;
  81. return uasort($this->objs, $f) ? $this : false;
  82. }
  83. /**
  84. * @see \Gogs\Lib\ArrayIterator
  85. */
  86. public function limit(int $lim) {
  87. $this->objs = array_slice($this->objs, 0, $lim);
  88. return $this;
  89. }
  90. /**
  91. * @see \Gogs\Lib\ArrayIterator
  92. */
  93. public function offset(int $off) {
  94. $this->objs = array_slice($this->objs, $off);
  95. return $this;
  96. }
  97. /**
  98. * @see \Gogs\Lib\ArrayIterator
  99. */
  100. public function reverse() {
  101. $this->objs = array_reverse($this->objs);
  102. return $this;
  103. }
  104. /**
  105. * Remove an element in collection.
  106. *
  107. * The function will first look for the element as a
  108. * index key, but if its not found it will look for the
  109. * element as a value.
  110. *
  111. * Deep functions only when the value is given and not the key.
  112. *
  113. * @deprecated 0.1.1 Will be removed in future release
  114. * @param mixed $any Index key or element value
  115. * @param bool $deep Delete every item and not just the first
  116. * @return bool
  117. */
  118. public function remove($any, bool $deep = true) {
  119. if (isset($this->objs[$any])) {
  120. unset($this->objs[$any]);
  121. return true;
  122. } else if (in_array($any, $this->objs)) {
  123. $key = array_search($any, $this->objs, true);
  124. // No need to add deep ($key deletion)
  125. $val = $this->remove($key);
  126. if ($val && $deep) // Delete every object
  127. $this->remove($any, $deep);
  128. return $val;
  129. }
  130. return false;
  131. }
  132. }
  133. }