Commit | Line | Data |
---|---|---|
70be69e2 | 1 | <?php |
50bfb460 SB |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
50bfb460 | 5 | | | |
bc77d7c0 TO |
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 | | |
50bfb460 SB |
9 | +--------------------------------------------------------------------+ |
10 | */ | |
11 | ||
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
50bfb460 | 16 | */ |
70be69e2 | 17 | |
5bc392e6 | 18 | /** |
b8c71ffa | 19 | * Class CRM_Utils_OptionBag. |
5bc392e6 | 20 | */ |
70be69e2 TO |
21 | class CRM_Utils_OptionBag implements ArrayAccess, IteratorAggregate, Countable { |
22 | protected $data; | |
23 | ||
5bc392e6 EM |
24 | /** |
25 | * @param array $data | |
26 | */ | |
be2fb01f | 27 | public function __construct($data = []) { |
70be69e2 TO |
28 | $this->data = $data; |
29 | } | |
30 | ||
31 | /** | |
32 | * @return array | |
33 | */ | |
34 | public function getArray() { | |
35 | return $this->data; | |
36 | } | |
37 | ||
38 | /** | |
fe482240 | 39 | * Retrieve a value from the bag. |
70be69e2 TO |
40 | * |
41 | * @param string $key | |
42 | * @param string|null $type | |
43 | * @param mixed $default | |
44 | * @return mixed | |
45 | * @throws API_Exception | |
46 | */ | |
47 | public function get($key, $type = NULL, $default = NULL) { | |
48 | if (!array_key_exists($key, $this->data)) { | |
49 | return $default; | |
50 | } | |
51 | if (!$type) { | |
52 | return $this->data[$key]; | |
53 | } | |
54 | $r = CRM_Utils_Type::validate($this->data[$key], $type); | |
55 | if ($r !== NULL) { | |
56 | return $r; | |
57 | } | |
58 | else { | |
be2fb01f | 59 | throw new \API_Exception(ts("Could not find valid value for %1 (%2)", [1 => $key, 2 => $type])); |
70be69e2 TO |
60 | } |
61 | } | |
62 | ||
5bc392e6 | 63 | /** |
a2f24340 | 64 | * @param mixed $key |
5bc392e6 EM |
65 | * |
66 | * @return bool | |
67 | */ | |
70be69e2 TO |
68 | public function has($key) { |
69 | return isset($this->data[$key]); | |
70 | } | |
71 | ||
72 | /** | |
50bfb460 | 73 | * (PHP 5 >= 5.0.0) |
70be69e2 TO |
74 | * Whether a offset exists |
75 | * @link http://php.net/manual/en/arrayaccess.offsetexists.php | |
50bfb460 | 76 | * |
77855840 | 77 | * @param mixed $offset |
50bfb460 SB |
78 | * An offset to check for. |
79 | * | |
3bdca100 | 80 | * @return bool |
a6c01b45 | 81 | * true on success or false on failure. |
3bdca100 | 82 | * The return value will be casted to boolean if non-boolean was returned. |
70be69e2 TO |
83 | */ |
84 | public function offsetExists($offset) { | |
85 | return array_key_exists($offset, $this->data); | |
86 | } | |
87 | ||
88 | /** | |
50bfb460 | 89 | * (PHP 5 >= 5.0.0) |
70be69e2 TO |
90 | * Offset to retrieve |
91 | * @link http://php.net/manual/en/arrayaccess.offsetget.php | |
50bfb460 | 92 | * |
77855840 | 93 | * @param mixed $offset |
50bfb460 SB |
94 | * The offset to retrieve. |
95 | * | |
72b3a70c CW |
96 | * @return mixed |
97 | * Can return all value types. | |
70be69e2 TO |
98 | */ |
99 | public function offsetGet($offset) { | |
100 | return $this->data[$offset]; | |
101 | } | |
102 | ||
103 | /** | |
50bfb460 | 104 | * (PHP 5 >= 5.0.0) |
70be69e2 TO |
105 | * Offset to set |
106 | * @link http://php.net/manual/en/arrayaccess.offsetset.php | |
50bfb460 | 107 | * |
77855840 | 108 | * @param mixed $offset |
50bfb460 SB |
109 | * The offset to assign the value to. |
110 | * | |
77855840 | 111 | * @param mixed $value |
50bfb460 | 112 | * The value to set. |
70be69e2 TO |
113 | */ |
114 | public function offsetSet($offset, $value) { | |
115 | $this->data[$offset] = $value; | |
116 | } | |
117 | ||
118 | /** | |
50bfb460 | 119 | * (PHP 5 >= 5.0.0) |
70be69e2 TO |
120 | * Offset to unset |
121 | * @link http://php.net/manual/en/arrayaccess.offsetunset.php | |
50bfb460 | 122 | * |
77855840 | 123 | * @param mixed $offset |
50bfb460 | 124 | * The offset to unset. |
70be69e2 TO |
125 | */ |
126 | public function offsetUnset($offset) { | |
127 | unset($this->data[$offset]); | |
128 | } | |
129 | ||
130 | /** | |
50bfb460 | 131 | * (PHP 5 >= 5.0.0) |
70be69e2 TO |
132 | * Retrieve an external iterator |
133 | * @link http://php.net/manual/en/iteratoraggregate.getiterator.php | |
50bfb460 | 134 | * |
72b3a70c | 135 | * @return Traversable |
50bfb460 SB |
136 | * An instance of an object implementing Iterator or |
137 | * Traversable | |
70be69e2 TO |
138 | */ |
139 | public function getIterator() { | |
140 | return new ArrayIterator($this->data); | |
141 | } | |
142 | ||
143 | /** | |
50bfb460 | 144 | * (PHP 5 >= 5.1.0) |
70be69e2 TO |
145 | * Count elements of an object |
146 | * @link http://php.net/manual/en/countable.count.php | |
50bfb460 | 147 | * |
a6c01b45 CW |
148 | * @return int |
149 | * The custom count as an integer. | |
3bdca100 | 150 | * The return value is cast to an integer. |
70be69e2 TO |
151 | */ |
152 | public function count() { | |
153 | return count($this->data); | |
154 | } | |
155 | ||
5bc392e6 | 156 | } |