ConfigSchema::hasNumber()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace Weew\ConfigSchema;
4
5
use Weew\Config\IConfig;
6
use Weew\ConfigSchema\Exceptions\ConfigValidationException;
7
use Weew\ConfigSchema\Nodes\ArrayNode;
8
use Weew\ConfigSchema\Nodes\BooleanNode;
9
use Weew\ConfigSchema\Nodes\IArrayNode;
10
use Weew\ConfigSchema\Nodes\IBooleanNode;
11
use Weew\ConfigSchema\Nodes\INumericNode;
12
use Weew\ConfigSchema\Nodes\IStringNode;
13
use Weew\ConfigSchema\Nodes\IValueNode;
14
use Weew\ConfigSchema\Nodes\NumericNode;
15
use Weew\ConfigSchema\Nodes\StringNode;
16
use Weew\ConfigSchema\Nodes\ValueNode;
17
use Weew\Validator\IConstraint;
18
use Weew\Validator\IValidationResult;
19
use Weew\Validator\IValidator;
20
use Weew\Validator\Validator;
21
22
class ConfigSchema implements IConfigSchema {
23
    /**
24
     * @var IConfig
25
     */
26
    protected $config;
27
28
    /**
29
     * @var IValidator
30
     */
31
    protected $validator;
32
33
    /**
34
     * ConfigSchema constructor.
35
     *
36
     * @param IConfig $config
37
     * @param IValidator $validator
38
     */
39
    public function __construct(IConfig $config, IValidator $validator = null) {
40
        if ( ! $validator instanceof IValidator) {
41
            $validator = $this->createValidator();
42
        }
43
44
        $this->config = $config;
45
        $this->validator = $validator;
46
    }
47
48
    /**
49
     * @param string $key
50
     * @param string $message
51
     *
52
     * @return IValueNode
53
     */
54
    public function hasValue($key, $message = null) {
55
        return new ValueNode($this, $key, $message);
56
    }
57
58
    /**
59
     * @param string $key
60
     * @param string $message
61
     *
62
     * @return IStringNode
63
     */
64
    public function hasString($key, $message = null) {
65
        return new StringNode($this, $key, $message);
66
    }
67
68
    /**
69
     * @param string $key
70
     * @param string $message
71
     *
72
     * @return INumericNode
73
     */
74
    public function hasNumber($key, $message = null) {
75
        return new NumericNode($this, $key, $message);
76
    }
77
78
    /**
79
     * @param string $key
80
     * @param string $message
81
     *
82
     * @return IBooleanNode
83
     */
84
    public function hasBoolean($key, $message = null) {
85
        return new BooleanNode($this, $key, $message);
86
    }
87
88
    /**
89
     * @param string $key
90
     * @param string $message
91
     *
92
     * @return IArrayNode
93
     */
94
    public function hasArray($key, $message = null) {
95
        return new ArrayNode($this, $key, $message);
96
    }
97
98
    /**
99
     * @param string $key
100
     * @param string $message
101
     *
102
     * @return IStringNode
103
     */
104
    public function hasArrayKeys($key, $message = null) {
105
        $key = s('%s.#', $key);
106
107
        return new StringNode($this, $key, $message);
108
    }
109
110
    /**
111
     * @param string $key
112
     * @param string $message
113
     *
114
     * @return StringNode
115
     */
116
    public function hasArrayValues($key, $message = null) {
117
        $key = s('%s.*', $key);
118
119
        return new StringNode($this, $key, $message);
120
    }
121
122
    /**
123
     * @throws ConfigValidationException
124
     */
125
    public function assert() {
126
        $check = $this->check();
127
128
        if ($check->isFailed()) {
129
            $message = 'Configuration is not valid. ';
130
131
            foreach ($check->getErrors() as $index => $error) {
132
                $message .= s(
133
                    "\n%s: %s ",
134
                    $error->getSubject(),
135
                    $error->getMessage()
136
                );
137
            }
138
139
            throw new ConfigValidationException($message, $check);
140
        }
141
    }
142
143
    /**
144
     * @return IValidationResult
145
     */
146
    public function check() {
147
        return $this->validator->check(
148
            $this->config->toArray()
149
        );
150
    }
151
152
    /**
153
     * @param string $key
154
     * @param IConstraint $constraint
155
     *
156
     * @return IConfigSchema
157
     */
158
    public function constraint($key, IConstraint $constraint) {
159
        $this->validator->addConstraint($key, $constraint);
160
161
        return $this;
162
    }
163
164
    /**
165
     * @param string $key
166
     * @param IConstraint[] $constraints
167
     *
168
     * @return IConfigSchema
169
     */
170
    public function constraints($key, array $constraints) {
171
        $this->validator->addConstraints($key, $constraints);
172
173
        return $this;
174
    }
175
176
    /**
177
     * @return IValidator
178
     */
179
    protected function createValidator() {
180
        return new Validator();
181
    }
182
}
183