Created
August 12, 2011 05:25
-
-
Save showsky/1141516 to your computer and use it in GitHub Desktop.
classCollection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* | |
* 物件集合處理 | |
* @author Cheng-Ting | |
* | |
*/ | |
class Collection | |
{ | |
private $object; | |
public function __construct(){} | |
/** | |
* | |
* 新增物件到集合裡面 | |
* @param Object $object | |
* @param Mixed $key | |
* @throws Exception | |
*/ | |
public function addItem($object, $key = NULL) | |
{ | |
if(!is_object($object)) throw new Exception('it not object'); | |
if(!is_null($key) && !array_key_exists($key, $this->object)) | |
{ | |
$this->object[$key] = $object; | |
}else{ | |
$this->object[] = $object; | |
} | |
} | |
/** | |
* | |
* 刪除集合裡面的物件 | |
* @param Mixed $key | |
* @throws Exception | |
*/ | |
public function removeItem($key = NULL) | |
{ | |
if(!is_null($key) && array_key_exists($key, $this->object)) | |
{ | |
unset($this->object[$key]); | |
}else{ | |
throw new Exception('not search $key'); | |
} | |
} | |
/** | |
* | |
* 取得集合裡面的物件 | |
* @param Mixed $key | |
* @throws Exception | |
*/ | |
public function getItem($key = NULL) | |
{ | |
if(!is_null($key) && array_key_exists($key, $this->object)) | |
{ | |
return $this->object[$key]; | |
}else{ | |
throw new Exception('not search '.$key); | |
} | |
} | |
/** | |
* | |
* 取得內部所有集合物件 | |
* @return Array | |
*/ | |
public function __toString() | |
{ | |
var_dump($this->object); | |
} | |
/** | |
* | |
* 取回集合物件的 索引 | |
* @return Array | |
*/ | |
public function keys() | |
{ | |
return array_keys($this->object); | |
} | |
} | |
/** | |
* | |
* 實作物件 foreach 功能 | |
* @author Cheng-Ting | |
* | |
*/ | |
class cIterator implements Iterator | |
{ | |
private $array; | |
private $collection; | |
private $index = 0; | |
/** | |
* | |
* 建立 Iterator 處理 | |
* @param Collection $object | |
*/ | |
public function __construct(Collection $object) | |
{ | |
$this->array = $object->keys(); | |
$this->collection = $object; | |
} | |
/** | |
* | |
* 重新設定物件集合 | |
* @param String $name | |
* @param Collection $value | |
*/ | |
public function __set($name, Collection $value) | |
{ | |
if($name == 'collection') | |
{ | |
$this->collection = $value; | |
$this->array = $value->keys(); | |
} | |
} | |
public function rewind() | |
{ | |
$this->index = 0; | |
} | |
public function next() | |
{ | |
$this->index++; | |
} | |
public function key() | |
{ | |
return $this->array[$this->index]; | |
} | |
public function current() | |
{ | |
return $this->collection->getItem($this->array[$this->index]); | |
} | |
public function valid() | |
{ | |
return isset($this->array[$this->index]); | |
} | |
} | |
/** | |
* | |
* 書物件 範例 | |
* @author Cheng-Ting | |
* | |
*/ | |
class book | |
{ | |
private $title; | |
public function __construct($title) | |
{ | |
$this->title = $title; | |
} | |
public function __toString() | |
{ | |
echo $this->title; | |
} | |
} | |
/* | |
* 以下為使用範例 | |
*/ | |
try { | |
$collcection = new Collection(); | |
$data = array('PHP','Mysql','Linux','JS','as'); | |
foreach($data as $row) | |
{ | |
$collcection->addItem(new book($row)); //建立物件到集合裡面 | |
} | |
$collcection->addItem(new book('ok'), 'ok'); //建立物件到集合裡面使用自訂索引 | |
$iterator = new cIterator($collcection); | |
foreach($iterator as $key => $values) //使用foreach方式取回整個集合物件 | |
{ | |
var_dump($values); | |
} | |
} catch (Exception $e) { | |
echo $e->getMessage(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment