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

Parser   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 3
dl 0
loc 75
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A isAssociative() 0 4 1
B parse() 0 15 8
B getRule() 0 16 6
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
 * Parses rule definitions
25
 *
26
 * @since 3.0.0
27
 */
28
class Parser
29
{
30
    /**
31
     * @var \Caridea\Validate\Registry $registry
32
     */
33
    private $registry;
34
35
    /**
36
     * Creates a new Validation rule registry.
37
     *
38
     * @param \Caridea\Validate\Registry $registry The registry
39
     */
40 2
    public function __construct(Registry $registry)
41
    {
42 2
        $this->registry = $registry;
43 2
    }
44
45
    /**
46
     * Tests if an array is associative.
47
     *
48
     * @param array $array The array to test
49
     * @return bool Whether the array is associative
50
     */
51 1
    public function isAssociative(array $array): bool
52
    {
53 1
        return count(array_filter(array_keys($array), 'is_string')) > 0;
54
    }
55
56
    /**
57
     * Turns a number of rule definitions into an actual Rule Set.
58
     *
59
     * @param string|object|array $rules The rules to parse
60
     * @return \Caridea\Validate\Rule\Set The set of Rules
61
     */
62 4
    public function parse($rules): Rule\Set
63
    {
64 4
        $isArray = is_array($rules);
65 4
        $isAssoc = $isArray && $this->isAssociative($rules);
66 4
        $set = null;
67 4
        if (is_string($rules) || is_object($rules) || $isAssoc) {
68 2
            $set = $this->getRule($rules);
69 2
        } elseif ($isArray) {
70 2
            foreach ($rules as $v) {
71 2
                $toAdd = $this->getRule($v);
72 2
                $set = $set === null ? $toAdd : $set->merge($toAdd);
73
            }
74
        }
75 4
        return $set ?? new Rule\Set();
76
    }
77
78
    /**
79
     * Parses rule definitions.
80
     *
81
     * @param string|object|array $rule Either a string name, an associative
82
     *        array, or an object with name → arguments
83
     * @param mixed $arg Optional constructor argument, or an array of arguments
84
     * @return \Caridea\Validate\Rule\Set An set of instantiated rules
85
     */
86 10
    public function getRule($rule, $arg = null): Rule\Set
87
    {
88 10
        $rules = new Rule\Set();
89 10
        if (is_string($rule)) {
90 10
            $vrule = $this->registry->factory($rule, $arg);
91 9
            if ($vrule instanceof Draft) {
92 5
                $vrule = $vrule->finish($this->registry);
93
            }
94 9
            $rules->add($vrule);
95 8
        } elseif (is_object($rule) || is_array($rule)) {
96 8
            foreach ($rule as $name => $args) {
97 8
                $rules->merge($this->getRule($name, $args));
98
            }
99
        }
100 9
        return $rules;
101
    }
102
}
103