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