Merge pull request #15665 from MikeyMJCO/patch-1
[civicrm-core.git] / CRM / Utils / OptionBag.php
CommitLineData
70be69e2 1<?php
50bfb460
SB
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
50bfb460 5 +--------------------------------------------------------------------+
f299f7db 6 | Copyright CiviCRM LLC (c) 2004-2020 |
50bfb460
SB
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28/**
29 *
30 * @package CRM
f299f7db 31 * @copyright CiviCRM LLC (c) 2004-2020
50bfb460 32 */
70be69e2 33
5bc392e6 34/**
b8c71ffa 35 * Class CRM_Utils_OptionBag.
5bc392e6 36 */
70be69e2
TO
37class CRM_Utils_OptionBag implements ArrayAccess, IteratorAggregate, Countable {
38 protected $data;
39
5bc392e6
EM
40 /**
41 * @param array $data
42 */
be2fb01f 43 public function __construct($data = []) {
70be69e2
TO
44 $this->data = $data;
45 }
46
47 /**
48 * @return array
49 */
50 public function getArray() {
51 return $this->data;
52 }
53
54 /**
fe482240 55 * Retrieve a value from the bag.
70be69e2
TO
56 *
57 * @param string $key
58 * @param string|null $type
59 * @param mixed $default
60 * @return mixed
61 * @throws API_Exception
62 */
63 public function get($key, $type = NULL, $default = NULL) {
64 if (!array_key_exists($key, $this->data)) {
65 return $default;
66 }
67 if (!$type) {
68 return $this->data[$key];
69 }
70 $r = CRM_Utils_Type::validate($this->data[$key], $type);
71 if ($r !== NULL) {
72 return $r;
73 }
74 else {
be2fb01f 75 throw new \API_Exception(ts("Could not find valid value for %1 (%2)", [1 => $key, 2 => $type]));
70be69e2
TO
76 }
77 }
78
5bc392e6
EM
79 /**
80 * @param $key
81 *
82 * @return bool
83 */
70be69e2
TO
84 public function has($key) {
85 return isset($this->data[$key]);
86 }
87
88 /**
50bfb460 89 * (PHP 5 &gt;= 5.0.0)
70be69e2
TO
90 * Whether a offset exists
91 * @link http://php.net/manual/en/arrayaccess.offsetexists.php
50bfb460 92 *
77855840 93 * @param mixed $offset
50bfb460
SB
94 * An offset to check for.
95 *
3bdca100 96 * @return bool
a6c01b45 97 * true on success or false on failure.
3bdca100 98 * The return value will be casted to boolean if non-boolean was returned.
70be69e2
TO
99 */
100 public function offsetExists($offset) {
101 return array_key_exists($offset, $this->data);
102 }
103
104 /**
50bfb460 105 * (PHP 5 &gt;= 5.0.0)
70be69e2
TO
106 * Offset to retrieve
107 * @link http://php.net/manual/en/arrayaccess.offsetget.php
50bfb460 108 *
77855840 109 * @param mixed $offset
50bfb460
SB
110 * The offset to retrieve.
111 *
72b3a70c
CW
112 * @return mixed
113 * Can return all value types.
70be69e2
TO
114 */
115 public function offsetGet($offset) {
116 return $this->data[$offset];
117 }
118
119 /**
50bfb460 120 * (PHP 5 &gt;= 5.0.0)
70be69e2
TO
121 * Offset to set
122 * @link http://php.net/manual/en/arrayaccess.offsetset.php
50bfb460 123 *
77855840 124 * @param mixed $offset
50bfb460
SB
125 * The offset to assign the value to.
126 *
77855840 127 * @param mixed $value
50bfb460 128 * The value to set.
70be69e2
TO
129 */
130 public function offsetSet($offset, $value) {
131 $this->data[$offset] = $value;
132 }
133
134 /**
50bfb460 135 * (PHP 5 &gt;= 5.0.0)
70be69e2
TO
136 * Offset to unset
137 * @link http://php.net/manual/en/arrayaccess.offsetunset.php
50bfb460 138 *
77855840 139 * @param mixed $offset
50bfb460 140 * The offset to unset.
70be69e2
TO
141 */
142 public function offsetUnset($offset) {
143 unset($this->data[$offset]);
144 }
145
146 /**
50bfb460 147 * (PHP 5 &gt;= 5.0.0)
70be69e2
TO
148 * Retrieve an external iterator
149 * @link http://php.net/manual/en/iteratoraggregate.getiterator.php
50bfb460 150 *
72b3a70c 151 * @return Traversable
50bfb460
SB
152 * An instance of an object implementing Iterator or
153 * Traversable
70be69e2
TO
154 */
155 public function getIterator() {
156 return new ArrayIterator($this->data);
157 }
158
159 /**
50bfb460 160 * (PHP 5 &gt;= 5.1.0)
70be69e2
TO
161 * Count elements of an object
162 * @link http://php.net/manual/en/countable.count.php
50bfb460 163 *
a6c01b45
CW
164 * @return int
165 * The custom count as an integer.
3bdca100 166 * The return value is cast to an integer.
70be69e2
TO
167 */
168 public function count() {
169 return count($this->data);
170 }
171
5bc392e6 172}