Completed
Push — master ( 77b530...acb106 )
by Jonathan
02:05
created

Builder   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 68
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A field() 0 5 1
B build() 0 10 5
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Caridea
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7
 * use this file except in compliance with the License. You may obtain a copy of
8
 * the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
 * License for the specific language governing permissions and limitations under
16
 * the License.
17
 *
18
 * @copyright 2015-2018 LibreWorks contributors
19
 * @license   Apache-2.0
20
 */
21
namespace Caridea\Validate;
22
23
/**
24
 * Builds validators.
25
 *
26
 * @copyright 2015-2018 LibreWorks contributors
27
 * @license   Apache-2.0
28
 */
29
class Builder
30
{
31
    /**
32
     * @var \Caridea\Validate\Parser
33
     */
34
    private $parser;
35
    /**
36
     * @var array<string,\Caridea\Validate\Rule\Set>
37
     */
38
    private $validators = [];
39
40
    /**
41
     * Creates a new Validation Builder.
42
     *
43
     * @param \Caridea\Validate\Parser $parser The parser.
44
     */
45 2
    public function __construct(Parser $parser)
46
    {
47 2
        $this->parser = $parser;
48 2
    }
49
50
    /**
51
     * Adds one or more rules to this builder.
52
     *
53
     * @param string $field The field to validate
54
     * @param string|object|array $rules Either a string name, an associative
55
     *        array, or an object with name → arguments
56
     * @return $this provides a fluent interface
57
     */
58 1
    public function field(string $field, ...$rules): self
59
    {
60 1
        $this->validators[$field] = $this->parser->parse($rules);
61 1
        return $this;
62
    }
63
64
    /**
65
     * Builds a validator for the provided ruleset.
66
     *
67
     * ```javascript
68
     * // rules.json
69
     * {
70
     *     name: 'required',
71
     *     email: ['required', 'email'],
72
     *     drinks: { one_of: [['coffee', 'tea']] },
73
     *     phone: {max_length: 10}
74
     * }
75
     * ```
76
     * ```php
77
     * $ruleset = json_decode(file_get_contents('rules.json'));
78
     * $builder = new \Caridea\Validate\Builder();
79
     * $validator = $builder->build($ruleset);
80
     * ```
81
     *
82
     * @param object|array $ruleset Object or associative array (as returned
83
     *        from `json_decode`) with ruleset, or `null` to use defined rules.
84
     * @return \Caridea\Validate\Validator the built validator
85
     */
86 2
    public function build($ruleset = null): Validator
87
    {
88 2
        $validators = array_merge([], $this->validators);
89 2
        if (is_object($ruleset) || (is_array($ruleset) && $this->parser->isAssociative($ruleset))) {
90 1
            foreach ($ruleset as $field => $rules) {
0 ignored issues
show
Bug introduced by
The expression $ruleset of type array|null|object is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
91 1
                $validators[$field] = $this->parser->parse($rules);
92
            }
93
        }
94 2
        return new Validator($validators);
95
    }
96
}
97