| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 10 | | CiviCRM is free software; you can copy, modify, and distribute it | |
| 11 | | under the terms of the GNU Affero General Public License | |
| 12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
| 13 | | | |
| 14 | | CiviCRM is distributed in the hope that it will be useful, but | |
| 15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 17 | | See the GNU Affero General Public License for more details. | |
| 18 | | | |
| 19 | | You should have received a copy of the GNU Affero General Public | |
| 20 | | License and the CiviCRM Licensing Exception along | |
| 21 | | with this program; if not, contact CiviCRM LLC | |
| 22 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 23 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 25 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2016 |
| 32 | */ |
| 33 | |
| 34 | /** |
| 35 | * Class CRM_Utils_OptionBag. |
| 36 | */ |
| 37 | class CRM_Utils_OptionBag implements ArrayAccess, IteratorAggregate, Countable { |
| 38 | protected $data; |
| 39 | |
| 40 | /** |
| 41 | * @param array $data |
| 42 | */ |
| 43 | public function __construct($data = array()) { |
| 44 | $this->data = $data; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @return array |
| 49 | */ |
| 50 | public function getArray() { |
| 51 | return $this->data; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Retrieve a value from the bag. |
| 56 | * |
| 57 | * @param string $key |
| 58 | * @param string|null $type |
| 59 | * @param mixed $default |
| 60 | * @return mixed |
| 61 | * @throws API_Exception |
| 62 | */ |
| 63 | public function get($key, $type = NULL, $default = NULL) { |
| 64 | if (!array_key_exists($key, $this->data)) { |
| 65 | return $default; |
| 66 | } |
| 67 | if (!$type) { |
| 68 | return $this->data[$key]; |
| 69 | } |
| 70 | $r = CRM_Utils_Type::validate($this->data[$key], $type); |
| 71 | if ($r !== NULL) { |
| 72 | return $r; |
| 73 | } |
| 74 | else { |
| 75 | throw new \API_Exception(ts("Could not find valid value for %1 (%2)", array(1 => $key, 2 => $type))); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * @param $key |
| 81 | * |
| 82 | * @return bool |
| 83 | */ |
| 84 | public function has($key) { |
| 85 | return isset($this->data[$key]); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * (PHP 5 >= 5.0.0) |
| 90 | * Whether a offset exists |
| 91 | * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
| 92 | * |
| 93 | * @param mixed $offset |
| 94 | * An offset to check for. |
| 95 | * |
| 96 | * @return bool |
| 97 | * true on success or false on failure. |
| 98 | * The return value will be casted to boolean if non-boolean was returned. |
| 99 | */ |
| 100 | public function offsetExists($offset) { |
| 101 | return array_key_exists($offset, $this->data); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * (PHP 5 >= 5.0.0) |
| 106 | * Offset to retrieve |
| 107 | * @link http://php.net/manual/en/arrayaccess.offsetget.php |
| 108 | * |
| 109 | * @param mixed $offset |
| 110 | * The offset to retrieve. |
| 111 | * |
| 112 | * @return mixed |
| 113 | * Can return all value types. |
| 114 | */ |
| 115 | public function offsetGet($offset) { |
| 116 | return $this->data[$offset]; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * (PHP 5 >= 5.0.0) |
| 121 | * Offset to set |
| 122 | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
| 123 | * |
| 124 | * @param mixed $offset |
| 125 | * The offset to assign the value to. |
| 126 | * |
| 127 | * @param mixed $value |
| 128 | * The value to set. |
| 129 | */ |
| 130 | public function offsetSet($offset, $value) { |
| 131 | $this->data[$offset] = $value; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * (PHP 5 >= 5.0.0) |
| 136 | * Offset to unset |
| 137 | * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
| 138 | * |
| 139 | * @param mixed $offset |
| 140 | * The offset to unset. |
| 141 | */ |
| 142 | public function offsetUnset($offset) { |
| 143 | unset($this->data[$offset]); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * (PHP 5 >= 5.0.0) |
| 148 | * Retrieve an external iterator |
| 149 | * @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
| 150 | * |
| 151 | * @return Traversable |
| 152 | * An instance of an object implementing Iterator or |
| 153 | * Traversable |
| 154 | */ |
| 155 | public function getIterator() { |
| 156 | return new ArrayIterator($this->data); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * (PHP 5 >= 5.1.0) |
| 161 | * Count elements of an object |
| 162 | * @link http://php.net/manual/en/countable.count.php |
| 163 | * |
| 164 | * @return int |
| 165 | * The custom count as an integer. |
| 166 | * The return value is cast to an integer. |
| 167 | */ |
| 168 | public function count() { |
| 169 | return count($this->data); |
| 170 | } |
| 171 | |
| 172 | } |