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. --- .../protected/pages/Advanced/Samples/I18N/Home.php | 2 +- framework/Collections/TAttributeCollection.php | 50 ++++++++- framework/Collections/TList.php | 108 ++++--------------- framework/Collections/TMap.php | 71 ++----------- framework/Data/TXmlDocument.php | 47 ++++---- framework/Exceptions/messages.txt | 22 +++- framework/Security/TAuthorizationRule.php | 18 ++-- framework/TComponent.php | 4 +- framework/Web/THttpRequest.php | 42 ++++---- framework/Web/UI/TControl.php | 43 ++++---- framework/Web/UI/TTemplateControl.php | 2 +- framework/Web/UI/WebControls/TDataGrid.php | 34 +++--- framework/Web/UI/WebControls/TDataList.php | 17 +-- framework/Web/UI/WebControls/TDataSourceView.php | 2 +- framework/Web/UI/WebControls/TListControl.php | 21 +--- framework/Web/UI/WebControls/TRepeater.php | 17 +-- framework/Web/UI/WebControls/TTable.php | 83 +++++++++------ tests/UnitTests/framework/Collections/utList.php | 91 +--------------- tests/UnitTests/framework/Collections/utMap.php | 85 --------------- tests/unit/Collections/TListTest.php | 118 ++++----------------- tests/unit/Collections/TMapTest.php | 96 ++--------------- 21 files changed, 302 insertions(+), 671 deletions(-) diff --git a/demos/quickstart/protected/pages/Advanced/Samples/I18N/Home.php b/demos/quickstart/protected/pages/Advanced/Samples/I18N/Home.php index 6f868074..9f4c7c4e 100644 --- a/demos/quickstart/protected/pages/Advanced/Samples/I18N/Home.php +++ b/demos/quickstart/protected/pages/Advanced/Samples/I18N/Home.php @@ -35,7 +35,7 @@ class Home extends TPage */ public function getCurrentCulture() { - $culture = $this->Application->Globalization->Culture; + $culture = $this->getApplication()->getGlobalization()->getCulture(); $cultureInfo = new CultureInfo($culture); return $cultureInfo->getNativeName(); } 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; - } } /** diff --git a/framework/Data/TXmlDocument.php b/framework/Data/TXmlDocument.php index 0b37258d..bb22b254 100644 --- a/framework/Data/TXmlDocument.php +++ b/framework/Data/TXmlDocument.php @@ -416,36 +416,41 @@ class TXmlElementList extends TList return $this->_o; } - /** - * Overrides the parent implementation with customized processing of the newly added item. - * @param mixed the newly added item - */ - protected function addedItem($item) - { - if($item->getParent()!==null) - $item->getParent()->getElements()->remove($item); - $item->setParent($this->_o); - } /** - * Overrides the parent implementation with customized processing of the removed item. - * @param mixed the removed item + * Inserts an item at the specified position. + * This overrides the parent implementation by performing additional + * operations for each newly added TXmlElement object. + * @param integer the speicified position. + * @param mixed new item + * @throws TInvalidDataTypeException if the item to be inserted is not a TXmlElement object. */ - protected function removedItem($item) + public function insertAt($index,$item) { - $item->setParent(null); + if($item instanceof TXmlElement) + { + parent::insertAt($index,$item); + if($item->getParent()!==null) + $item->getParent()->getElements()->remove($item); + $item->setParent($this->_o); + } + else + throw new TInvalidDataTypeException('xmlelementlist_xmlelement_required'); } /** - * 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 mixed item to be added - * @return boolean whether the item can be added to the map + * Removes an item at the specified position. + * This overrides the parent implementation by performing additional + * cleanup work when removing a TXmlElement object. + * @param integer the index of the item to be removed. + * @return mixed the removed item. */ - protected function canAddItem($item) + public function removeAt($index) { - return ($item instanceof TXmlElement); + $item=parent::removeAt($index); + if($item instanceof TXmlElement) + $item->setParent(null); + return $item; } } diff --git a/framework/Exceptions/messages.txt b/framework/Exceptions/messages.txt index 69015d48..8b739623 100644 --- a/framework/Exceptions/messages.txt +++ b/framework/Exceptions/messages.txt @@ -15,8 +15,6 @@ component_statements_invalid = Component '%s' is evaluating invalid PHP statem propertyvalue_enumvalue_invalid = Value '%s' is a not valid enumeration value (%s). list_index_invalid = Index '%d' is out of range. -list_addition_disallowed = The new item cannot be added to the list. -list_item_unremovable = The item cannot be removed from the list. list_item_inexistent = The item cannot be found in the list. list_data_not_iterable = Data must be either an array or an object implementing Traversable interface. @@ -111,9 +109,13 @@ template_eventhandler_invalid = An invalid event handler is attached to %s's e xmldocument_file_read_failed = TXmlDocument is unable to read file '%s'. xmldocument_file_write_failed = TXmlDocument is unable to write file '%s'. +xmlelementlist_xmlelement_required = TXmlElementList can only accept TXmlElement objects. + authorizationrule_action_invalid = TAuthorizationRule.Action can only take 'allow' or 'deny' as the value. authorizationrule_verb_invalid = TAuthorizationRule.Verb can only take 'get' or 'post' as the value. +authorizationrulecollection_authorizationrule_required = TAuthorizationRuleCollection can only accept TAuthorizationRule objects. + usermanager_userfile_invalid = TUserManager.UserFile '%s' is not a valid file. usermanager_userfile_unchangeable = TUserManager.UserFile cannot be modified. The user module has been initialized already. @@ -141,6 +143,8 @@ control_enabletheming_unchangeable = %s.EnableTheming cannot be modified after control_stylesheet_applied = StyleSheet skin has already been applied to %s. control_id_nonunique = %s.ID '%s' is not unique among all controls under the same naming container. +controllist_control_required = TControlList can only accept strings or TControl objects. + webcontrol_accesskey_invalid = %s.AccessKey '%s' is invalid. It must be a single character only. webcontrol_style_invalid = %s.Style must take string value only. @@ -189,4 +193,16 @@ comparevalidator_controltocompare_invalid = TCompareValidator.ControlToCompare c repeater_template_required = TRepeater.%s requires a template instance implementing ITemplate interface. datalist_template_required = TDataList.%s requires a template instance implementing ITemplate interface. -templatecolumn_template_required = TTemplateColumn.%s requires a template instance implementing ITemplate interface. \ No newline at end of file +templatecolumn_template_required = TTemplateColumn.%s requires a template instance implementing ITemplate interface. + +repeateritemcollection_repeateritem_required = TRepeaterItemCollection can only accept TRepeaterItem objects. + +datagriditemcollection_datagriditem_required = TDataGridItemCollection can only accept TDataGridItem objects. + +datagridcolumncollection_datagridcolumn_required = TDataGridColumnCollection can only accept TDataGridColumn objects. + +datalistitemcollection_datalistitem_required = TDataListItemCollection can only accept TDataListItem objects. + +tablerowcollection_tablerow_required = TTableRowCollection can only accept TTableRow objects. + +tablecellcollection_tablerow_required = TTableCellCollection can only accept TTableCell objects. \ No newline at end of file diff --git a/framework/Security/TAuthorizationRule.php b/framework/Security/TAuthorizationRule.php index 2ee6de49..81955a44 100644 --- a/framework/Security/TAuthorizationRule.php +++ b/framework/Security/TAuthorizationRule.php @@ -200,13 +200,19 @@ class TAuthorizationRuleCollection extends TList } /** - * Ensures that only instance of TAuthorizationRule is added to the collection. - * @param mixed item to be added to the collection - * @return boolean whether the item can be added to the collection - */ - protected function canAddItem($item) + * Inserts an item at the specified position. + * This overrides the parent implementation by performing additional + * operations for each newly added TAuthorizationRule object. + * @param integer the speicified position. + * @param mixed new item + * @throws TInvalidDataTypeException if the item to be inserted is not a TAuthorizationRule object. + */ + public function insertAt($index,$item) { - return ($item instanceof TAuthorizationRule); + if($item instanceof TAuthorizationRule) + parent::insertAt($index,$item); + else + throw new TInvalidDataTypeException('authorizationrulecollection_authorizationrule_required'); } } diff --git a/framework/TComponent.php b/framework/TComponent.php index cc5fa1b5..6641fd1a 100644 --- a/framework/TComponent.php +++ b/framework/TComponent.php @@ -300,12 +300,12 @@ class TComponent * getting and setting properties, e.g., * * $component->OnClick[]=array($object,'buttonClicked'); - * $component->OnClick->insert(0,array($object,'buttonClicked')); + * $component->OnClick->insertAt(0,array($object,'buttonClicked')); * * which are equivalent to the following * * $component->getEventHandlers('OnClick')->add(array($object,'buttonClicked')); - * $component->getEventHandlers('OnClick')->insert(0,array($object,'buttonClicked')); + * $component->getEventHandlers('OnClick')->insertAt(0,array($object,'buttonClicked')); * * * @param string the event name diff --git a/framework/Web/THttpRequest.php b/framework/Web/THttpRequest.php index 2a3a35aa..ba626d07 100644 --- a/framework/Web/THttpRequest.php +++ b/framework/Web/THttpRequest.php @@ -140,7 +140,7 @@ class THttpRequest extends TMap implements IModule $_COOKIE=$this->stripSlashes($_COOKIE); } - $this->copyfrom(array_merge($_GET,$_POST)); + $this->copyFrom(array_merge($_GET,$_POST)); $this->_initialized=true; $this->getApplication()->setRequest($this); @@ -487,32 +487,38 @@ class THttpCookieCollection extends TList } /** - * Adds the cookie if owner of this collection is of THttpResponse. - * This method will be invoked whenever an item is added to the collection. + * Inserts an item at the specified position. + * This overrides the parent implementation by performing additional + * operations for each newly added THttpCookie object. + * @param integer the speicified position. + * @param mixed new item + * @throws TInvalidDataTypeException if the item to be inserted is not a THttpCookie object. */ - protected function addedItem($item) + public function insertAt($index,$item) { - if($this->_o instanceof THttpResponse) - $this->_o->addCookie($item); + if($item instanceof THttpCookie) + { + parent::insertAt($index,$item); + if($this->_o instanceof THttpResponse) + $this->_o->addCookie($item); + } + else + throw new TInvalidDataTypeException('authorizationrulecollection_authorizationrule_required'); } /** - * Removes the cookie if owner of this collection is of THttpResponse. - * This method will be invoked whenever an item is removed from the collection. + * Removes an item at the specified position. + * This overrides the parent implementation by performing additional + * cleanup work when removing a TCookie object. + * @param integer the index of the item to be removed. + * @return mixed the removed item. */ - protected function removedItem($item) + public function removeAt($index) { + $item=parent::removeAt($index); if($this->_o instanceof THttpResponse) $this->_o->removeCookie($item); - } - - /** - * Restricts acceptable item of this collection to THttpCookie. - * This method will be invoked whenever an item is to be added into the collection. - */ - protected function canAddItem($item) - { - return ($item instanceof THttpCookie); + return $item; } } diff --git a/framework/Web/UI/TControl.php b/framework/Web/UI/TControl.php index 7b9fee4e..ee1b8e11 100644 --- a/framework/Web/UI/TControl.php +++ b/framework/Web/UI/TControl.php @@ -1487,32 +1487,39 @@ class TControlList extends TList } /** - * Overrides the parent implementation with customized processing of the newly added item. - * @param mixed the newly added item - */ - protected function addedItem($item) - { - if($item instanceof TControl) + * Inserts an item at the specified position. + * This overrides the parent implementation by performing additional + * operations for each newly added child control. + * @param integer the speicified position. + * @param mixed new item + * @throws TInvalidDataTypeException if the item to be inserted is neither a string nor a TControl. + */ + public function insertAt($index,$item) + { + if(is_string($item)) + parent::insertAt($index,$item); + else if($item instanceof TControl) + { + parent::insertAt($index,$item); $this->_o->addedControl($item); + } + else + throw new TInvalidDataTypeException('controllist_control_required'); } /** - * Overrides the parent implementation with customized processing of the removed item. - * @param mixed the removed item + * Removes an item at the specified position. + * This overrides the parent implementation by performing additional + * cleanup work when removing a child control. + * @param integer the index of the item to be removed. + * @return mixed the removed item. */ - protected function removedItem($item) + public function removeAt($index) { + $item=parent::removeAt($index); if($item instanceof TControl) $this->_o->removedControl($item); - } - - /** - * Only string or instance of TControl can be added into collection. - * @param mixed the item to be added - */ - protected function canAddItem($item) - { - return is_string($item) || ($item instanceof TControl); + return $item; } /** diff --git a/framework/Web/UI/TTemplateControl.php b/framework/Web/UI/TTemplateControl.php index 79ada7d4..d7f1868b 100644 --- a/framework/Web/UI/TTemplateControl.php +++ b/framework/Web/UI/TTemplateControl.php @@ -165,7 +165,7 @@ class TTemplateControl extends TControl implements INamingContainer if(isset($this->_placeholders[$id])) { list($parent,$loc)=$this->_placeholders[$id]; - $parent->getControls()->insert($loc,$content); + $parent->getControls()->insertAt($loc,$content); } } diff --git a/framework/Web/UI/WebControls/TDataGrid.php b/framework/Web/UI/WebControls/TDataGrid.php index 646ead01..0aad717d 100644 --- a/framework/Web/UI/WebControls/TDataGrid.php +++ b/framework/Web/UI/WebControls/TDataGrid.php @@ -1553,15 +1553,18 @@ class TDataGridItem extends TTableRow implements INamingContainer class TDataGridItemCollection extends TList { /** - * Returns true only when the item to be added is a {@link TDataGridItem}. - * 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. - * @param mixed item to be added - * @return boolean whether the item can be added to the list + * Inserts an item at the specified position. + * This overrides the parent implementation by inserting only TDataGridItem. + * @param integer the speicified position. + * @param mixed new item + * @throws TInvalidDataTypeException if the item to be inserted is not a TDataGridItem. */ - protected function canAddItem($item) + public function insertAt($index,$item) { - return ($item instanceof TDataGridItem); + if($item instanceof TDataGridItem) + parent::insertAt($index,$item); + else + throw new TInvalidDataTypeException('datagriditemcollection_datagriditem_required'); } } @@ -1578,15 +1581,18 @@ class TDataGridItemCollection extends TList class TDataGridColumnCollection extends TList { /** - * Returns true only when the item to be added is a {@link TDataGridItem}. - * 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. - * @param mixed item to be added - * @return boolean whether the item can be added to the list + * Inserts an item at the specified position. + * This overrides the parent implementation by inserting only TDataGridColumn. + * @param integer the speicified position. + * @param mixed new item + * @throws TInvalidDataTypeException if the item to be inserted is not a TDataGridColumn. */ - protected function canAddItem($item) + public function insertAt($index,$item) { - return ($item instanceof TDataGridColumn); + if($item instanceof TDataGridColumn) + parent::insertAt($index,$item); + else + throw new TInvalidDataTypeException('datagridcolumncollection_datagridcolumn_required'); } } diff --git a/framework/Web/UI/WebControls/TDataList.php b/framework/Web/UI/WebControls/TDataList.php index c0d74974..47b60cd6 100644 --- a/framework/Web/UI/WebControls/TDataList.php +++ b/framework/Web/UI/WebControls/TDataList.php @@ -1354,15 +1354,18 @@ class TDataListItem extends TWebControl implements INamingContainer class TDataListItemCollection extends TList { /** - * Returns true only when the item to be added is a {@link TDataListItem}. - * 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. - * @param mixed item to be added - * @return boolean whether the item can be added to the list + * Inserts an item at the specified position. + * This overrides the parent implementation by inserting only TDataListItem. + * @param integer the speicified position. + * @param mixed new item + * @throws TInvalidDataTypeException if the item to be inserted is not a TDataListItem. */ - protected function canAddItem($item) + public function insertAt($index,$item) { - return ($item instanceof TDataListItem); + if($item instanceof TDataListItem) + parent::insertAt($index,$item); + else + throw new TInvalidDataTypeException('datalistitemcollection_datalistitem_required'); } } diff --git a/framework/Web/UI/WebControls/TDataSourceView.php b/framework/Web/UI/WebControls/TDataSourceView.php index ab79e74c..c20556bb 100644 --- a/framework/Web/UI/WebControls/TDataSourceView.php +++ b/framework/Web/UI/WebControls/TDataSourceView.php @@ -103,7 +103,7 @@ abstract class TDataSourceView extends TComponent * @param array|TMap * @return integer affected rows */ - public function insert($values) + public function insertAt($values) { throw new TNotSupportedException('datasourceview_insert_unsupported'); } diff --git a/framework/Web/UI/WebControls/TListControl.php b/framework/Web/UI/WebControls/TListControl.php index 6faccf31..8d2959b4 100644 --- a/framework/Web/UI/WebControls/TListControl.php +++ b/framework/Web/UI/WebControls/TListControl.php @@ -566,21 +566,6 @@ abstract class TListControl extends TDataBoundControl */ class TListItemCollection extends TList { - /** - * Appends an item to the collection. - * @param TListItem the item to be appended. - * @throws TInvalidDataTypeException if the item being appended is neither a string nor a TListItem - */ - public function add($item) - { - if(is_string($item)) - parent::add(new TListItem($item)); - else if($item instanceof TListItem) - parent::add($item); - else - throw new TInvalidDataTypeException('listitemcollection_item_invalid'); - } - /** * Inserts an item into the collection. * @param integer the location where the item will be inserted. @@ -588,12 +573,12 @@ class TListItemCollection extends TList * @param TListItem the item to be inserted. * @throws TInvalidDataTypeException if the item being inserted is neither a string nor TListItem */ - public function insert($index,$item) + public function insertAt($index,$item) { if(is_string($item)) - parent::insert($index,new TListItem($item)); + parent::insertAt($index,new TListItem($item)); else if($item instanceof TListItem) - parent::insert($index,$item); + parent::insertAt($index,$item); else throw new TInvalidDataTypeException('listitemcollection_item_invalid'); } diff --git a/framework/Web/UI/WebControls/TRepeater.php b/framework/Web/UI/WebControls/TRepeater.php index 6aa6b461..a122f387 100644 --- a/framework/Web/UI/WebControls/TRepeater.php +++ b/framework/Web/UI/WebControls/TRepeater.php @@ -661,15 +661,18 @@ class TRepeaterItem extends TControl implements INamingContainer class TRepeaterItemCollection extends TList { /** - * Returns true only when the item to be added is a {@link TRepeaterItem}. - * 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. - * @param mixed item to be added - * @return boolean whether the item can be added to the list + * Inserts an item at the specified position. + * This overrides the parent implementation by inserting only TRepeaterItem. + * @param integer the speicified position. + * @param mixed new item + * @throws TInvalidDataTypeException if the item to be inserted is not a TRepeaterItem. */ - protected function canAddItem($item) + public function insertAt($index,$item) { - return ($item instanceof TRepeaterItem); + if($item instanceof TRepeaterItem) + parent::insertAt($index,$item); + else + throw new TInvalidDataTypeException('repeateritemcollection_repeateritem_required'); } } diff --git a/framework/Web/UI/WebControls/TTable.php b/framework/Web/UI/WebControls/TTable.php index dab0fb60..fb70e66c 100644 --- a/framework/Web/UI/WebControls/TTable.php +++ b/framework/Web/UI/WebControls/TTable.php @@ -712,32 +712,38 @@ class TTableRowCollection extends TList } /** - * Only string or instance of TControl can be added into collection. - * @param mixed the item to be added + * Inserts an item at the specified position. + * This overrides the parent implementation by performing additional + * operations for each newly added table row. + * @param integer the speicified position. + * @param mixed new item + * @throws TInvalidDataTypeException if the item to be inserted is not a TTableRow object. */ - protected function canAddItem($item) + public function insertAt($index,$item) { - return ($item instanceof TTableRow); - } - - /** - * Overrides the parent implementation with customized processing of the newly added item. - * @param mixed the newly added item - */ - protected function addedItem($item) - { - if($this->_owner) - $this->_owner->getControls()->add($item); + if($item instanceof TTableRow) + { + parent::insertAt($index,$item); + if($this->_owner) + $this->_owner->getControls()->insertAt($index,$item); + } + else + throw new TInvalidDataTypeException('tablerowcollection_tablerow_required'); } /** - * Overrides the parent implementation with customized processing of the removed item. - * @param mixed the removed item + * Removes an item at the specified position. + * This overrides the parent implementation by performing additional + * cleanup work when removing a table row. + * @param integer the index of the item to be removed. + * @return mixed the removed item. */ - protected function removedItem($item) + public function removeAt($index) { - if($this->_owner) + $item=parent::removeAt($index); + if($item instanceof TTableRow) $this->_owner->getControls()->remove($item); + return $item; } } @@ -768,33 +774,40 @@ class TTableCellCollection extends TList $this->_owner=$owner; } - /** - * Only string or instance of TTableCell can be added into collection. - * @param mixed the item to be added - */ - protected function canAddItem($item) - { - return ($item instanceof TTableCell); - } /** - * Overrides the parent implementation with customized processing of the newly added item. - * @param mixed the newly added item + * Inserts an item at the specified position. + * This overrides the parent implementation by performing additional + * operations for each newly added table cell. + * @param integer the speicified position. + * @param mixed new item + * @throws TInvalidDataTypeException if the item to be inserted is not a TTableCell object. */ - protected function addedItem($item) + public function insertAt($index,$item) { - if($this->_owner) - $this->_owner->getControls()->add($item); + if($item instanceof TTableCell) + { + parent::insertAt($index,$item); + if($this->_owner) + $this->_owner->getControls()->insertAt($index,$item); + } + else + throw new TInvalidDataTypeException('tablecellcollection_tablecell_required'); } /** - * Overrides the parent implementation with customized processing of the removed item. - * @param mixed the removed item + * Removes an item at the specified position. + * This overrides the parent implementation by performing additional + * cleanup work when removing a table cell. + * @param integer the index of the item to be removed. + * @return mixed the removed item. */ - protected function removedItem($item) + public function removeAt($index) { - if($this->_owner) + $item=parent::removeAt($index); + if($item instanceof TTableCell) $this->_owner->getControls()->remove($item); + return $item; } } ?> \ No newline at end of file diff --git a/tests/UnitTests/framework/Collections/utList.php b/tests/UnitTests/framework/Collections/utList.php index 056f38b1..e95f718b 100644 --- a/tests/UnitTests/framework/Collections/utList.php +++ b/tests/UnitTests/framework/Collections/utList.php @@ -7,54 +7,6 @@ class ListItem public $data='data'; } -class NewList extends TList -{ - private $_canAddItem=true; - private $_canRemoveItem=true; - private $_itemAdded=false; - private $_itemRemoved=false; - - protected function addedItem($item) - { - $this->_itemAdded=true; - } - - protected function removedItem($item) - { - $this->_itemRemoved=true; - } - - protected function canAddItem($item) - { - return $this->_canAddItem; - } - - protected function canRemoveItem($item) - { - return $this->_canRemoveItem; - } - - public function setCanAddItem($value) - { - $this->_canAddItem=$value; - } - - public function setCanRemoveItem($value) - { - $this->_canRemoveItem=$value; - } - - public function isItemAdded() - { - return $this->_itemAdded; - } - - public function isItemRemoved() - { - return $this->_itemRemoved; - } -} - class utList extends UnitTestCase { protected $list; @@ -101,16 +53,16 @@ class utList extends UnitTestCase } - public function testInsert() + public function testInsertAt() { - $this->list->insert(0,$this->item3); + $this->list->insertAt(0,$this->item3); $this->assertEqual(3,$this->list->getCount()); $this->assertEqual(2,$this->list->indexOf($this->item2)); $this->assertEqual(0,$this->list->indexOf($this->item3)); $this->assertEqual(1,$this->list->indexOf($this->item1)); try { - $this->list->insert(4,$this->item3); + $this->list->insertAt(4,$this->item3); $this->fail('exception not raised when adding item at an out-of-range index'); } catch(TInvalidDataValueException $e) @@ -279,43 +231,6 @@ class utList extends UnitTestCase $this->assertTrue(isset($this->list[1])); $this->assertFalse(isset($this->list[2])); } - - public function testDerivedClasses() - { - $newList=new NewList; - $this->assertFalse($newList->isItemAdded()); - $newList->add($this->item1); - $this->assertTrue($newList->isItemAdded()); - $newList->add($this->item2); - - $newList->setCanAddItem(false); - try - { - $newList->add($this->item3); - $this->fail('no exception raised when adding an item that is disallowed'); - } - catch(TInvalidOperationException $e) - { - $this->assertEqual(2,$newList->getCount()); - $this->pass(); - } - - $this->assertFalse($newList->isItemRemoved()); - $newList->remove($this->item1); - $this->assertTrue($newList->isItemRemoved()); - - $newList->setCanRemoveItem(false); - try - { - $newList->remove($this->item2); - $this->fail('no exception raised when removing an item that is disallowed'); - } - catch(TInvalidOperationException $e) - { - $this->assertEqual(1,$newList->getCount()); - $this->pass(); - } - } } ?> \ No newline at end of file diff --git a/tests/UnitTests/framework/Collections/utMap.php b/tests/UnitTests/framework/Collections/utMap.php index 1f7a20ad..37818542 100644 --- a/tests/UnitTests/framework/Collections/utMap.php +++ b/tests/UnitTests/framework/Collections/utMap.php @@ -7,54 +7,6 @@ class MapItem public $data='data'; } -class NewMap extends TMap -{ - private $_canAddItem=true; - private $_canRemoveItem=true; - private $_itemAdded=false; - private $_itemRemoved=false; - - protected function addedItem($key,$value) - { - $this->_itemAdded=true; - } - - protected function removedItem($key,$value) - { - $this->_itemRemoved=true; - } - - protected function canAddItem($key,$value) - { - return $this->_canAddItem; - } - - protected function canRemoveItem($key,$value) - { - return $this->_canRemoveItem; - } - - public function setCanAddItem($value) - { - $this->_canAddItem=$value; - } - - public function setCanRemoveItem($value) - { - $this->_canRemoveItem=$value; - } - - public function isItemAdded() - { - return $this->_itemAdded; - } - - public function isItemRemoved() - { - return $this->_itemRemoved; - } -} - class utMap extends UnitTestCase { protected $map; @@ -203,43 +155,6 @@ class utMap extends UnitTestCase $this->assertTrue(isset($this->map['key1'])); $this->assertFalse(isset($this->map['unknown key'])); } - - public function testDerivedClasses() - { - $newMap=new NewMap; - $this->assertFalse($newMap->isItemAdded()); - $newMap->add('key','value'); - $this->assertTrue($newMap->isItemAdded()); - $newMap->add('key2','value2'); - - $newMap->setCanAddItem(false); - try - { - $newMap->add('new key','new value'); - $this->fail('no exception raised when adding an item that is disallowed'); - } - catch(TInvalidOperationException $e) - { - $this->assertEqual(2,$newMap->getCount()); - $this->pass(); - } - - $this->assertFalse($newMap->isItemRemoved()); - $newMap->remove('key'); - $this->assertTrue($newMap->isItemRemoved()); - - $newMap->setCanRemoveItem(false); - try - { - $newMap->remove('key2'); - $this->fail('no exception raised when removing an item that is disallowed'); - } - catch(TInvalidOperationException $e) - { - $this->assertEqual(1,$newMap->getCount()); - $this->pass(); - } - } } ?> \ No newline at end of file diff --git a/tests/unit/Collections/TListTest.php b/tests/unit/Collections/TListTest.php index 3dacb701..a64e9da2 100644 --- a/tests/unit/Collections/TListTest.php +++ b/tests/unit/Collections/TListTest.php @@ -6,61 +6,13 @@ class ListItem public $data='data'; } -class NewList extends TList -{ - private $_canAddItem=true; - private $_canRemoveItem=true; - private $_itemAdded=false; - private $_itemRemoved=false; - - protected function addedItem($item) - { - $this->_itemAdded=true; - } - - protected function removedItem($item) - { - $this->_itemRemoved=true; - } - - protected function canAddItem($item) - { - return $this->_canAddItem; - } - - protected function canRemoveItem($item) - { - return $this->_canRemoveItem; - } - - public function setCanAddItem($value) - { - $this->_canAddItem=$value; - } - - public function setCanRemoveItem($value) - { - $this->_canRemoveItem=$value; - } - - public function isItemAdded() - { - return $this->_itemAdded; - } - - public function isItemRemoved() - { - return $this->_itemRemoved; - } -} - /** * @package System.Collections */ class TListTest extends PHPUnit2_Framework_TestCase { protected $list; protected $item1,$item2,$item3; - + public function setUp() { $this->list=new TList; $this->item1=new ListItem; @@ -69,14 +21,14 @@ class TListTest extends PHPUnit2_Framework_TestCase { $this->list->add($this->item1); $this->list->add($this->item2); } - + public function tearDown() { $this->list=null; $this->item1=null; $this->item2=null; $this->item3=null; } - + public function testConstruct() { $a=array(1,2,3); $list=new TList($a); @@ -89,7 +41,7 @@ class TListTest extends PHPUnit2_Framework_TestCase { $this->assertEquals(2,$this->list->getCount()); $this->assertEquals(2,$this->list->Count); } - + public function testAdd() { $this->list->add(null); $this->list->add($this->item3); @@ -107,10 +59,10 @@ class TListTest extends PHPUnit2_Framework_TestCase { $this->list->insert(4,$this->item3); $this->fail('exception not raised when adding item at an out-of-range index'); } catch(TInvalidDataValueException $e) { - + } } - + public function testRemove() { $this->list->remove($this->item1); $this->assertEquals(1,$this->list->getCount()); @@ -120,10 +72,10 @@ class TListTest extends PHPUnit2_Framework_TestCase { $this->list->remove($this->item1); $this->fail('exception not raised when removing nonexisting item'); } catch(Exception $e) { - + } } - + public function testRemoveAt() { $this->list->add($this->item3); $this->list->removeAt(1); @@ -134,17 +86,17 @@ class TListTest extends PHPUnit2_Framework_TestCase { $this->list->removeAt(2); $this->fail('exception not raised when removing item with invalid index'); } catch(TInvalidDataValueException $e) { - + } } - + public function testClear() { $this->list->clear(); $this->assertEquals(0,$this->list->getCount()); $this->assertEquals(-1,$this->list->indexOf($this->item1)); $this->assertEquals(-1,$this->list->indexOf($this->item2)); } - + public function testContains() { $this->assertTrue($this->list->contains($this->item1)); $this->assertTrue($this->list->contains($this->item2)); @@ -165,7 +117,7 @@ class TListTest extends PHPUnit2_Framework_TestCase { $this->list->copyFrom($this); $this->fail('exception not raised when copying from non-traversable object'); } catch(TInvalidDataTypeException $e) { - + } } @@ -177,15 +129,15 @@ class TListTest extends PHPUnit2_Framework_TestCase { $this->list->mergeWith($this); $this->fail('exception not raised when copying from non-traversable object'); } catch(TInvalidDataTypeException $e) { - + } } - + public function testToArray() { $array=$this->list->toArray(); $this->assertTrue(count($array)==2 && $array[0]===$this->item1 && $array[1]===$this->item2); } - + public function testArrayRead() { $this->assertTrue($this->list[0]===$this->item1); $this->assertTrue($this->list[1]===$this->item2); @@ -193,10 +145,10 @@ class TListTest extends PHPUnit2_Framework_TestCase { $a=$this->list[2]; $this->fail('exception not raised when accessing item with out-of-range index'); } catch(TInvalidDataValueException $e) { - + } } - + /*public function testArrayWrite() { $this->list[]=$this->item3; $this->assertTrue($this->list[2]===$this->item3 && $this->list->getCount()===3); @@ -208,16 +160,16 @@ class TListTest extends PHPUnit2_Framework_TestCase { $this->list[5]=$this->item3; $this->fail('exception not raised when setting item at an out-of-range index'); } catch(TInvalidDataValueException $e) { - + } try { unset($this->list[5]); $this->fail('exception not raised when unsetting item at an out-of-range index'); } catch(TInvalidDataValueException $e) { - + } }*/ - + public function testGetIterator() { $n=0; $found=0; @@ -231,40 +183,12 @@ class TListTest extends PHPUnit2_Framework_TestCase { } $this->assertTrue($n==2 && $found==2); } - + public function testArrayMisc() { $this->assertEquals(1,count($this->list)); $this->assertTrue(isset($this->list[1])); $this->assertFalse(isset($this->list[2])); } - - public function testDerivedClasses() { - $newList=new NewList; - $this->assertFalse($newList->isItemAdded()); - $newList->add($this->item1); - $this->assertTrue($newList->isItemAdded()); - $newList->add($this->item2); - - $newList->setCanAddItem(false); - try { - $newList->add($this->item3); - $this->fail('no exception raised when adding an item that is disallowed'); - } catch(TInvalidOperationException $e) { - $this->assertEquals(2,$newList->getCount()); - } - - $this->assertFalse($newList->isItemRemoved()); - $newList->remove($this->item1); - $this->assertTrue($newList->isItemRemoved()); - - $newList->setCanRemoveItem(false); - try { - $newList->remove($this->item2); - $this->fail('no exception raised when removing an item that is disallowed'); - } catch(TInvalidOperationException $e) { - $this->assertEquals(1,$newList->getCount()); - } - } } diff --git a/tests/unit/Collections/TMapTest.php b/tests/unit/Collections/TMapTest.php index a6f82570..af92fd6a 100644 --- a/tests/unit/Collections/TMapTest.php +++ b/tests/unit/Collections/TMapTest.php @@ -5,61 +5,13 @@ class MapItem { public $data='data'; } -class NewMap extends TMap -{ - private $_canAddItem=true; - private $_canRemoveItem=true; - private $_itemAdded=false; - private $_itemRemoved=false; - - protected function addedItem($key,$value) - { - $this->_itemAdded=true; - } - - protected function removedItem($key,$value) - { - $this->_itemRemoved=true; - } - - protected function canAddItem($key,$value) - { - return $this->_canAddItem; - } - - protected function canRemoveItem($key,$value) - { - return $this->_canRemoveItem; - } - - public function setCanAddItem($value) - { - $this->_canAddItem=$value; - } - - public function setCanRemoveItem($value) - { - $this->_canRemoveItem=$value; - } - - public function isItemAdded() - { - return $this->_itemAdded; - } - - public function isItemRemoved() - { - return $this->_itemRemoved; - } -} - /** * @package System.Collections */ class TMapTest extends PHPUnit2_Framework_TestCase { protected $map; protected $item1,$item2,$item3; - + public function setUp() { $this->map=new TMap; $this->item1=new MapItem; @@ -75,7 +27,7 @@ class TMapTest extends PHPUnit2_Framework_TestCase { $this->item2=null; $this->item3=null; } - + public function testConstruct() { $a=array(1,2,'key3'=>3); $map=new TMap($a); @@ -87,7 +39,7 @@ class TMapTest extends PHPUnit2_Framework_TestCase { public function testGetCount() { $this->assertEquals(2,$this->map->getCount()); } - + public function testGetKeys() { $keys=$this->map->getKeys(); $this->assertTrue(count($keys)===2 && $keys[0]==='key1' && $keys[1]==='key2'); @@ -131,7 +83,7 @@ class TMapTest extends PHPUnit2_Framework_TestCase { } catch(TInvalidDataTypeException $e) { - + } } @@ -147,7 +99,7 @@ class TMapTest extends PHPUnit2_Framework_TestCase { } catch(TInvalidDataTypeException $e) { - + } } @@ -169,7 +121,7 @@ class TMapTest extends PHPUnit2_Framework_TestCase { try { unset($this->map['unknown key']); - + } catch(Exception $e) { @@ -198,42 +150,6 @@ class TMapTest extends PHPUnit2_Framework_TestCase { $this->assertTrue(isset($this->map['key1'])); $this->assertFalse(isset($this->map['unknown key'])); } - - public function testDerivedClasses() - { - $newMap=new NewMap; - $this->assertFalse($newMap->isItemAdded()); - $newMap->add('key','value'); - $this->assertTrue($newMap->isItemAdded()); - $newMap->add('key2','value2'); - - $newMap->setCanAddItem(false); - try - { - $newMap->add('new key','new value'); - $this->fail('no exception raised when adding an item that is disallowed'); - } - catch(TInvalidOperationException $e) - { - $this->assertEquals(2,$newMap->getCount()); - - } - - $this->assertFalse($newMap->isItemRemoved()); - $newMap->remove('key'); - $this->assertTrue($newMap->isItemRemoved()); - - $newMap->setCanRemoveItem(false); - try - { - $newMap->remove('key2'); - $this->fail('no exception raised when removing an item that is disallowed'); - } - catch(TInvalidOperationException $e) - { - $this->assertEquals(1,$newMap->getCount()); - } - } } ?> \ No newline at end of file -- cgit v1.2.3