Dictionary.php 8.89 KB
Newer Older
w  
Qiang Xue committed
1 2
<?php
/**
Qiang Xue committed
3
 * Dictionary class file.
w  
Qiang Xue committed
4 5
 *
 * @link http://www.yiiframework.com/
Qiang Xue committed
6
 * @copyright Copyright &copy; 2008 Yii Software LLC
w  
Qiang Xue committed
7 8 9
 * @license http://www.yiiframework.com/license/
 */

Qiang Xue committed
10 11
namespace yii\base;

Qiang Xue committed
12 13
use yii\util\ArrayHelper;

w  
Qiang Xue committed
14
/**
Qiang Xue committed
15
 * Dictionary implements a collection that stores key-value pairs.
w  
Qiang Xue committed
16 17
 *
 * You can access, add or remove an item with a key by using
Qiang Xue committed
18
 * [[itemAt()]], [[add()]], and [[remove()]].
Qiang Xue committed
19
 *
Qiang Xue committed
20
 * To get the number of the items in the dictionary, use [[getCount()]].
Qiang Xue committed
21 22 23 24
 *
 * Because Dictionary implements a set of SPL interfaces, it can be used
 * like a regular PHP array as follows,
 *
w  
Qiang Xue committed
25
 * ~~~
Qiang Xue committed
26 27 28
 * $dictionary[$key] = $value;		   // add a key-value pair
 * unset($dictionary[$key]);			 // remove the value with the specified key
 * if (isset($dictionary[$key]))		 // if the dictionary contains the key
Qiang Xue committed
29
 * foreach ($dictionary as $key=>$value) // traverse the items in the dictionary
Qiang Xue committed
30
 * $n = count($dictionary);			  // returns the number of items in the dictionary
Qiang Xue committed
31
 * ~~~
w  
Qiang Xue committed
32
 *
Qiang Xue committed
33
 * @property integer $count the number of items in the dictionary
Qiang Xue committed
34
 * @property array $keys The keys in the dictionary
Qiang Xue committed
35
 *
w  
Qiang Xue committed
36
 * @author Qiang Xue <qiang.xue@gmail.com>
Qiang Xue committed
37
 * @since 2.0
w  
Qiang Xue committed
38
 */
