3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * Class CRM_Utils_OptionBag.
21 class CRM_Utils_OptionBag
implements ArrayAccess
, IteratorAggregate
, Countable
{
27 public function __construct($data = []) {
34 public function getArray() {
39 * Retrieve a value from the bag.
42 * @param string|null $type
43 * @param mixed $default
45 * @throws API_Exception
47 public function get($key, $type = NULL, $default = NULL) {
48 if (!array_key_exists($key, $this->data
)) {
52 return $this->data
[$key];
54 $r = CRM_Utils_Type
::validate($this->data
[$key], $type);
59 throw new \
API_Exception(ts("Could not find valid value for %1 (%2)", [1 => $key, 2 => $type]));
68 public function has($key) {
69 return isset($this->data
[$key]);
74 * Whether a offset exists
75 * @link http://php.net/manual/en/arrayaccess.offsetexists.php
77 * @param mixed $offset
78 * An offset to check for.
81 * true on success or false on failure.
82 * The return value will be casted to boolean if non-boolean was returned.
84 public function offsetExists($offset) {
85 return array_key_exists($offset, $this->data
);
91 * @link http://php.net/manual/en/arrayaccess.offsetget.php
93 * @param mixed $offset
94 * The offset to retrieve.
97 * Can return all value types.
99 public function offsetGet($offset) {
100 return $this->data
[$offset];
104 * (PHP 5 >= 5.0.0)
106 * @link http://php.net/manual/en/arrayaccess.offsetset.php
108 * @param mixed $offset
109 * The offset to assign the value to.
111 * @param mixed $value
114 public function offsetSet($offset, $value) {
115 $this->data
[$offset] = $value;
119 * (PHP 5 >= 5.0.0)
121 * @link http://php.net/manual/en/arrayaccess.offsetunset.php
123 * @param mixed $offset
124 * The offset to unset.
126 public function offsetUnset($offset) {
127 unset($this->data
[$offset]);
131 * (PHP 5 >= 5.0.0)
132 * Retrieve an external iterator
133 * @link http://php.net/manual/en/iteratoraggregate.getiterator.php
135 * @return Traversable
136 * An instance of an object implementing Iterator or
139 public function getIterator() {
140 return new ArrayIterator($this->data
);
144 * (PHP 5 >= 5.1.0)
145 * Count elements of an object
146 * @link http://php.net/manual/en/countable.count.php
149 * The custom count as an integer.
150 * The return value is cast to an integer.
152 public function count() {
153 return count($this->data
);