-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathXadrArray.php
196 lines (182 loc) · 5.07 KB
/
XadrArray.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
/*
* This file has its roots as part of the Mojavi package which was
* Copyright (c) 2003 Sean Kerr. It has been incorporated into this
* derivative work under the terms of the LGPL V2.1.
* (http://www.gnu.org/licenses/lgpl-2.1.html)
*/
namespace Xmf\Xadr;
/**
* Provide a standard mechanism for a runtime registry for key/value pairs, useful
* for attributes and parameters.
*
* @category Xmf\Xadr\XadrArray
* @package Xmf
* @author Richard Griffith <[email protected]>
* @author Sean Kerr <[email protected]>
* @copyright 2013-2015 XOOPS Project (http://xoops.org)
* @copyright 2003 Sean Kerr
* @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
* @link http://xoops.org
*/
class XadrArray extends \ArrayObject implements \Xoops\Core\AttributeInterface
{
/**
* Retrieve an attribute value.
*
* @param string $name Name of an attribute
* @param mixed $default A default value returned if the requested
* named attribute is not set.
*
* @return mixed The value of the attribute, or $default if not set.
*/
public function get($name, $default = null)
{
if ($this->offsetExists($name)) {
return $this->offsetGet($name);
}
return $default;
}
/**
* Set an attribute value.
*
* @param string $name Name of the attribute option
* @param mixed $value Value of the attribute option
*
* @return void
*/
public function set($name, $value)
{
$this->offsetSet($name, $value);
}
/**
* Get a copy of all attributes
*
* @return array An array of attributes
*/
public function getAll()
{
return $this->getArrayCopy();
}
/**
* Get a list of all attribute names
*
* @return array An array of attribute names/keys
*/
public function getNames()
{
return array_keys((array) $this);
}
/**
* Determine if an attribute exists.
*
* @param string $name An attribute name.
*
* @return boolean TRUE if the given attribute exists, otherwise FALSE.
*/
public function has($name)
{
return $this->offsetExists($name);
}
/**
* Remove an attribute.
*
* @param string $name An attribute name.
*
* @return mixed An attribute value, if the named attribute existed and
* has been removed, otherwise NULL.
*/
public function remove($name)
{
$value = null;
if ($this->offsetExists($name)) {
$value = $this->offsetGet($name);
$this->offsetUnset($name);
}
return $value;
}
/**
* Remove all attributes.
*
* @return array old values
*/
public function clear()
{
return $this->exchangeArray(array());
}
/**
* Replace all attribute with new set
*
* @param mixed $values array (or object) of new attributes
*
* @return array old values
*/
public function setAll($values)
{
$oldValues = $this->exchangeArray($values);
return $oldValues;
}
/**
* Set multiple attributes by using an associative array
*
* @param array $values array of new attributes
*
* @return void
*/
public function setMerge($values)
{
$oldValues = $this->getArrayCopy();
$this->exchangeArray(array_merge($oldValues, $values));
}
/**
* Set an element attribute array
*
* This allows an attribute which is an array to be built one
* element at a time.
*
* @param string $stem An attribute array name.
* @param string $name An attribute array item name. If empty, the
* value will be appended to the end of the
* array rather than added with the key $name.
* @param mixed $value An attribute array item value.
*
* @return void
*/
public function setArrayItem($stem, $name, $value)
{
$newValue = array();
if ($this->offsetExists($stem)) {
$newValue = $this->offsetGet($stem);
if (!is_array($newValue)) {
$newValue = array();
}
}
if (empty($name)) {
$newValue[] = $value;
} else {
$newValue[$name] = $value;
}
$this->offsetSet($stem, $newValue);
}
/**
* Retrieve a set of attributes based on a partial name
*
* @param string|null $nameLike restrict output to only attributes with a name starting with
* this string.
*
* @return array an array of all attributes with names matching $nameLike
*/
public function getAllLike($nameLike = null)
{
if ($nameLike === null) {
return $this->getArrayCopy();
}
$likeSet = array();
foreach ($this as $k => $v) {
if (mb_substr($k, 0, mb_strlen($nameLike))==$nameLike) {
$likeSet[$k]=$v;
}
}
return $likeSet;
}
}