Ejemplo n.º 1
0
 /**
  * Sets default model
  *
  * @param string
  * @return this
  */
 public function setModel($model)
 {
     $error = Eden_Collection_Error::i()->argument(1, 'string');
     if (!is_subclass_of($model, 'Eden_Model')) {
         $error->setMessage(Eden_Collection_Error::NOT_SUB_MODEL)->addVariable($model)->trigger();
     }
     $this->_model = $model;
     return $this;
 }
Ejemplo n.º 2
0
if(!class_exists('Eden_Collection')){class Eden_Collection extends Eden_Class implements ArrayAccess,Iterator,Serializable,Countable{const FIRST='first';const LAST='last';const MODEL='Eden_Model';protected $_list=array();protected $_model=self::MODEL;public static function i(){return self::_getMultiple(__CLASS__);}public function __call($name,$args){if(strpos($name,'get')===0){$value=isset($args[0]) ? $args[0] : NULL;$list=array();foreach($this->_list as $i=>$row){$list[]=$row->$name(isset($args[0]) ? $args[0] : NULL);}return $list;}else if (strpos($name,'set')===0){$value=isset($args[0]) ? $args[0] : NULL;$separator=isset($args[1]) ? $args[1] : NULL;foreach($this->_list as $i=>$row){$row->$name($value,$separator);}return $this;}$found=false;foreach($this->_list as $i=>$row){if(!method_exists($row,$name)){continue;}$found=true;$row->callThis($name,$args);}if($found){return $this;}try{return parent::__call($name,$args);}catch(Eden_Error $e){Eden_Collection_Error::i($e->getMessage())->trigger();}}public function __construct(array $data=array()){$this->set($data);}public function __set($name,$value){foreach($this->_list as $i=>$row){$row[$name]=$value;}return $this;}public function __toString(){return json_encode($this->get());}public function add($row=array()){Eden_Collection_Error::i()->argument(1,'array',$this->_model);if(is_array($row)){$model=$this->_model;$row=$this->$model($row);}$this->_list[]=$row;return $this;}public function count(){return count($this->_list);}public function cut($index=self::LAST){Eden_Collection_Error::i()->argument(1,'string','int');if($index==self::FIRST){$index=0;}else if($index==self::LAST){$index=count($this->_list) -1;}if(isset($this->_list[$index])){unset($this->_list[$index]);}$this->_list=array_values($this->_list);return $this;}public function each($callback){Eden_Error::i()->argument(1,'callable');foreach($this->_list as $key=>$value){call_user_func($callback,$key,$value);}return $this;}public function current(){return current($this->_list);}public function get($modified=true){Eden_Collection_Error::i()->argument(1,'bool');$array=array();foreach($this->_list as $i=>$row){$array[$i]=$row->get($modified);}return $array;}public function key(){return key($this->_list);}public function next(){next($this->_list);}public function offsetExists($offset){return isset($this->_list[$offset]);}public function offsetGet($offset){return isset($this->_list[$offset]) ? $this->_list[$offset] : NULL;}public function offsetSet($offset,$value){Eden_Collection_Error::i()->argument(2,'array',$this->_model);if(is_array($value)){$model=$this->_model;$value=$this->$model($value);}if (is_null($offset)){$this->_list[]=$value;}else{$this->_list[$offset]=$value;}}public function offsetUnset($offset){$this->_list=Eden_Model::i($this->_list)->cut($offset)->get();}public function rewind(){reset($this->_list);}public function serialize(){return $this->__toString();}public function set(array $data=array()){foreach($data as $row){$this->add($row);}return $this;}public function setModel($model){$error=Eden_Collection_Error::i()->argument(1,'string');if(!is_subclass_of($model,'Eden_Model')){$error->setMessage(Eden_Collection_Error::NOT_SUB_MODEL)->addVariable($model)->trigger();}$this->_model=$model;return $this;}public function unserialize($data){$this->_list=json_decode($data,true);return $this;}public function valid(){return isset($this->_list[key($this->_list)]);}}class Eden_Collection_Error extends Eden_Error{const NOT_COLLECTION='The data passed into __construct is not a collection.';const NOT_SUB_MODEL='Class %s is not a child of Eden_Model';public static function i($message=NULL,$code=0){$class=__CLASS__;return new $class($message,$code);}}}