123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- <?php
- class ModelCollection implements Iterator, ArrayAccess, Countable
- {
- /**
- * @var array|AbstractModel[]
- */
- private $models = array();
- /**
- * Constructor.
- *
- * @param array|AbstractModel[] $array
- */
- public function __construct($array = array())
- {
- if($this->isNumericArray($array)){
- foreach($array as $model){
- $this->add($model);
- }
- }
- else{
- foreach($array as $key => $model){
- $this->add($model, $key);
- }
- }
- }
- /**
- * @param array $array
- *
- * @return bool
- */
- protected function isNumericArray($array)
- {
- return array_keys($array) === range(0, count($array) - 1)
- && count(array_filter($array, 'is_string')) === 0;
- }
- /**
- * Adds a model to the collection,
- * but won't replace if it exists with that key
- *
- * @param AbstractModel $model
- * @param mixed|null $key
- */
- public function add($model, $key = null)
- {
- if(is_null($model) || !($model instanceof AbstractModel)){
- return;
- }
- if(is_null($key)){
- $this->models[] = $model;
- }
- elseif(!$this->has($key)){
- $this->models[$key] = $model;
- }
- }
- /**
- * Replace a model with given key
- *
- * @param AbstractModel $model
- * @param mixed $key
- */
- public function replace($model, $key)
- {
- if(is_null($model) || !($model instanceof AbstractModel)){
- return;
- }
- $model[$key] = $model;
- }
- /**
- * Delete a model by key
- *
- * @param mixed $key
- */
- public function delete($key)
- {
- if($this->has($key)){
- unset($this->models[$key]);
- }
- }
- /**
- * Check if collection has a model by key
- *
- * @param mixed $key
- *
- * @return bool
- */
- public function has($key)
- {
- return isset($this->models[$key]);
- }
- /**
- * Get a model from the collection by key
- *
- * @param mixed $key
- *
- * @return AbstractModel|null
- */
- public function get($key)
- {
- if($this->has($key)){
- return $this->models[$key];
- }
- return null;
- }
- /**
- * Search a model in collection with a condition
- *
- * @param callable $callable Gives back if the search matches
- *
- * @return AbstractModel|null
- */
- public function search($callable)
- {
- if(is_callable($callable)){
- foreach($this->models as $model){
- if($callable($model)){
- return $model;
- }
- }
- }
- return null;
- }
- /**
- * Search all models in collection with a condition
- *
- * @param callable $callable Gives back if the search matches
- *
- * @return static
- */
- public function searchAll($callable)
- {
- $collection = new static;
- if(is_callable($callable)){
- foreach($this->models as $model){
- if($callable($model)){
- $collection->add($model);
- }
- }
- }
- return $collection;
- }
- /**
- * Convert models to an array of strings
- *
- * @param callable $callable Gives back a string for a model
- *
- * @return array|string[]
- */
- public function toStringArray($callable)
- {
- $strings = array();
- if(is_callable($callable)){
- foreach($this->models as $model){
- $strings[] = $callable($model);
- }
- }
- return $strings;
- }
- /**
- * @inheritdoc
- */
- public function current()
- {
- return current($this->models);
- }
- /**
- * @inheritdoc
- */
- public function next()
- {
- return next($this->models);
- }
- /**
- * @inheritdoc
- */
- public function key()
- {
- return key($this->models);
- }
- /**
- * @inheritdoc
- */
- public function valid()
- {
- return $this->current() !== false;
- }
- /**
- * @inheritdoc
- */
- public function rewind()
- {
- reset($this->models);
- }
- /**
- * @inheritdoc
- */
- public function offsetExists($offset)
- {
- return $this->has($offset);
- }
- /**
- * @inheritdoc
- */
- public function offsetGet($offset)
- {
- return $this->get($offset);
- }
- /**
- * @inheritdoc
- */
- public function offsetSet($offset, $value)
- {
- $this->add($value, $offset);
- }
- /**
- * @inheritdoc
- */
- public function offsetUnset($offset)
- {
- $this->delete($offset);
- }
- /**
- * @inheritdoc
- */
- public function count()
- {
- return count($this->models);
- }
- }
|