Completed
Push — master ( 40f739...c90891 )
by Maxim
03:13
created

ConfigSchema::constraints()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
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
     * @throws ConfigValidationException
112
     */
113
    public function assert() {
114
        $check = $this->check();
115
116
        if ($check->isFailed()) {
117
            $message = 'Configuration is not valid. ';
118
119
            foreach ($check->getErrors() as $index => $error) {
120
                $message .= s(
121
                    "\n%s: %s ",
122
                    $error->getSubject(),
123
                    $error->getMessage()
124
                );
125
            }
126
127
            throw new ConfigValidationException($message, $check);
128
        }
129
    }
130
131
    /**
132
     * @return IValidationResult
133
     */
134
    public function check() {
135
        return $this->validator->check(
136
            $this->config->toArray()
137
        );
138
    }
139
140
    /**
141
     * @param string $key
142
     * @param IConstraint $constraint
143
     *
144
     * @return IConfigSchema
145
     */
146
    public function constraint($key, IConstraint $constraint) {
147
        $this->validator->addConstraint($key, $constraint);
148
149
        return $this;
150
    }
151
152
    /**
153
     * @param string $key
154
     * @param IConstraint[] $constraints
155
     *
156
     * @return IConfigSchema
157
     */
158
    public function constraints($key, array $constraints) {
159
        $this->validator->addConstraints($key, $constraints);
160
161
        return $this;
162
    }
163
164
    /**
165
     * @return IValidator
166
     */
167
    protected function createValidator() {
168
        return new Validator();
169
    }
170
}
171