Passed
Push — master ( 68b778...1b1614 )
by Thierry
07:15 queued 04:29
created

Config::_setOptions()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 26
rs 8.8333
cc 7
nc 7
nop 3
1
<?php
2
3
/**
4
 * Config.php - Jaxon config manager
5
 *
6
 * Read and set Jaxon config options.
7
 *
8
 * @package jaxon-core
0 ignored issues
show
Coding Style introduced by
Package name "jaxon-core" is not valid; consider "Jaxoncore" instead
Loading history...
9
 * @author Thierry Feuzeu <[email protected]>
10
 * @copyright 2016 Thierry Feuzeu <[email protected]>
11
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
12
 * @link https://github.com/jaxon-php/jaxon-core
13
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
14
15
namespace Jaxon\Utils\Config;
16
17
class Config
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Config
Loading history...
18
{
19
    /**
20
     * The config options
21
     *
22
     * @var array
23
     */
24
    protected $aOptions = [];
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
25
26
    /**
27
     * The constructor
28
     *
29
     * @param array             $aOptions           The options array
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 13 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 11 found
Loading history...
30
     * @param string            $sKeys              The keys of the options in the array
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 12 found
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter name; 14 found
Loading history...
31
     */
32
    public function __construct(array $aOptions = [], $sKeys = '')
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
33
    {
34
        if(count($aOptions) > 0)
35
        {
36
            $this->setOptions($aOptions, $sKeys);
37
        }
38
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
39
40
    /**
41
     * Set the value of a config option
42
     *
43
     * @param string            $sName              The option name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 12 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter name; 14 found
Loading history...
44
     * @param mixed             $xValue             The option value
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 13 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 13 found
Loading history...
45
     *
46
     * @return void
47
     */
48
    public function setOption($sName, $xValue)
49
    {
50
        $this->aOptions[$sName] = $xValue;
51
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
52
53
    /**
54
     * Recursively set Jaxon options from a data array
55
     *
56
     * @param array             $aOptions           The options array
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter type; 13 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 11 found
Loading history...
57
     * @param string            $sPrefix            The prefix for option names
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 12 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter name; 12 found
Loading history...
58
     * @param integer           $nDepth             The depth from the first call
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 11 found
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter name; 13 found
Loading history...
59
     *
60
     * @return void
61
     */
62
    private function _setOptions(array $aOptions, $sPrefix = '', $nDepth = 0)
63
    {
64
        $sPrefix = trim((string)$sPrefix);
65
        $nDepth = intval($nDepth);
66
        // Check the max depth
67
        if($nDepth < 0 || $nDepth > 9)
68
        {
69
            throw new \Jaxon\Utils\Config\Exception\Data(jaxon_trans('errors.data.depth',
70
                ['key' => $sPrefix, 'depth' => $nDepth]));
71
        }
72
        foreach($aOptions as $sName => $xOption)
73
        {
74
            if(is_int($sName))
75
            {
76
                continue;
77
            }
78
79
            $sName = trim($sName);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
80
            $sFullName = ($sPrefix) ? $sPrefix . '.' . $sName : $sName;
81
            // Save the value of this option
82
            $this->aOptions[$sFullName] = $xOption;
83
            // Save the values of its sub-options
84
            if(is_array($xOption))
85
            {
86
                // Recursively read the options in the array
87
                $this->_setOptions($xOption, $sFullName, $nDepth + 1);
88
            }
89
        }
90
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
91
92
    /**
93
     * Set the values of an array of config options
94
     *
95
     * @param array             $aOptions           The options array
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 13 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 11 found
Loading history...
96
     * @param string            $sKeys              The keys of the options in the array
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 12 found
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter name; 14 found
Loading history...
97
     *
98
     * @return Config
99
     */
100
    public function setOptions(array $aOptions, $sKeys = '')
101
    {
102
        // Find the config array in the input data
103
        $aKeys = explode('.', (string)$sKeys);
104
        foreach($aKeys as $sKey)
105
        {
106
            if(($sKey))
107
            {
108
                if(!array_key_exists($sKey, $aOptions) || !is_array($aOptions[$sKey]))
109
                {
110
                    return $this;
111
                }
112
                $aOptions = $aOptions[$sKey];
113
            }
114
        }
115
        $this->_setOptions($aOptions);
116
117
        return $this;
118
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
119
120
    /**
121
     * Get the value of a config option
122
     *
123
     * @param string            $sName              The option name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 12 found
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter name; 14 found
Loading history...
124
     * @param mixed             $xDefault           The default value, to be returned if the option is not defined
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 13 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 11 found
Loading history...
125
     *
126
     * @return mixed            The option value, or its default value
127
     */
128
    public function getOption($sName, $xDefault = null)
129
    {
130
        return (array_key_exists($sName, $this->aOptions) ? $this->aOptions[$sName] : $xDefault);
131
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
132
133
    /**
134
     * Check the presence of a config option
135
     *
136
     * @param string            $sName              The option name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 12 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 14 found
Loading history...
137
     *
138
     * @return bool             True if the option exists, and false if not
139
     */
140
    public function hasOption($sName)
141
    {
142
        return array_key_exists($sName, $this->aOptions);
143
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
144
145
    /**
146
     * Get the names of the options matching a given prefix
147
     *
148
     * @param string            $sPrefix            The prefix to match
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 12 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 12 found
Loading history...
149
     *
150
     * @return array            The options matching the prefix
151
     */
152
    public function getOptionNames($sPrefix)
153
    {
154
        $sPrefix = trim((string)$sPrefix);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
155
        $sPrefix = rtrim($sPrefix, '.') . '.';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
156
        $sPrefixLen = strlen($sPrefix);
157
        $aOptions = [];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
158
        foreach($this->aOptions as $sName => $xValue)
159
        {
160
            if(substr($sName, 0, $sPrefixLen) == $sPrefix)
161
            {
162
                $iNextDotPos = strpos($sName, '.', $sPrefixLen);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 12 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
163
                $sOptionName = $iNextDotPos === false ?
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 12 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
Coding Style introduced by
Expected 1 space after "?"; newline found
Loading history...
164
                    substr($sName, $sPrefixLen) :
0 ignored issues
show
Coding Style introduced by
Expected 1 space after ":"; newline found
Loading history...
165
                    substr($sName, $sPrefixLen, $iNextDotPos - $sPrefixLen);
166
                $aOptions[$sOptionName] = $sPrefix . $sOptionName;
167
            }
168
        }
169
        return $aOptions;
170
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
171
}
172