ModelCollection.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 won't 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. * Convert models to an array of strings
  143. *
  144. * @param callable $callable Gives back a string for a model
  145. *
  146. * @return array|string[]
  147. */
  148. public function toStringArray($callable)
  149. {
  150. $strings = array();
  151. if(is_callable($callable)){
  152. foreach($this->models as $model){
  153. $strings[] = $callable($model);
  154. }
  155. }
  156. return $strings;
  157. }
  158. /**
  159. * @inheritdoc
  160. */
  161. public function current()
  162. {
  163. return current($this->models);
  164. }
  165. /**
  166. * @inheritdoc
  167. */
  168. public function next()
  169. {
  170. return next($this->models);
  171. }
  172. /**
  173. * @inheritdoc
  174. */
  175. public function key()
  176. {
  177. return key($this->models);
  178. }
  179. /**
  180. * @inheritdoc
  181. */
  182. public function valid()
  183. {
  184. return $this->current() !== false;
  185. }
  186. /**
  187. * @inheritdoc
  188. */
  189. public function rewind()
  190. {
  191. reset($this->models);
  192. }
  193. /**
  194. * @inheritdoc
  195. */
  196. public function offsetExists($offset)
  197. {
  198. return $this->has($offset);
  199. }
  200. /**
  201. * @inheritdoc
  202. */
  203. public function offsetGet($offset)
  204. {
  205. return $this->get($offset);
  206. }
  207. /**
  208. * @inheritdoc
  209. */
  210. public function offsetSet($offset, $value)
  211. {
  212. $this->add($value, $offset);
  213. }
  214. /**
  215. * @inheritdoc
  216. */
  217. public function offsetUnset($offset)
  218. {
  219. $this->delete($offset);
  220. }
  221. /**
  222. * @inheritdoc
  223. */
  224. public function count()
  225. {
  226. return count($this->models);
  227. }
  228. }