3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2017
35 * Class CRM_Utils_OptionBag.
37 class CRM_Utils_OptionBag
implements ArrayAccess
, IteratorAggregate
, Countable
{
43 public function __construct($data = array()) {
50 public function getArray() {
55 * Retrieve a value from the bag.
58 * @param string|null $type
59 * @param mixed $default
61 * @throws API_Exception
63 public function get($key, $type = NULL, $default = NULL) {
64 if (!array_key_exists($key, $this->data
)) {
68 return $this->data
[$key];
70 $r = CRM_Utils_Type
::validate($this->data
[$key], $type);
75 throw new \
API_Exception(ts("Could not find valid value for %1 (%2)", array(1 => $key, 2 => $type)));
84 public function has($key) {
85 return isset($this->data
[$key]);
90 * Whether a offset exists
91 * @link http://php.net/manual/en/arrayaccess.offsetexists.php
93 * @param mixed $offset
94 * An offset to check for.
97 * true on success or false on failure.
98 * The return value will be casted to boolean if non-boolean was returned.
100 public function offsetExists($offset) {
101 return array_key_exists($offset, $this->data
);
105 * (PHP 5 >= 5.0.0)
107 * @link http://php.net/manual/en/arrayaccess.offsetget.php
109 * @param mixed $offset
110 * The offset to retrieve.
113 * Can return all value types.
115 public function offsetGet($offset) {
116 return $this->data
[$offset];
120 * (PHP 5 >= 5.0.0)
122 * @link http://php.net/manual/en/arrayaccess.offsetset.php
124 * @param mixed $offset
125 * The offset to assign the value to.
127 * @param mixed $value
130 public function offsetSet($offset, $value) {
131 $this->data
[$offset] = $value;
135 * (PHP 5 >= 5.0.0)
137 * @link http://php.net/manual/en/arrayaccess.offsetunset.php
139 * @param mixed $offset
140 * The offset to unset.
142 public function offsetUnset($offset) {
143 unset($this->data
[$offset]);
147 * (PHP 5 >= 5.0.0)
148 * Retrieve an external iterator
149 * @link http://php.net/manual/en/iteratoraggregate.getiterator.php
151 * @return Traversable
152 * An instance of an object implementing Iterator or
155 public function getIterator() {
156 return new ArrayIterator($this->data
);
160 * (PHP 5 >= 5.1.0)
161 * Count elements of an object
162 * @link http://php.net/manual/en/countable.count.php
165 * The custom count as an integer.
166 * The return value is cast to an integer.
168 public function count() {
169 return count($this->data
);