Passed
Push — master ( 0dab70...882871 )
by Alain
02:20
created

AbstractConfig::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 2
Metric Value
c 2
b 0
f 2
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * Abstract Config Object
4
 *
5
 * @package   BrightNucleus\Config
6
 * @author    Alain Schlesser <[email protected]>
7
 * @license   GPL-2.0+
8
 * @link      http://www.brightnucleus.com/
9
 * @copyright 2016 Alain Schlesser, Bright Nucleus
10
 */
11
12
namespace BrightNucleus\Config;
13
14
use ArrayObject;
15
use OutOfRangeException;
16
use BadMethodCallException;
17
use BrightNucleus\Config\ConfigInterface;
18
19
/**
20
 * Config loader used to load config PHP files as objects.
21
 *
22
 * @since      0.1.0
23
 *
24
 * @package    BrightNucleus\Config
25
 * @author     Alain Schlesser <[email protected]>
26
 */
27
abstract class AbstractConfig extends ArrayObject implements ConfigInterface
28
{
29
30
    /**
31
     * Instantiate the AbstractConfig object.
32
     *
33
     * @since 0.1.0
34
     *
35
     * @param  array $config Array with settings.
36
     */
37 1
    public function __construct(array $config)
38
    {
39
        // Make sure the config entries can be accessed as properties.
40 1
        parent::__construct($config, ArrayObject::ARRAY_AS_PROPS);
41 1
    }
42
43
    /**
44
     * Check whether the Config has a specific key.
45
     *
46
     * To check a value several levels deep, add the keys for each level as a comma-separated list.
47
     *
48
     * @since 0.1.0
49
     * @since 0.1.4 Accepts list of keys.
50
     *
51
     * @param string ... List of keys.
52
     * @return bool
53
     */
54 2
    public function hasKey()
55
    {
56 2
        $keys = array_reverse(func_get_args());
57
58 2
        $array = $this->getArrayCopy();
59 2
        while (count($keys) > 0) {
60 2
            $key = array_pop($keys);
61 2
            if ( ! array_key_exists($key, $array)) {
62 2
                return false;
63
            }
64 2
            $array = $array[$key];
65
        }
66
67 2
        return true;
68
    }
69
70
    /**
71
     * Get the value of a specific key.
72
     *
73
     * To get a value several levels deep, add the keys for each level as a comma-separated list.
74
     *
75
     * @since 0.1.0
76
     * @since 0.1.4 Accepts list of keys.
77
     *
78
     * @param string ... List of keys.
79
     * @return mixed
80
     * @throws BadMethodCallException If no argument was provided.
81
     * @throws OutOfRangeException If an unknown key is requested.
82
     */
83 2
    public function getKey()
84
    {
85 2
        if (func_num_args() < 1) {
86
            throw new BadMethodCallException(_('No configuration was provided to getKey().'));
87
        }
88
89 2
        $keys = func_get_args();
90
91 2
        if ( ! call_user_func_array([$this, 'hasKey'], $keys)) {
92 2
            throw new OutOfRangeException(sprintf(_('The configuration key %1$s does not exist.'),
93 2
                implode('->', $keys)));
94
        }
95
96 2
        $keys  = array_reverse($keys);
97 2
        $array = $this->getArrayCopy();
98 2
        while (count($keys) > 0) {
99 2
            $key   = array_pop($keys);
100 2
            $array = $array[$key];
101
        }
102
103 2
        return $array;
104
    }
105
106
    /**
107
     * Get a (multi-dimensional) array of all the configuration settings.
108
     *
109
     * @since 0.1.4
110
     *
111
     * @return array
112
     */
113 1
    public function getAll()
114
    {
115 1
        return $this->getArrayCopy();
116
    }
117
118
    /**
119
     * Get the an array with all the keys
120
     *
121
     * @since 0.1.0
122
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<integer|string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
123
     */
124 1
    public function getKeys()
125
    {
126 1
        return array_keys((array)$this);
127
    }
128
129
    /**
130
     * Validate the Config file.
131
     *
132
     * @since  0.1.0
133
     * @return boolean
134
     */
135
    abstract public function isValid();
136
}
137