Completed
Push — master ( 54a6ce...5c7adb )
by Harry
47:33 queued 45:24
created

ValidatorBuilderTrait::getBuildContext()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
/**
3
 * This file is part of graze/config-validation.
4
 *
5
 * Copyright (c) 2017 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/config-validation/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/config-validation
12
 */
13
14
namespace Graze\ConfigValidation;
15
16
use Respect\Validation\Validatable;
17
use Respect\Validation\Validator as v;
18
19
trait ValidatorBuilderTrait
20
{
21
    /** @var string */
22
    protected $separator = '.';
23
24
    /**
25
     * @var array [path][here]['required' => bool, 'validator' => Validator, 'default' => mixed]
26
     */
27
    protected $validators = [];
28
29
    /**
30
     * @var bool
31
     */
32
    protected $dirty = true;
33
34
    /**
35
     * @var Validatable[]
36
     */
37
    protected $validator = [];
38
39
    /**
40
     * @return mixed
41
     */
42 2
    public function getBuildContext()
43
    {
44 2
        return $this->validators;
45
    }
46
47
    /**
48
     * @param string           $path
49
     * @param bool             $required
50
     * @param Validatable|null $validator
51
     * @param mixed|null       $default
52
     *
53
     * @return $this
54
     */
55 24
    protected function addValidator($path, $required, Validatable $validator = null, $default = null)
56
    {
57 24
        $parent = &$this->validators;
58 24
        foreach (explode($this->separator, $path) as $node) {
59 24
            if (!isset($parent[$node])) {
60 24
                $parent[$node] = [];
61
            }
62 24
            $parent = &$parent[$node];
63
        }
64
65 24
        $parent['required'] = $required;
66 24
        $parent['validator'] = $validator;
67 24
        $parent['default'] = $default;
68
69 24
        $this->dirty = true;
70
71 24
        return $this;
72
    }
73
74
    /**
75
     * @param string                   $path
76
     * @param ConfigValidatorInterface $childConfig
77
     *
78
     * @return $this
79
     */
80 2
    public function addChild($path, ConfigValidatorInterface $childConfig)
81
    {
82
83 2
        $parent = &$this->validators;
84 2
        foreach (explode($this->separator, $path) as $node) {
85 2
            if (!isset($parent[$node])) {
86 2
                $parent[$node] = [];
87
            }
88 2
            $parent = &$parent[$node];
89
        }
90
91 2
        $parent = $childConfig->getBuildContext();
92
93 2
        $this->dirty = true;
94
95 2
        return $this;
96
    }
97
98
    /**
99
     * @param string      $path
100
     * @param Validatable $validator
101
     *
102
     * @return $this
103
     */
104 24
    public function required($path, Validatable $validator = null)
105
    {
106 24
        $this->addValidator($path, true, $validator);
107 24
        return $this;
108
    }
109
110
    /**
111
     * @param string      $path
112
     * @param Validatable $validator
113
     * @param mixed|null  $default
114
     *
115
     * @return $this
116
     */
117 24
    public function optional($path, Validatable $validator = null, $default = null)
118
    {
119 24
        if (!is_null($validator) && is_null($default)) {
120 4
            $validator = v::oneOf($validator, v::nullType());
121
        }
122
123 24
        $this->addValidator($path, false, $validator, $default);
124 24
        return $this;
125
    }
126
127
    /**
128
     * @param string $name
129
     *
130
     * @return Validatable
131
     */
132 24
    public function getValidator($name = '')
133
    {
134 24
        if ($this->dirty || !isset($this->validator[$name]) || $this->validator[$name] == null) {
135 24
            $this->validator[$name] = $this->buildValidator($this->validators, $name);
136 24
            $this->dirty = false;
137
        }
138
139 24
        return $this->validator[$name];
140
    }
141
142
    /**
143
     * @return string
144
     */
145 4
    public function getSeparator()
146
    {
147 4
        return $this->separator;
148
    }
149
150
    /**
151
     * @param string $separator
152
     *
153
     * @return $this
154
     */
155 2
    public function setSeparator($separator)
156
    {
157 2
        $this->separator = $separator;
158 2
        return $this;
159
    }
160
161
    /**
162
     * @param array  $definition
163
     * @param string $namePrefix
164
     *
165
     * @return Validatable
166
     */
167
    protected abstract function buildValidator(array $definition, $namePrefix = '');
168
169
    /**
170
     * Traverse the definition to see if any children are required
171
     *
172
     * @param array $definition
173
     *
174
     * @return bool
175
     */
176 19
    protected function hasMandatoryItem(array $definition)
177
    {
178 19
        foreach ($definition as $key => $node) {
179 19
            if (isset($node['required'])) {
180 19
                if ($node['required']) {
181 19
                    return true;
182
                }
183
            } else {
184 1
                if ($this->hasMandatoryItem($node)) {
185 17
                    return true;
186
                }
187
            }
188
        }
189
190 16
        return false;
191
    }
192
}
193