1: <?php
2: /**
3: * This file contains CTypedMap class.
4: *
5: * @author Qiang Xue <qiang.xue@gmail.com>
6: * @link http://www.yiiframework.com/
7: * @copyright 2008-2013 Yii Software LLC
8: * @license http://www.yiiframework.com/license/
9: */
10:
11: /**
12: * CTypedMap represents a map whose items are of the certain type.
13: *
14: * CTypedMap extends {@link CMap} by making sure that the elements to be
15: * added to the list is of certain class type.
16: *
17: * @author Qiang Xue <qiang.xue@gmail.com>
18: * @package system.collections
19: * @since 1.0
20: */
21: class CTypedMap extends CMap
22: {
23: private $_type;
24:
25: /**
26: * Constructor.
27: * @param string $type class type
28: */
29: public function __construct($type)
30: {
31: $this->_type=$type;
32: }
33:
34: /**
35: * Adds an item into the map.
36: * This method overrides the parent implementation by
37: * checking the item to be inserted is of certain type.
38: * @param integer $index the specified position.
39: * @param mixed $item new item
40: * @throws CException If the index specified exceeds the bound,
41: * the map is read-only or the element is not of the expected type.
42: */
43: public function add($index,$item)
44: {
45: if($item instanceof $this->_type)
46: parent::add($index,$item);
47: else
48: throw new CException(Yii::t('yii','CTypedMap<{type}> can only hold objects of {type} class.',
49: array('{type}'=>$this->_type)));
50: }
51: }
52: