Merge pull request #4124 from tohojo/adv-search-fix
[civicrm-core.git] / CRM / Utils / OptionBag.php
1 <?php
2
3 /**
4 * Class CRM_Utils_OptionBag
5 */
6 class CRM_Utils_OptionBag implements ArrayAccess, IteratorAggregate, Countable {
7 protected $data;
8
9 /**
10 * @param array $data
11 */
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 /**
24 * Retrieve a value from the bag
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
48 /**
49 * @param $key
50 *
51 * @return bool
52 */
53 public function has($key) {
54 return isset($this->data[$key]);
55 }
56
57 /**
58 * (PHP 5 &gt;= 5.0.0)<br/>
59 * Whether a offset exists
60 * @link http://php.net/manual/en/arrayaccess.offsetexists.php
61 * @param mixed $offset <p>
62 * An offset to check for.
63 * </p>
64 * @return boolean true on success or false on failure.
65 * </p>
66 * <p>
67 * The return value will be casted to boolean if non-boolean was returned.
68 */
69 public function offsetExists($offset) {
70 return array_key_exists($offset, $this->data);
71 }
72
73 /**
74 * (PHP 5 &gt;= 5.0.0)<br/>
75 * Offset to retrieve
76 * @link http://php.net/manual/en/arrayaccess.offsetget.php
77 * @param mixed $offset <p>
78 * The offset to retrieve.
79 * </p>
80 * @return mixed Can return all value types.
81 */
82 public function offsetGet($offset) {
83 return $this->data[$offset];
84 }
85
86 /**
87 * (PHP 5 &gt;= 5.0.0)<br/>
88 * Offset to set
89 * @link http://php.net/manual/en/arrayaccess.offsetset.php
90 * @param mixed $offset <p>
91 * The offset to assign the value to.
92 * </p>
93 * @param mixed $value <p>
94 * The value to set.
95 * </p>
96 * @return void
97 */
98 public function offsetSet($offset, $value) {
99 $this->data[$offset] = $value;
100 }
101
102 /**
103 * (PHP 5 &gt;= 5.0.0)<br/>
104 * Offset to unset
105 * @link http://php.net/manual/en/arrayaccess.offsetunset.php
106 * @param mixed $offset <p>
107 * The offset to unset.
108 * </p>
109 * @return void
110 */
111 public function offsetUnset($offset) {
112 unset($this->data[$offset]);
113 }
114
115 /**
116 * (PHP 5 &gt;= 5.0.0)<br/>
117 * Retrieve an external iterator
118 * @link http://php.net/manual/en/iteratoraggregate.getiterator.php
119 * @return Traversable An instance of an object implementing <b>Iterator</b> or
120 * <b>Traversable</b>
121 */
122 public function getIterator() {
123 return new ArrayIterator($this->data);
124 }
125
126 /**
127 * (PHP 5 &gt;= 5.1.0)<br/>
128 * Count elements of an object
129 * @link http://php.net/manual/en/countable.count.php
130 * @return int The custom count as an integer.
131 * </p>
132 * <p>
133 * The return value is cast to an integer.
134 */
135 public function count() {
136 return count($this->data);
137 }
138
139
140 }