Completed
Pull Request — master (#491)
by Richard
10:43
created

ConfigurationAbstract::getAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
ccs 2
cts 2
cp 1
crap 1
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\XoopsArray;
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 <[email protected]>
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 XoopsArray
28
{
29
    /**
30
     * Get a copy of all attributes
31
     *
32
     * @return array An array of attributes
33
     */
34 1
    public function getAll()
35
    {
36 1
        return $this->getArrayCopy();
37
    }
38
39
    /**
40
     * Get a list of all attribute names
41
     *
42
     * @return array An array of attribute names/keys
43
     */
44 1
    public function getNames()
45
    {
46 1
        return array_keys((array) $this);
47
    }
48
49
    /**
50
     * Replace all attribute with new set
51
     *
52
     * @param mixed $values array (or object) of new attributes
53
     *
54
     * @return array old values
55
     */
56 1
    public function setAll($values)
57
    {
58 1
        $oldValues = $this->exchangeArray($values);
59 1
        return $oldValues;
60
    }
61
62
    /**
63
     * Set multiple attributes by using an associative array
64
     *
65
     * @param array $values array of new attributes
66
     *
67
     * @return void
68
     */
69 1
    public function setMerge($values)
70
    {
71 1
        $oldValues = $this->getArrayCopy();
72 1
        $this->exchangeArray(array_merge($oldValues, $values));
73 1
    }
74
75
    /**
76
     * Set an element attribute array
77
     *
78
     * This allows an attribute which is an array to be built one
79
     * element at a time.
80
     *
81
     * @param string $stem  An attribute array name.
82
     * @param string $name  An attribute array item name. If empty, the
83
     *                      value will be appended to the end of the
84
     *                      array rather than added with the key $name.
85
     * @param mixed  $value An attribute array item value.
86
     *
87
     * @return void
88
     */
89 1
    public function setArrayItem($stem, $name, $value)
90
    {
91 1
        $newValue = array();
92 1 View Code Duplication
        if ($this->offsetExists($stem)) {
93 1
            $newValue = $this->offsetGet($stem);
94 1
            if (!is_array($newValue)) {
95 1
                $newValue = array();
96
            }
97
        }
98 1
        if (empty($name)) {
99 1
            $newValue[] = $value;
100
        } else {
101 1
            $newValue[$name] = $value;
102
        }
103 1
        $this->offsetSet($stem, $newValue);
104 1
    }
105
106
    /**
107
     * Retrieve a set of attributes based on a partial name
108
     *
109
     * @param string|null $nameLike restrict output to only attributes with a name starting with
110
     *                              this string.
111
     *
112
     * @return array an array of all attributes with names matching $nameLike
113
     */
114 1 View Code Duplication
    public function getAllLike($nameLike = null)
115
    {
116 1
        if ($nameLike === null) {
117 1
            return $this->getArrayCopy();
118
        }
119
120 1
        $likeSet = array();
121 1
        foreach ($this as $k => $v) {
122 1
            if (mb_substr($k, 0, mb_strlen($nameLike))==$nameLike) {
123 1
                $likeSet[$k]=$v;
124
            }
125
        }
126 1
        return $likeSet;
127
    }
128
}
129