Issues (10)

src/ConfigSchema.php (1 issue)

Severity
1
<?php
2
/**
3
 * Bright Nucleus Config Component.
4
 *
5
 * @package   BrightNucleus\Config
6
 * @author    Alain Schlesser <[email protected]>
7
 * @license   MIT
8
 * @link      http://www.brightnucleus.com/
9
 * @copyright 2016-2017 Alain Schlesser, Bright Nucleus
10
 */
11
12
namespace BrightNucleus\Config;
13
14
use BrightNucleus\Exception\InvalidArgumentException;
15
16
/**
17
 * Generic implementation of a configuration requirements check.
18
 *
19
 * @since   0.1.0
20
 *
21
 * @package BrightNucleus\Config
22
 * @author  Alain Schlesser <[email protected]>
23
 */
24
class ConfigSchema extends AbstractConfigSchema
25
{
26
27
    /**
28
     * The key that is used in the schema to define a default value.
29
     */
30
    const DEFAULT_VALUE = 'default';
31
    /**
32
     * The key that is used in the schema to define a required value.
33
     */
34
    const REQUIRED_KEY = 'required';
35
36
    /**
37
     * Instantiate a ConfigSchema object.
38
     *
39
     * @since 0.1.0
40
     *
41
     * @param ConfigInterface|array $schema The schema to parse.
42
     *
43
     * @throws InvalidArgumentException
44
     */
45 1
    public function __construct($schema)
46
    {
47 1
        if ($schema instanceof ConfigInterface) {
48 1
            $schema = $schema->getArrayCopy();
49
        }
50
51 1
        if (! is_array($schema)) {
0 ignored issues
show
The condition is_array($schema) is always true.
Loading history...
52 1
            throw new InvalidArgumentException(
53
                sprintf(
54 1
                    _('Invalid schema source: %1$s'),
55 1
                    print_r($schema, true)
56
                )
57
            );
58
        }
59
60 1
        array_walk($schema, [$this, 'parseSchema']);
61 1
    }
62
63
    /**
64
     * Parse a single provided schema entry.
65
     *
66
     * @since 0.1.0
67
     *
68
     * @param mixed  $data The data associated with the key.
69
     * @param string $key  The key of the schema data.
70
     */
71 1
    protected function parseSchema($data, $key)
72
    {
73 1
        $this->parseDefined($key);
74
75 1
        if (array_key_exists(self::REQUIRED_KEY, $data)) {
76 1
            $this->parseRequired(
77
                $key,
78 1
                $data[self::REQUIRED_KEY]
79
            );
80
        }
81
82 1
        if (array_key_exists(self::DEFAULT_VALUE, $data)) {
83 1
            $this->parseDefault(
84
                $key,
85 1
                $data[self::DEFAULT_VALUE]
86
            );
87
        }
88 1
    }
89
90
    /**
91
     * Parse the set of defined values.
92
     *
93
     * @since 0.1.0
94
     *
95
     * @param string $key The key of the schema data.
96
     */
97 1
    protected function parseDefined($key)
98
    {
99 1
        $this->defined[] = $key;
100 1
    }
101
102
    /**
103
     * Parse the set of required values.
104
     *
105
     * @since 0.1.0
106
     *
107
     * @param string $key  The key of the schema data.
108
     * @param mixed  $data The data associated with the key.
109
     */
110 1
    protected function parseRequired($key, $data)
111
    {
112 1
        if ($this->isTruthy($data)) {
113 1
            $this->required[] = $key;
114
        }
115 1
    }
116
117
    /**
118
     * Parse the set of default values.
119
     *
120
     * @since 0.1.0
121
     *
122
     * @param string $key  The key of the schema data.
123
     * @param mixed  $data The data associated with the key.
124
     */
125 1
    protected function parseDefault($key, $data)
126
    {
127 1
        $this->defaults[$key] = $data;
128 1
    }
129
130
    /**
131
     * Return a boolean true or false for an arbitrary set of data. Recognizes
132
     * several different string values that should be valued as true.
133
     *
134
     * @since 0.1.0
135
     *
136
     * @param mixed $data The data to evaluate.
137
     *
138
     * @return bool
139
     */
140 1
    protected function isTruthy($data)
141
    {
142
        $truthy_values = [
143 1
            true,
144
            1,
145
            'true',
146
            'True',
147
            'TRUE',
148
            'y',
149
            'Y',
150
            'yes',
151
            'Yes',
152
            'YES',
153
            '√',
154
        ];
155
156 1
        return in_array($data, $truthy_values, true);
157
    }
158
}
159