diff options
| author | xue <> | 2006-02-24 01:07:56 +0000 | 
|---|---|---|
| committer | xue <> | 2006-02-24 01:07:56 +0000 | 
| commit | 8a3d7d5dd5149cca2bea77ec31a36700eb990092 (patch) | |
| tree | f33a12653d72f195fdd0ad47ed397c4c543ad691 | |
| parent | 211f4c01bab338b7d992faa27bbde238a2004f17 (diff) | |
Added ReadOnly property to TList and TMap.
| -rw-r--r-- | framework/Collections/TList.php | 70 | ||||
| -rw-r--r-- | framework/Collections/TMap.php | 47 | ||||
| -rw-r--r-- | framework/Exceptions/messages.txt | 2 | 
3 files changed, 92 insertions, 27 deletions
diff --git a/framework/Collections/TList.php b/framework/Collections/TList.php index 3480edc3..5f4534ca 100644 --- a/framework/Collections/TList.php +++ b/framework/Collections/TList.php @@ -49,17 +49,39 @@ class TList extends TComponent implements IteratorAggregate,ArrayAccess  	 * @var integer
  	 */
  	private $_c=0;
 +	/**
 +	 * @var boolean whether this list is read-only
 +	 */
 +	private $_r=false;
  	/**
  	 * Constructor.
  	 * Initializes the list with an array or an iterable object.
  	 * @param array|Iterator the intial data. Default is null, meaning no initialization.
 +	 * @param boolean whether the list is read-only
  	 * @throws TInvalidDataTypeException If data is not null and neither an array nor an iterator.
  	 */
 -	public function __construct($data=null)
 +	public function __construct($data=null,$readOnly=false)
  	{
  		if($data!==null)
  			$this->copyFrom($data);
 +		$this->setReadOnly($readOnly);
 +	}
 +
 +	/**
 +	 * @return boolean whether this list is read-only or not. Defaults to false.
 +	 */
 +	public function getReadOnly()
 +	{
 +		return $this->_r;
 +	}
 +
 +	/**
 +	 * @param boolean whether this list is read-only or not
 +	 */
 +	protected function setReadOnly($value)
 +	{
 +		$this->_r=TPropertyValue::ensureBoolean($value);
  	}
  	/**
 @@ -113,18 +135,24 @@ 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 list is read-only
  	 */
  	public function insertAt($index,$item)
  	{
 -		if($index===$this->_c)
 -			$this->_d[$this->_c++]=$item;
 -		else if($index>=0 && $index<$this->_c)
 +		if(!$this->_r)
  		{
 -			array_splice($this->_d,$index,0,array($item));
 -			$this->_c++;
 +			if($index===$this->_c)
 +				$this->_d[$this->_c++]=$item;
 +			else if($index>=0 && $index<$this->_c)
 +			{
 +				array_splice($this->_d,$index,0,array($item));
 +				$this->_c++;
 +			}
 +			else
 +				throw new TInvalidDataValueException('list_index_invalid',$index);
  		}
  		else
 -			throw new TInvalidDataValueException('list_index_invalid',$index);
 +			throw new TInvalidOperation('list_readonly');
  	}
  	/**
 @@ -150,24 +178,30 @@ class TList extends TComponent implements IteratorAggregate,ArrayAccess  	 * Removes an item at the specified position.
  	 * @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 TInvalidDataValueException If the index specified exceeds the bound
 +	 * @throws TInvalidOperationException if the list is read-only
  	 */
  	public function removeAt($index)
  	{
 -		if($index>=0 && $index<$this->_c)
 +		if(!$this->_r)
  		{
 -			$this->_c--;
 -			if($index===$this->_c)
 -				return array_pop($this->_d);
 -			else
 +			if($index>=0 && $index<$this->_c)
  			{
 -				$item=$this->_d[$index];
 -				array_splice($this->_d,$index,1);
 -				return $item;
 +				$this->_c--;
 +				if($index===$this->_c)
 +					return array_pop($this->_d);
 +				else
 +				{
 +					$item=$this->_d[$index];
 +					array_splice($this->_d,$index,1);
 +					return $item;
 +				}
  			}
 +			else
 +				throw new TInvalidDataValueException('list_index_invalid',$index);
  		}
  		else
 -			throw new TInvalidDataValueException('list_index_invalid',$index);
 +			throw new TInvalidOperation('list_readonly');
  	}
  	/**
 @@ -275,7 +309,6 @@ class TList extends TComponent implements IteratorAggregate,ArrayAccess  	 * This method is required by the interface ArrayAccess.
  	 * @param integer the offset to set item
  	 * @param mixed the item value
 -	 * @throws TOutOfRangeException If the index specified exceeds the bound
  	 */
  	public function offsetSet($offset,$item)
  	{
 @@ -292,7 +325,6 @@ class TList extends TComponent implements IteratorAggregate,ArrayAccess  	 * Unsets the item at the specified offset.
  	 * This method is required by the interface ArrayAccess.
  	 * @param integer the offset to unset item
 -	 * @throws TOutOfRangeException If the index specified exceeds the bound
  	 */
  	public function offsetUnset($offset)
  	{
 diff --git a/framework/Collections/TMap.php b/framework/Collections/TMap.php index 7c46ae70..df580534 100644 --- a/framework/Collections/TMap.php +++ b/framework/Collections/TMap.php @@ -44,17 +44,39 @@ class TMap extends TComponent implements IteratorAggregate,ArrayAccess  	 * @var array internal data storage
  	 */
  	private $_d=array();
 +	/**
 +	 * @var boolean whether this list is read-only
 +	 */
 +	private $_r=false;
  	/**
  	 * Constructor.
  	 * Initializes the list with an array or an iterable object.
  	 * @param array|Iterator the intial data. Default is null, meaning no initialization.
 +	 * @param boolean whether the list is read-only
  	 * @throws TInvalidDataTypeException If data is not null and neither an array nor an iterator.
  	 */
 -	public function __construct($data=null)
 +	public function __construct($data=null,$readOnly=false)
  	{
  		if($data!==null)
  			$this->copyFrom($data);
 +		$this->setReadOnly($readOnly);
 +	}
 +
 +	/**
 +	 * @return boolean whether this map is read-only or not. Defaults to false.
 +	 */
 +	public function getReadOnly()
 +	{
 +		return $this->_r;
 +	}
 +
 +	/**
 +	 * @param boolean whether this list is read-only or not
 +	 */
 +	protected function setReadOnly($value)
 +	{
 +		$this->_r=TPropertyValue::ensureBoolean($value);
  	}
  	/**
 @@ -99,28 +121,37 @@ class TMap extends TComponent implements IteratorAggregate,ArrayAccess  	 * Note, if the specified key already exists, the old value will be overwritten.
  	 * @param mixed key
  	 * @param mixed value
 +	 * @throws TInvalidOperationException if the map is read-only
  	 */
  	public function add($key,$value)
  	{
 -		$this->_d[$key]=$value;
 +		if(!$this->_r)
 +			$this->_d[$key]=$value;
 +		else
 +			throw new TInvalidOperation('map_readonly');
  	}
  	/**
  	 * Removes an item from the map by its key.
  	 * @param mixed the key of the item to be removed
  	 * @return mixed the removed value, null if no such key exists.
 -	 * @throws TInvalidOperationException if the item cannot be removed
 +	 * @throws TInvalidOperationException if the map is read-only
  	 */
  	public function remove($key)
  	{
 -		if(isset($this->_d[$key]) || array_key_exists($key,$this->_d))
 +		if(!$this->_r)
  		{
 -			$value=$this->_d[$key];
 -			unset($this->_d[$key]);
 -			return $value;
 +			if(isset($this->_d[$key]) || array_key_exists($key,$this->_d))
 +			{
 +				$value=$this->_d[$key];
 +				unset($this->_d[$key]);
 +				return $value;
 +			}
 +			else
 +				return null;
  		}
  		else
 -			return null;
 +			throw new TInvalidOperation('map_readonly');
  	}
  	/**
 diff --git a/framework/Exceptions/messages.txt b/framework/Exceptions/messages.txt index 0734fa1f..b9b1ffb0 100644 --- a/framework/Exceptions/messages.txt +++ b/framework/Exceptions/messages.txt @@ -17,10 +17,12 @@ propertyvalue_enumvalue_invalid			= Value '%s' is a not valid enumeration value  list_index_invalid						= Index '%d' is out of range.
  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.
 +list_readonly							= %s is read-only.
  map_addition_disallowed					= The new item cannot be added to the map.
  map_item_unremovable					= The item cannot be removed from the map.
  map_data_not_iterable					= Data must be either an array or an object implementing Traversable interface.
 +map_readonly							= %s is read-only.
  application_basepath_invalid			= Application base path '%s' does not exist or is not a directory.
  application_runtimepath_invalid			= Application runtime path '%s' does not exist or is not writable by Web server process.
  | 
