Completed
Push — master ( 6fb51e...94364b )
by Thierry
01:40
created

Config::hasOption()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Config.php - Jaxon config manager
5
 *
6
 * Read and set Jaxon config options.
7
 *
8
 * @package jaxon-core
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
 */
14
15
namespace Jaxon\Config;
16
17
class Config
18
{
19
    protected $aOptions = [];
20
21
    /**
22
     * Set the value of a config option
23
     *
24
     * @param string            $sName              The option name
25
     * @param mixed             $xValue             The option value
26
     *
27
     * @return void
28
     */
29
    public function setOption($sName, $xValue)
30
    {
31
        $this->aOptions[$sName] = $xValue;
32
    }
33
34
    /**
35
     * Recursively set Jaxon options from a data array
36
     *
37
     * @param array             $aOptions           The options array
38
     * @param string            $sPrefix            The prefix for option names
39
     * @param integer           $nDepth             The depth from the first call
40
     *
41
     * @return void
42
     */
43
    private function _setOptions(array $aOptions, $sPrefix = '', $nDepth = 0)
44
    {
45
        $sPrefix = trim((string)$sPrefix);
46
        $nDepth = intval($nDepth);
47
        // Check the max depth
48
        if($nDepth < 0 || $nDepth > 9)
49
        {
50
            throw new \Jaxon\Config\Exception\Data(jaxon_trans('config.errors.data.depth',
51
                array('key' => $sPrefix, 'depth' => $nDepth)));
52
        }
53
        foreach($aOptions as $sName => $xOption)
54
        {
55
            if(is_int($sName))
56
            {
57
                continue;
58
            }
59
60
            $sName = trim($sName);
61
            $sFullName = ($sPrefix) ? $sPrefix . '.' . $sName : $sName;
62
            // Save the value of this option
63
            $this->aOptions[$sFullName] = $xOption;
64
            // Save the values of its sub-options
65
            if(is_array($xOption))
66
            {
67
                // Recursively read the options in the array
68
                $this->_setOptions($xOption, $sFullName, $nDepth + 1);
69
            }
70
        }
71
    }
72
73
    /**
74
     * Set the values of an array of config options
75
     *
76
     * @param array             $aOptions           The options array
77
     * @param string            $sKeys              The keys of the options in the array
78
     *
79
     * @return void
80
     */
81
    public function setOptions(array $aOptions, $sKeys = '')
82
    {
83
        // Find the config array in the input data
84
        $aKeys = explode('.', (string)$sKeys);
85
        foreach ($aKeys as $sKey)
86
        {
87
            if(($sKey))
88
            {
89
                if(!array_key_exists($sKey, $aOptions) || !is_array($aOptions[$sKey]))
90
                {
91
                    return;
92
                }
93
                $aOptions = $aOptions[$sKey];
94
            }
95
        }
96
        $this->_setOptions($aOptions);
97
    }
98
99
    /**
100
     * Get the value of a config option
101
     *
102
     * @param string            $sName              The option name
103
     * @param mixed             $xDefault           The default value, to be returned if the option is not defined
104
     *
105
     * @return mixed            The option value, or its default value
106
     */
107
    public function getOption($sName, $xDefault = null)
108
    {
109
        return (array_key_exists($sName, $this->aOptions) ? $this->aOptions[$sName] : $xDefault);
110
    }
111
112
    /**
113
     * Check the presence of a config option
114
     *
115
     * @param string            $sName              The option name
116
     *
117
     * @return bool             True if the option exists, and false if not
118
     */
119
    public function hasOption($sName)
120
    {
121
        return array_key_exists($sName, $this->aOptions);
122
    }
123
124
    /**
125
     * Get the names of the options matching a given prefix
126
     *
127
     * @param string            $sPrefix            The prefix to match
128
     *
129
     * @return array            The options matching the prefix
130
     */
131
    public function getOptionNames($sPrefix)
132
    {
133
        $sPrefix = trim((string)$sPrefix);
134
        $sPrefix = rtrim($sPrefix, '.') . '.';
135
        $sPrefixLen = strlen($sPrefix);
136
        $aOptions = [];
137
        foreach($this->aOptions as $sName => $xValue)
138
        {
139
            if(substr($sName, 0, $sPrefixLen) == $sPrefix)
140
            {
141
                $iNextDotPos = strpos($sName, '.', $sPrefixLen);
142
                $sOptionName = $iNextDotPos === false ? substr($sName, $sPrefixLen) :
143
                    substr($sName, $sPrefixLen, $iNextDotPos - $sPrefixLen);
144
                $aOptions[$sOptionName] = $sPrefix . $sOptionName;
145
            }
146
        }
147
        return $aOptions;
148
    }
149
}
150