From 01bc363ac789cfb9d644ce82a949da5cd7e1c220 Mon Sep 17 00:00:00 2001 From: xue <> Date: Tue, 31 Jan 2006 00:01:49 +0000 Subject: Modified TList and TMap implementation so that they can be more easily extended. --- framework/Collections/TAttributeCollection.php | 50 +++++++++++- framework/Collections/TList.php | 108 +++++-------------------- framework/Collections/TMap.php | 71 ++-------------- 3 files changed, 70 insertions(+), 159 deletions(-) (limited to 'framework/Collections') diff --git a/framework/Collections/TAttributeCollection.php b/framework/Collections/TAttributeCollection.php index c8870919..abeb3c53 100644 --- a/framework/Collections/TAttributeCollection.php +++ b/framework/Collections/TAttributeCollection.php @@ -54,7 +54,6 @@ class TAttributeCollection extends TMap */ public function __get($name) { - $name=strtolower($name); return $this->contains($name)?$this->itemAt($name):parent::__get($name); } @@ -68,7 +67,52 @@ class TAttributeCollection extends TMap */ public function __set($name,$value) { - $this->add(strtolower($name),$value); + $this->add($name,$value); + } + + /** + * Returns the item with the specified key. + * This overrides the parent implementation by converting the key to lower case first. + * @param mixed the key + * @return mixed the element at the offset, null if no element is found at the offset + */ + public function itemAt($key) + { + return parent::itemAt(strtolower($key)); + } + + + /** + * Adds an item into the map. + * This overrides the parent implementation by converting the key to lower case first. + * @param mixed key + * @param mixed value + */ + public function add($key,$value) + { + parent::add(strtolower($key),$value); + } + + /** + * Removes an item from the map by its key. + * This overrides the parent implementation by converting the key to lower case first. + * @param mixed the key of the item to be removed + * @return mixed the removed value, null if no such key exists. + */ + public function remove($key) + { + return parent::remove(strtolower($key)); + } + + /** + * Returns whether the specified is in the map. + * This overrides the parent implementation by converting the key to lower case first. + * @param mixed the key + * @return boolean whether the map contains an item with the specified key + */ + public function contains($key) + { + return parent::contains(strtolower($key)); } /** @@ -80,7 +124,6 @@ class TAttributeCollection extends TMap */ public function hasProperty($name) { - $name=strtolower($name); return $this->contains($name) || parent::hasProperty($name); } @@ -93,7 +136,6 @@ class TAttributeCollection extends TMap */ public function canGetProperty($name) { - $name=strtolower($name); return $this->contains($name) || parent::canGetProperty($name); } diff --git a/framework/Collections/TList.php b/framework/Collections/TList.php index 2946053b..f2628231 100644 --- a/framework/Collections/TList.php +++ b/framework/Collections/TList.php @@ -29,10 +29,8 @@ * Note, count($list) will always return 1. You should use {@link getCount()} * to determine the number of items in the list. * - * To extend TList by doing additional operations with each added or removed item, - * you can override {@link addedItem} and {@link removedItem}. - * You can also override {@link canAddItem} and {@link canRemoveItem} to - * control whether to add or remove a particular item. + * To extend TList by doing additional operations with each addition or removal + * operations, override {@link insertAt()}, and {@link removeAt()}. * * @author Qiang Xue * @version $Revision: $ $Date: $ @@ -101,17 +99,10 @@ class TList extends TComponent implements IteratorAggregate,ArrayAccess /** * Appends an item at the end of the list. * @param mixed new item - * @throws TInvalidOperationException If the item is not allowed to be added */ public function add($item) { - if($this->canAddItem($item)) - { - $this->_d[$this->_c++]=$item; - $this->addedItem($item); - } - else - throw new TInvalidOperationException('list_addition_disallowed'); + $this->insertAt($this->_c,$item); } /** @@ -121,26 +112,18 @@ class TList extends TComponent implements IteratorAggregate,ArrayAccess * @param integer the speicified position. * @param mixed new item * @throws TInvalidDataValueException If the index specified exceeds the bound - * @throws TInvalidOperationException If the item is not allowed to be added */ - public function insert($index,$item) + public function insertAt($index,$item) { - if($this->canAddItem($item)) + if($index===$this->_c) + $this->_d[$this->_c++]=$item; + else if($index>=0 && $index<$this->_c) { - if($index===$this->_c) - $this->add($item); - else if($index>=0 && $index<$this->_c) - { - array_splice($this->_d,$index,0,array($item)); - $this->_c++; - $this->addedItem($item); - } - else - throw new TInvalidDataValueException('list_index_invalid',$index); + array_splice($this->_d,$index,0,array($item)); + $this->_c++; } else - throw new TInvalidOperationException('list_addition_disallowed'); - + throw new TInvalidDataValueException('list_index_invalid',$index); } /** @@ -148,18 +131,12 @@ class TList extends TComponent implements IteratorAggregate,ArrayAccess * The list will first search for the item. * The first item found will be removed from the list. * @param mixed the item to be removed. - * @throws TInvalidOperationException If the item cannot be removed * @throws TInvalidDataValueException If the item does not exist */ public function remove($item) { if(($index=$this->indexOf($item))>=0) - { - if($this->canRemoveItem($item)) - $this->removeAt($index); - else - throw new TInvalidOperationException('list_item_unremovable'); - } + $this->removeAt($index); else throw new TInvalidDataValueException('list_item_inexistent'); } @@ -169,25 +146,18 @@ class TList extends TComponent implements IteratorAggregate,ArrayAccess * @param integer the index of the item to be removed. * @return mixed the removed item. * @throws TOutOfRangeException If the index specified exceeds the bound - * @throws TInvalidOperationException If the item cannot be removed */ public function removeAt($index) { if(isset($this->_d[$index])) { $item=$this->_d[$index]; - if($this->canRemoveItem($item)) - { - if($index===$this->_c-1) - unset($this->_d[$index]); - else - array_splice($this->_d,$index,1); - $this->_c--; - $this->removedItem($item); - return $item; - } + if($index===$this->_c-1) + unset($this->_d[$index]); else - throw new TInvalidOperationException('list_item_unremovable'); + array_splice($this->_d,$index,1); + $this->_c--; + return $item; } else throw new TInvalidDataValueException('list_index_invalid',$index); @@ -303,11 +273,11 @@ class TList extends TComponent implements IteratorAggregate,ArrayAccess public function offsetSet($offset,$item) { if($offset===null || $offset===$this->_c) - $this->add($item); + $this->insertAt($this->_c,$item); else { $this->removeAt($offset); - $this->insert($offset,$item); + $this->insertAt($offset,$item); } } @@ -321,48 +291,6 @@ class TList extends TComponent implements IteratorAggregate,ArrayAccess { $this->removeAt($offset); } - - /** - * This method is invoked after an item is successfully added to the list. - * You can override this method to provide customized processing of the addition. - * @param mixed the newly added item - */ - protected function addedItem($item) - { - } - - /** - * This method is invoked after an item is successfully removed from the list. - * You can override this method to provide customized processing of the removal. - * @param mixed the removed item - */ - protected function removedItem($item) - { - } - - /** - * This method is invoked before adding an item to the list. - * If it returns true, the item will be added to the list, otherwise not. - * You can override this method to decide whether a specific can be added. - * @param mixed item to be added - * @return boolean whether the item can be added to the list - */ - protected function canAddItem($item) - { - return true; - } - - /** - * This method is invoked before removing an item from the list. - * If it returns true, the item will be removed from the list, otherwise not. - * You can override this method to decide whether a specific can be removed. - * @param mixed item to be removed - * @return boolean whether the item can be removed to the list - */ - protected function canRemoveItem($item) - { - return true; - } } diff --git a/framework/Collections/TMap.php b/framework/Collections/TMap.php index d5a44322..be37a46b 100644 --- a/framework/Collections/TMap.php +++ b/framework/Collections/TMap.php @@ -100,19 +100,12 @@ class TMap extends TComponent implements IteratorAggregate,ArrayAccess * Note, if the specified key already exists, the old value will be removed first. * @param mixed key * @param mixed value - * @throws TInvalidOperationException if the item cannot be added */ public function add($key,$value) { - if($this->canAddItem($key,$value)) - { - if(isset($this->_d[$key])) - $this->remove($key); - $this->_d[$key]=$value; - $this->addedItem($key,$value); - } - else - throw new TInvalidOperationException('map_addition_disallowed'); + if(isset($this->_d[$key])) + $this->remove($key); + $this->_d[$key]=$value; } /** @@ -126,14 +119,8 @@ class TMap extends TComponent implements IteratorAggregate,ArrayAccess if(isset($this->_d[$key])) { $value=$this->_d[$key]; - if($this->canRemoveItem($key,$value)) - { - unset($this->_d[$key]); - $this->removedItem($key,$value); - return $value; - } - else - throw new TInvalidOperationException('map_item_unremovable'); + unset($this->_d[$key]); + return $value; } else return null; @@ -220,7 +207,7 @@ class TMap extends TComponent implements IteratorAggregate,ArrayAccess */ public function offsetGet($offset) { - return isset($this->_d[$offset]) ? $this->_d[$offset] : null; + return $this->itemAt($offset); } /** @@ -243,52 +230,6 @@ class TMap extends TComponent implements IteratorAggregate,ArrayAccess { $this->remove($offset); } - - /** - * This method is invoked after an item is successfully added to the map. - * You can override this method provide customized processing of the addition. - * @param string key to the item - * @param mixed the newly added item - */ - protected function addedItem($key,$item) - { - } - - /** - * This method is invoked after an item is successfully removed from the map. - * You can override this method provide customized processing of the removal. - * @param string key to the item - * @param mixed the removed item - */ - protected function removedItem($key,$item) - { - } - - /** - * This method is invoked before adding an item to the map. - * If it returns true, the item will be added to the map, otherwise not. - * You can override this method to decide whether a specific can be added. - * @param string key to the item - * @param mixed item to be added - * @return boolean whether the item can be added to the map - */ - protected function canAddItem($key,$item) - { - return true; - } - - /** - * This method is invoked before removing an item from the map. - * If it returns true, the item will be removed from the map, otherwise not. - * You can override this method to decide whether a specific can be removed. - * @param string key to the item - * @param mixed item to be removed - * @return boolean whether the item can be removed to the map - */ - protected function canRemoveItem($key,$item) - { - return true; - } } /** -- cgit v1.2.3