ModelCollection.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. class ModelCollection implements Iterator, ArrayAccess, Countable
  3. {
  4. /**
  5. * @var array|AbstractModel[]
  6. */
  7. private $models = array();
  8. /**
  9. * Constructor.
  10. *
  11. * @param array|AbstractModel[] $array
  12. */
  13. public function __construct($array = array())
  14. {
  15. if($this->isNumericArray($array)){
  16. foreach($array as $model){
  17. $this->add($model);
  18. }
  19. }
  20. else{
  21. foreach($array as $key => $model){
  22. $this->add($model, $key);
  23. }
  24. }
  25. }
  26. /**
  27. * @param array $array
  28. *
  29. * @return bool
  30. */
  31. protected function isNumericArray($array)
  32. {
  33. return array_keys($array) === range(0, count($array) - 1)
  34. && count(array_filter($array, 'is_string')) === 0;
  35. }
  36. /**
  37. * Adds a model to the collection,
  38. * but will not replace if it exists with that key
  39. *
  40. * @param AbstractModel $model
  41. * @param mixed|null $key
  42. */
  43. public function add($model, $key = null)
  44. {
  45. if(is_null($model) || !($model instanceof AbstractModel)){
  46. return;
  47. }
  48. if(is_null($key)){
  49. $this->models[] = $model;
  50. }
  51. elseif(!$this->has($key)){
  52. $this->models[$key] = $model;
  53. }
  54. }
  55. /**
  56. * Replace a model with given key
  57. *
  58. * @param AbstractModel $model
  59. * @param mixed $key
  60. */
  61. public function replace($model, $key)
  62. {
  63. if(is_null($model) || !($model instanceof AbstractModel)){
  64. return;
  65. }
  66. $model[$key] = $model;
  67. }
  68. /**
  69. * Delete a model by key
  70. *
  71. * @param mixed $key
  72. */
  73. public function delete($key)
  74. {
  75. if($this->has($key)){
  76. unset($this->models[$key]);
  77. }
  78. }
  79. /**
  80. * Check if collection has a model by key
  81. *
  82. * @param mixed $key
  83. *
  84. * @return bool
  85. */
  86. public function has($key)
  87. {
  88. return isset($this->models[$key]);
  89. }
  90. /**
  91. * Get a model from the collection by key
  92. *
  93. * @param mixed $key
  94. *
  95. * @return AbstractModel|null
  96. */
  97. public function get($key)
  98. {
  99. if($this->has($key)){
  100. return $this->models[$key];
  101. }
  102. return null;
  103. }
  104. /**
  105. * Search a model in collection with a condition
  106. *
  107. * @param callable $callable Gives back if the search matches
  108. *
  109. * @return AbstractModel|null
  110. */
  111. public function search($callable)
  112. {
  113. if(is_callable($callable)){
  114. foreach($this->models as $model){
  115. if($callable($model)){
  116. return $model;
  117. }
  118. }
  119. }
  120. return null;
  121. }
  122. /**
  123. * Search all models in collection with a condition
  124. *
  125. * @param callable $callable Gives back if the search matches
  126. *
  127. * @return static
  128. */
  129. public function searchAll($callable)
  130. {
  131. $collection = new static;
  132. if(is_callable($callable)){
  133. foreach($this->models as $model){
  134. if($callable($model)){
  135. $collection->add($model);
  136. }
  137. }
  138. }
  139. return $collection;
  140. }
  141. /**
  142. * @inheritdoc
  143. */
  144. public function current()
  145. {
  146. return current($this->models);
  147. }
  148. /**
  149. * @inheritdoc
  150. */
  151. public function next()
  152. {
  153. return next($this->models);
  154. }
  155. /**
  156. * @inheritdoc
  157. */
  158. public function key()
  159. {
  160. return key($this->models);
  161. }
  162. /**
  163. * @inheritdoc
  164. */
  165. public function valid()
  166. {
  167. return $this->current() !== false;
  168. }
  169. /**
  170. * @inheritdoc
  171. */
  172. public function rewind()
  173. {
  174. reset($this->models);
  175. }
  176. /**
  177. * @inheritdoc
  178. */
  179. public function offsetExists($offset)
  180. {
  181. return $this->has($offset);
  182. }
  183. /**
  184. * @inheritdoc
  185. */
  186. public function offsetGet($offset)
  187. {
  188. return $this->get($offset);
  189. }
  190. /**
  191. * @inheritdoc
  192. */
  193. public function offsetSet($offset, $value)
  194. {
  195. $this->add($value, $offset);
  196. }
  197. /**
  198. * @inheritdoc
  199. */
  200. public function offsetUnset($offset)
  201. {
  202. $this->delete($offset);
  203. }
  204. /**
  205. * @inheritdoc
  206. */
  207. public function count()
  208. {
  209. return count($this->models);
  210. }
  211. }