39
class Dictionary extends Object implements \IteratorAggregate, \ArrayAccess, \Countable
w  
Qiang Xue committed
40 41
{
	/**
Qiang Xue committed
42
	 * @var array internal data storage
w  
Qiang Xue committed
43
	 */
Qiang Xue committed
44
	private $_d = array();
w  
Qiang Xue committed
45 46 47

	/**
	 * Constructor.
Qiang Xue committed
48 49 50
	 * Initializes the dictionary with an array or an iterable object.
	 * @param mixed $data the initial data to be populated into the dictionary.
	 * This can be an array or an iterable object.
Qiang Xue committed
51
	 * @param array $config name-value pairs that will be used to initialize the object properties
Qiang Xue committed
52
	 * @throws Exception if data is not well formed (neither an array nor an iterable object)
w  
Qiang Xue committed
53
	 */
Qiang Xue committed
54
	public function __construct($data = array(), $config = array())
w  
Qiang Xue committed
55
	{
Qiang Xue committed
56
		if ($data !== array()) {
w  
Qiang Xue committed
57
			$this->copyFrom($data);
Qiang Xue committed
58
		}
Qiang Xue committed
59
		parent::__construct($config);
w  
Qiang Xue committed
60 61 62
	}

	/**
Qiang Xue committed
63 64 65 66
	 * Returns an iterator for traversing the items in the dictionary.
	 * This method is required by the SPL interface `IteratorAggregate`.
	 * It will be implicitly called when you use `foreach` to traverse the dictionary.
	 * @return DictionaryIterator an iterator for traversing the items in the dictionary.
w  
Qiang Xue committed
67 68 69
	 */
	public function getIterator()
	{
Qiang Xue committed
70
		return new DictionaryIterator($this->_d);
w  
Qiang Xue committed
71 72 73
	}

	/**
Qiang Xue committed
74 75 76 77
	 * Returns the number of items in the dictionary.
	 * This method is required by the SPL `Countable` interface.
	 * It will be implicitly called when you use `count($dictionary)`.
	 * @return integer number of items in the dictionary.
w  
Qiang Xue committed
78 79 80 81 82 83 84
	 */
	public function count()
	{
		return $this->getCount();
	}

	/**
Qiang Xue committed
85 86
	 * Returns the number of items in the dictionary.
	 * @return integer the number of items in the dictionary
w  
Qiang Xue committed
87 88 89 90 91 92 93
	 */
	public function getCount()
	{
		return count($this->_d);
	}

	/**
Qiang Xue committed
94
	 * Returns the keys stored in the dictionary.
w  
Qiang Xue committed
95 96 97 98 99 100 101 102 103 104
	 * @return array the key list
	 */
	public function getKeys()
	{
		return array_keys($this->_d);
	}

	/**
	 * Returns the item with the specified key.
	 * @param mixed $key the key
Qiang Xue committed
105 106
	 * @return mixed the element with the specified key.
	 * Null if the key cannot be found in the dictionary.
w  
Qiang Xue committed
107 108 109
	 */
	public function itemAt($key)
	{
Qiang Xue committed
110
		return isset($this->_d[$key]) ? $this->_d[$key] : null;
w  
Qiang Xue committed
111 112 113
	}

	/**
Qiang Xue committed
114
	 * Adds an item into the dictionary.
w  
Qiang Xue committed
115 116 117
	 * Note, if the specified key already exists, the old value will be overwritten.
	 * @param mixed $key key
	 * @param mixed $value value
Qiang Xue committed
118
	 * @throws Exception if the dictionary is read-only
w  
Qiang Xue committed
119
	 */
Qiang Xue committed
120
	public function add($key, $value)
w  
Qiang Xue committed
121
	{
w  
Qiang Xue committed
122 123
		if ($key === null) {
			$this->_d[] = $value;
Qiang Xue committed
124
		} else {
w  
Qiang Xue committed
125
			$this->_d[$key] = $value;
w  
Qiang Xue committed
126 127 128 129
		}
	}

	/**
Qiang Xue committed
130
	 * Removes an item from the dictionary by its key.
w  
Qiang Xue committed
131 132
	 * @param mixed $key the key of the item to be removed
	 * @return mixed the removed value, null if no such key exists.
Qiang Xue committed
133
	 * @throws Exception if the dictionary is read-only
w  
Qiang Xue committed
134 135 136
	 */
	public function remove($key)
	{
w  
Qiang Xue committed
137 138 139 140
		if (isset($this->_d[$key])) {
			$value = $this->_d[$key];
			unset($this->_d[$key]);
			return $value;
Qiang Xue committed
141
		} else { // the value is null
w  
Qiang Xue committed
142 143
			unset($this->_d[$key]);
			return null;
Qiang Xue committed
144
		}
w  
Qiang Xue committed
145 146 147
	}

	/**
148 149 150 151
	 * Removes all items from the dictionary.
	 * @param boolean $safeClear whether to clear every item by calling [[remove]].
	 * Defaults to false, meaning all items in the dictionary will be cleared directly
	 * without calling [[remove]].
w  
Qiang Xue committed
152
	 */
153
	public function clear($safeClear = false)
w  
Qiang Xue committed
154
	{
155 156 157 158
		if ($safeClear) {
			foreach (array_keys($this->_d) as $key) {
				$this->remove($key);
			}
Qiang Xue committed
159
		} else {
160
			$this->_d = array();
Qiang Xue committed
161
		}
w  
Qiang Xue committed
162 163 164
	}

	/**
Qiang Xue committed
165
	 * Returns a value indicating whether the dictionary contains the specified key.
w  
Qiang Xue committed
166
	 * @param mixed $key the key
Qiang Xue committed
167
	 * @return boolean whether the dictionary contains an item with the specified key
w  
Qiang Xue committed
168 169 170
	 */
	public function contains($key)
	{
Qiang Xue committed
171
		return isset($this->_d[$key]) || array_key_exists($key, $this->_d);
w  
Qiang Xue committed
172 173 174
	}

	/**
Qiang Xue committed
175
	 * Returns the dictionary as a PHP array.
w  
Qiang Xue committed
176 177 178 179 180 181 182 183
	 * @return array the list of items in array
	 */
	public function toArray()
	{
		return $this->_d;
	}

	/**
Qiang Xue committed
184 185 186
	 * Copies iterable data into the dictionary.
	 * Note, existing data in the dictionary will be cleared first.
	 * @param mixed $data the data to be copied from, must be an array or an object implementing `Traversable`
Qiang Xue committed
187
	 * @throws InvalidCallException if data is neither an array nor an iterator.
w  
Qiang Xue committed
188
	 */
Qiang Xue committed
189
	public function copyFrom($data)
w  
Qiang Xue committed
190
	{
Qiang Xue committed
191
		if (is_array($data) || $data instanceof \Traversable) {
192
			if ($this->_d !== array()) {
w  
Qiang Xue committed
193
				$this->clear();
Qiang Xue committed
194 195 196 197 198 199 200
			}
			if ($data instanceof self) {
				$data = $data->_d;
			}
			foreach ($data as $key => $value) {
				$this->add($key, $value);
			}
Qiang Xue committed
201
		} else {
Qiang Xue committed
202
			throw new InvalidCallException('Data must be either an array or an object implementing Traversable.');
w  
Qiang Xue committed
203 204 205 206
		}
	}

	/**
Qiang Xue committed
207
	 * Merges iterable data into the dictionary.
w  
Qiang Xue committed
208
	 *
Qiang Xue committed
209
	 * Existing elements in the dictionary will be overwritten if their keys are the same as those in the source.
w  
Qiang Xue committed
210 211
	 * If the merge is recursive, the following algorithm is performed:
	 *
Qiang Xue committed
212 213 214 215
	 * - the dictionary data is saved as $a, and the source data is saved as $b;
	 * - if $a and $b both have an array indexed at the same string key, the arrays will be merged using this algorithm;
	 * - any integer-indexed elements in $b will be appended to $a;
	 * - any string-indexed elements in $b will overwrite elements in $a with the same index;
w  
Qiang Xue committed
216
	 *
Qiang Xue committed
217 218
	 * @param array|\Traversable $data the data to be merged with. It must be an array or object implementing Traversable
	 * @param boolean $recursive whether the merging should be recursive.
Qiang Xue committed
219
	 * @throws InvalidCallException if data is neither an array nor an object implementing `Traversable`.
w  
Qiang Xue committed
220
	 */
Qiang Xue committed
221
	public function mergeWith($data, $recursive = true)
w  
Qiang Xue committed
222
	{
223 224 225 226 227 228
		if (is_array($data) || $data instanceof \Traversable) {
			if ($data instanceof self) {
				$data = $data->_d;
			}
			if ($recursive) {
				if ($data instanceof \Traversable) {
Qiang Xue committed
229 230
					$d = array();
					foreach ($data as $key => $value) {
231 232
						$d[$key] = $value;
					}
Qiang Xue committed
233
					$this->_d = ArrayHelper::merge($this->_d, $d);
Qiang Xue committed
234
				} else {
Qiang Xue committed
235
					$this->_d = ArrayHelper::merge($this->_d, $data);
w  
Qiang Xue committed
236
				}
Qiang Xue committed
237
			} else {
Qiang Xue committed
238
				foreach ($data as $key => $value) {
239 240
					$this->add($key, $value);
				}
w  
Qiang Xue committed
241
			}
Qiang Xue committed
242
		} else {
Qiang Xue committed
243
			throw new InvalidCallException('The data to be merged with must be an array or an object implementing Traversable.');
244
		}
w  
Qiang Xue committed
245 246 247 248
	}

	/**
	 * Returns whether there is an element at the specified offset.
Qiang Xue committed
249
	 * This method is required by the SPL interface `ArrayAccess`.
w  
Qiang Xue committed
250
	 * It is implicitly called when you use something like `isset($dictionary[$offset])`.
Qiang Xue committed
251
	 * This is equivalent to [[contains]].
w  
Qiang Xue committed
252 253 254 255 256 257 258 259 260 261
	 * @param mixed $offset the offset to check on
	 * @return boolean
	 */
	public function offsetExists($offset)
	{
		return $this->contains($offset);
	}

	/**
	 * Returns the element at the specified offset.
Qiang Xue committed
262
	 * This method is required by the SPL interface `ArrayAccess`.
w  
Qiang Xue committed
263
	 * It is implicitly called when you use something like `$value = $dictionary[$offset];`.
Qiang Xue committed
264
	 * This is equivalent to [[itemAt]].
w  
Qiang Xue committed
265
	 * @param mixed $offset the offset to retrieve element.
w  
Qiang Xue committed
266 267 268 269 270 271 272 273 274
	 * @return mixed the element at the offset, null if no element is found at the offset
	 */
	public function offsetGet($offset)
	{
		return $this->itemAt($offset);
	}

	/**
	 * Sets the element at the specified offset.
Qiang Xue committed
275
	 * This method is required by the SPL interface `ArrayAccess`.
w  
Qiang Xue committed
276
	 * It is implicitly called when you use something like `$dictionary[$offset] = $item;`.
Qiang Xue committed
277 278 279 280
	 * If the offset is null, the new item will be appended to the dictionary.
	 * Otherwise, the existing item at the offset will be replaced with the new item.
	 * This is equivalent to [[add]].
	 * @param mixed $offset the offset to set element
w  
Qiang Xue committed
281 282
	 * @param mixed $item the element value
	 */
Qiang Xue committed
283
	public function offsetSet($offset, $item)
w  
Qiang Xue committed
284
	{
Qiang Xue committed
285
		$this->add($offset, $item);
w  
Qiang Xue committed
286 287 288 289
	}

	/**
	 * Unsets the element at the specified offset.
Qiang Xue committed
290
	 * This method is required by the SPL interface `ArrayAccess`.
w  
Qiang Xue committed
291
	 * It is implicitly called when you use something like `unset($dictionary[$offset])`.
Qiang Xue committed
292
	 * This is equivalent to [[remove]].
w  
Qiang Xue committed
293 294 295 296 297 298 299
	 * @param mixed $offset the offset to unset element
	 */
	public function offsetUnset($offset)
	{
		$this->remove($offset);
	}
}