CTypedMap.php 1.3 KB

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