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