| 1 | <?php |
| 2 | |
| 3 | class CRM_Utils_OptionBag implements ArrayAccess, IteratorAggregate, Countable { |
| 4 | protected $data; |
| 5 | |
| 6 | public function __construct($data = array()) { |
| 7 | $this->data = $data; |
| 8 | } |
| 9 | |
| 10 | /** |
| 11 | * @return array |
| 12 | */ |
| 13 | public function getArray() { |
| 14 | return $this->data; |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Retrieve a value from the bag |
| 19 | * |
| 20 | * @param string $key |
| 21 | * @param string|null $type |
| 22 | * @param mixed $default |
| 23 | * @return mixed |
| 24 | * @throws API_Exception |
| 25 | */ |
| 26 | public function get($key, $type = NULL, $default = NULL) { |
| 27 | if (!array_key_exists($key, $this->data)) { |
| 28 | return $default; |
| 29 | } |
| 30 | if (!$type) { |
| 31 | return $this->data[$key]; |
| 32 | } |
| 33 | $r = CRM_Utils_Type::validate($this->data[$key], $type); |
| 34 | if ($r !== NULL) { |
| 35 | return $r; |
| 36 | } |
| 37 | else { |
| 38 | throw new \API_Exception(ts("Could not find valid value for %1 (%2)", array(1 => $key, 2 => $type))); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | public function has($key) { |
| 43 | return isset($this->data[$key]); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * (PHP 5 >= 5.0.0)<br/> |
| 48 | * Whether a offset exists |
| 49 | * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
| 50 | * @param mixed $offset <p> |
| 51 | * An offset to check for. |
| 52 | * </p> |
| 53 | * @return boolean true on success or false on failure. |
| 54 | * </p> |
| 55 | * <p> |
| 56 | * The return value will be casted to boolean if non-boolean was returned. |
| 57 | */ |
| 58 | public function offsetExists($offset) { |
| 59 | return array_key_exists($offset, $this->data); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * (PHP 5 >= 5.0.0)<br/> |
| 64 | * Offset to retrieve |
| 65 | * @link http://php.net/manual/en/arrayaccess.offsetget.php |
| 66 | * @param mixed $offset <p> |
| 67 | * The offset to retrieve. |
| 68 | * </p> |
| 69 | * @return mixed Can return all value types. |
| 70 | */ |
| 71 | public function offsetGet($offset) { |
| 72 | return $this->data[$offset]; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * (PHP 5 >= 5.0.0)<br/> |
| 77 | * Offset to set |
| 78 | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
| 79 | * @param mixed $offset <p> |
| 80 | * The offset to assign the value to. |
| 81 | * </p> |
| 82 | * @param mixed $value <p> |
| 83 | * The value to set. |
| 84 | * </p> |
| 85 | * @return void |
| 86 | */ |
| 87 | public function offsetSet($offset, $value) { |
| 88 | $this->data[$offset] = $value; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * (PHP 5 >= 5.0.0)<br/> |
| 93 | * Offset to unset |
| 94 | * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
| 95 | * @param mixed $offset <p> |
| 96 | * The offset to unset. |
| 97 | * </p> |
| 98 | * @return void |
| 99 | */ |
| 100 | public function offsetUnset($offset) { |
| 101 | unset($this->data[$offset]); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * (PHP 5 >= 5.0.0)<br/> |
| 106 | * Retrieve an external iterator |
| 107 | * @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
| 108 | * @return Traversable An instance of an object implementing <b>Iterator</b> or |
| 109 | * <b>Traversable</b> |
| 110 | */ |
| 111 | public function getIterator() { |
| 112 | return new ArrayIterator($this->data); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * (PHP 5 >= 5.1.0)<br/> |
| 117 | * Count elements of an object |
| 118 | * @link http://php.net/manual/en/countable.count.php |
| 119 | * @return int The custom count as an integer. |
| 120 | * </p> |
| 121 | * <p> |
| 122 | * The return value is cast to an integer. |
| 123 | */ |
| 124 | public function count() { |
| 125 | return count($this->data); |
| 126 | } |
| 127 | |
| 128 | |
| 129 | } |