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