Completed
Pull Request — master (#578)
by Fabio
19:18 queued 07:13
created

TAttributeCollection::__getZappableSleepProps()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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