Collection.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. public function filter(callable $f) {
  84. $this->objs = array_filter($this->objs, $f);
  85. return $this;
  86. }
  87. /**
  88. * @see \Gogs\Lib\ArrayIterator
  89. */
  90. public function limit(int $lim) {
  91. $this->objs = array_slice($this->objs, 0, $lim);
  92. return $this;
  93. }
  94. /**
  95. * @see \Gogs\Lib\ArrayIterator
  96. */
  97. public function offset(int $off) {
  98. $this->objs = array_slice($this->objs, $off);
  99. return $this;
  100. }
  101. /**
  102. * @see \Gogs\Lib\ArrayIterator
  103. */
  104. public function reverse() {
  105. $this->objs = array_reverse($this->objs);
  106. return $this;
  107. }
  108. /**
  109. * Remove an element in collection.
  110. *
  111. * The function will first look for the element as a
  112. * index key, but if its not found it will look for the
  113. * element as a value.
  114. *
  115. * Deep functions only when the value is given and not the key.
  116. *
  117. * @deprecated 0.1.1 Will be removed in future release
  118. * @param mixed $any Index key or element value
  119. * @param bool $deep Delete every item and not just the first
  120. * @return bool
  121. */
  122. public function remove($any, bool $deep = true) {
  123. if (isset($this->objs[$any])) {
  124. unset($this->objs[$any]);
  125. return true;
  126. } else if (in_array($any, $this->objs)) {
  127. $key = array_search($any, $this->objs, true);
  128. // No need to add deep ($key deletion)
  129. $val = $this->remove($key);
  130. if ($val && $deep) // Delete every object
  131. $this->remove($any, $deep);
  132. return $val;
  133. }
  134. return false;
  135. }
  136. }
  137. }