Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * TAttributeCollection classes
4
 *
5
 * @author Qiang Xue <qiang.xue@gmail.com>
6
 * @link http://www.pradosoft.com/
7
 * @copyright Copyright &copy; 2005-2008 PradoSoft
8
 * @license http://www.pradosoft.com/license/
9
 * @version $Id: TAttributeCollection.php 2541 2008-10-21 15:05:13Z qiang.xue $
10
 * @package System.Collections
11
 */
12
 
13
/**
14
 * Includes TMap class
15
 */
16
Prado::using('System.Collections.TMap');
17
 
18
/**
19
 * TAttributeCollection class
20
 *
21
 * TAttributeCollection implements a collection for storing attribute names and values.
22
 *
23
 * Besides all functionalities provided by {@link TMap}, TAttributeCollection
24
 * allows you to get and set attribute values like getting and setting
25
 * properties. For example, the following usages are all valid for a
26
 * TAttributeCollection object:
27
 * <code>
28
 * $collection->Text='text';
29
 * echo $collection->Text;
30
 * </code>
31
 * They are equivalent to the following:
32
 * <code>
33
 * $collection->add('Text','text');
34
 * echo $collection->itemAt('Text');
35
 * </code>
36
 *
37
 * Note, attribute names are case-insensitive. They are converted to lower-case
38
 * in the collection storage.
39
 *
40
 * @author Qiang Xue <qiang.xue@gmail.com>
41
 * @version $Id: TAttributeCollection.php 2541 2008-10-21 15:05:13Z qiang.xue $
42
 * @package System.Collections
43
 * @since 3.0
44
 */
45
class TAttributeCollection extends TMap
46
{
47
	private $_caseSensitive=false;
48
 
49
	/**
50
	 * Returns a property value or an event handler list by property or event name.
51
	 * This method overrides the parent implementation by returning
52
	 * a key value if the key exists in the collection.
53
	 * @param string the property name or the event name
54
	 * @return mixed the property value or the event handler list
55
	 * @throws TInvalidOperationException if the property/event is not defined.
56
	 */
57
	public function __get($name)
58
	{
59
		return $this->contains($name)?$this->itemAt($name):parent::__get($name);
60
	}
61
 
62
	/**
63
	 * Sets value of a component property.
64
	 * This method overrides the parent implementation by adding a new key value
65
	 * to the collection.
66
	 * @param string the property name or event name
67
	 * @param mixed the property value or event handler
68
	 * @throws TInvalidOperationException If the property is not defined or read-only.
69
	 */
70
	public function __set($name,$value)
71
	{
72
		$this->add($name,$value);
73
	}
74
 
75
	/**
76
	 * @return boolean whether the keys are case-sensitive. Defaults to false.
77
	 */
78
	public function getCaseSensitive()
79
	{
80
		return $this->_caseSensitive;
81
	}
82
 
83
	/**
84
	 * @param boolean whether the keys are case-sensitive.
85
	 */
86
	public function setCaseSensitive($value)
87
	{
88
		$this->_caseSensitive=TPropertyValue::ensureBoolean($value);
89
	}
90
 
91
	/**
92
	 * Returns the item with the specified key.
93
	 * This overrides the parent implementation by converting the key to lower case first if CaseSensitive is false.
94
	 * @param mixed the key
95
	 * @return mixed the element at the offset, null if no element is found at the offset
96
	 */
97
	public function itemAt($key)
98
	{
99
		return parent::itemAt($this->_caseSensitive?$key:strtolower($key));
100
	}
101
 
102
 
103
	/**
104
	 * Adds an item into the map.
105
	 * This overrides the parent implementation by converting the key to lower case first if CaseSensitive is false.
106
	 * @param mixed key
107
	 * @param mixed value
108
	 */
109
	public function add($key,$value)
110
	{
111
		parent::add($this->_caseSensitive?$key:strtolower($key),$value);
112
	}
113
 
114
	/**
115
	 * Removes an item from the map by its key.
116
	 * This overrides the parent implementation by converting the key to lower case first if CaseSensitive is false.
117
	 * @param mixed the key of the item to be removed
118
	 * @return mixed the removed value, null if no such key exists.
119
	 */
120
	public function remove($key)
121
	{
122
		return parent::remove($this->_caseSensitive?$key:strtolower($key));
123
	}
124
 
125
	/**
126
	 * Returns whether the specified is in the map.
127
	 * This overrides the parent implementation by converting the key to lower case first if CaseSensitive is false.
128
	 * @param mixed the key
129
	 * @return boolean whether the map contains an item with the specified key
130
	 */
131
	public function contains($key)
132
	{
133
		return parent::contains($this->_caseSensitive?$key:strtolower($key));
134
	}
135
 
136
	/**
137
	 * Determines whether a property is defined.
138
	 * This method overrides parent implementation by returning true
139
	 * if the collection contains the named key.
140
	 * @param string the property name
141
	 * @return boolean whether the property is defined
142
	 */
143
	public function hasProperty($name)
144
	{
145
		return $this->contains($name) || parent::hasProperty($name);
146
	}
147
 
148
	/**
149
	 * Determines whether a property can be read.
150
	 * This method overrides parent implementation by returning true
151
	 * if the collection contains the named key.
152
	 * @param string the property name
153
	 * @return boolean whether the property can be read
154
	 */
155
	public function canGetProperty($name)
156
	{
157
		return $this->contains($name) || parent::canGetProperty($name);
158
	}
159
 
160
	/**
161
	 * Determines whether a property can be set.
162
	 * This method overrides parent implementation by always returning true
163
	 * because you can always add a new value to the collection.
164
	 * @param string the property name
165
	 * @return boolean true
166
	 */
167
	public function canSetProperty($name)
168
	{
169
		return true;
170
	}
171
}
172