4 * Class CRM_Utils_OptionBag
6 class CRM_Utils_OptionBag
implements ArrayAccess
, IteratorAggregate
, Countable
{
12 public function __construct($data = array()) {
19 public function getArray() {
24 * Retrieve a value from the bag.
27 * @param string|null $type
28 * @param mixed $default
30 * @throws API_Exception
32 public function get($key, $type = NULL, $default = NULL) {
33 if (!array_key_exists($key, $this->data
)) {
37 return $this->data
[$key];
39 $r = CRM_Utils_Type
::validate($this->data
[$key], $type);
44 throw new \
API_Exception(ts("Could not find valid value for %1 (%2)", array(1 => $key, 2 => $type)));
53 public function has($key) {
54 return isset($this->data
[$key]);
58 * (PHP 5 >= 5.0.0)<br/>
59 * Whether a offset exists
60 * @link http://php.net/manual/en/arrayaccess.offsetexists.php
61 * @param mixed $offset
63 * An offset to check for.
66 * true on success or false on failure.
69 * The return value will be casted to boolean if non-boolean was returned.
71 public function offsetExists($offset) {
72 return array_key_exists($offset, $this->data
);
76 * (PHP 5 >= 5.0.0)<br/>
78 * @link http://php.net/manual/en/arrayaccess.offsetget.php
79 * @param mixed $offset
81 * The offset to retrieve.
84 * Can return all value types.
86 public function offsetGet($offset) {
87 return $this->data
[$offset];
91 * (PHP 5 >= 5.0.0)<br/>
93 * @link http://php.net/manual/en/arrayaccess.offsetset.php
94 * @param mixed $offset
96 * The offset to assign the value to.
104 public function offsetSet($offset, $value) {
105 $this->data
[$offset] = $value;
109 * (PHP 5 >= 5.0.0)<br/>
111 * @link http://php.net/manual/en/arrayaccess.offsetunset.php
112 * @param mixed $offset
114 * The offset to unset.
118 public function offsetUnset($offset) {
119 unset($this->data
[$offset]);
123 * (PHP 5 >= 5.0.0)<br/>
124 * Retrieve an external iterator
125 * @link http://php.net/manual/en/iteratoraggregate.getiterator.php
126 * @return Traversable
127 * An instance of an object implementing <b>Iterator</b> or
130 public function getIterator() {
131 return new ArrayIterator($this->data
);
135 * (PHP 5 >= 5.1.0)<br/>
136 * Count elements of an object
137 * @link http://php.net/manual/en/countable.count.php
139 * The custom count as an integer.
142 * The return value is cast to an integer.
144 public function count() {
145 return count($this->data
);