From cbeed6e51bd1c7ea34b0cd934f3da0afe1b45d95 Mon Sep 17 00:00:00 2001 From: javalizard <> Date: Mon, 12 Apr 2010 00:07:29 +0000 Subject: added insertBefore and insertAfter --- framework/Collections/TList.php | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) (limited to 'framework/Collections/TList.php') diff --git a/framework/Collections/TList.php b/framework/Collections/TList.php index 50cbaf01..1a3dffb7 100644 --- a/framework/Collections/TList.php +++ b/framework/Collections/TList.php @@ -4,7 +4,7 @@ * * @author Qiang Xue * @link http://www.pradosoft.com/ - * @copyright Copyright © 2005-2008 PradoSoft + * @copyright Copyright © 2005-2008 PradoSoft * @license http://www.pradosoft.com/license/ * @version $Id$ * @package System.Collections @@ -16,7 +16,7 @@ * TList implements an integer-indexed collection class. * * You can access, append, insert, remove an item by using - * {@link itemAt}, {@link add}, {@link insert}, {@link remove}, and {@link removeAt}. + * {@link itemAt}, {@link add}, {@link insertAt}, {@link remove}, and {@link removeAt}. * To get the number of the items in the list, use {@link getCount}. * TList can also be used like a regular array as follows, * @@ -47,7 +47,7 @@ class TList extends TComponent implements IteratorAggregate,ArrayAccess,Countabl * number of items * @var integer */ - private $_c=0; + protected $_c=0; /** * @var boolean whether this list is read-only */ @@ -243,6 +243,36 @@ class TList extends TComponent implements IteratorAggregate,ArrayAccess,Countabl return $index; } + /** + * Finds the base item. If found, the item is inserted before it. + * @param mixed the base item which will be pushed back by the second parameter + * @param mixed the item + * @return object $this. null if the $baseitem is not found + */ + public function insertBefore($baseitem, $item) + { + if(($index = $this->indexOf($baseitem)) == -1) return null; + + $this->insertAt($index, $item); + + return $this; + } + + /** + * Finds the base item. If found, the item is inserted after it. + * @param mixed the base item which comes before the second parameter when added to the list + * @param mixed the item + * @return object $this. null if the $baseitem is not found + */ + public function insertAfter($baseitem, $item) + { + if(($index = $this->indexOf($baseitem)) == -1) return null; + + $this->insertAt($index + 1, $item); + + return $this; + } + /** * @return array the list of items in array */ -- cgit v1.2.3