Passed
Push — master ( 20faea...42b488 )
by Lee
01:26
created

Validator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 81
ccs 0
cts 16
cp 0
rs 10
c 0
b 0
f 0
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A isOptional() 0 3 1
A isRequired() 0 3 1
A isValid() 0 13 5
A add() 0 9 2
1
<?php
2
3
namespace Lavibi\Popoya;
4
5
class Validator
6
{
7
    const IS_REQUIRED = 1;
8
9
    const IS_OPTIONAL = 0;
10
11
    /**
12
     * @var ValidatorChain
13
     */
14
    protected $chain;
15
16
    /**
17
     * @var ValidatorChain[]
18
     */
19
    protected $dataValidatorChain;
20
21
    /**
22
     * Index array of data list need to validate
23
     *
24
     * 1 is required
25
     * 0 is optional
26
     *
27
     * @var []
0 ignored issues
show
Documentation Bug introduced by
The doc comment [] at position 0 could not be parsed: Unknown type name '[' at position 0 in [].
Loading history...
28
     */
29
    protected $data;
30
31
    /**
32
     * @var mixed
33
     */
34
    protected $values;
35
36
    /**
37
     * @param $name
38
     *
39
     * @return ValidatorChain
40
     */
41
    public function isRequired($name)
42
    {
43
        return $this->add($name, self::IS_REQUIRED);
44
    }
45
46
    /**
47
     * @param $name
48
     *
49
     * @return ValidatorChain
50
     */
51
    public function isOptional($name)
52
    {
53
        return $this->add($name, self::IS_OPTIONAL);
54
    }
55
56
    public function isValid($values)
57
    {
58
        foreach ($this->data as $name => $isRequired) {
59
            if ($isRequired && !isset($this->$values[$name])) {
60
                return false;
61
            }
62
63
            if (isset($this->$values[$name])) {
64
                $result = $this->dataValidatorChain[$name]->isValid($this->$values[$name]);
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
65
            }
66
        }
67
68
        return true;
69
    }
70
71
    /**
72
     * @param $name
73
     * @param $isRequired
74
     *
75
     * @return ValidatorChain
76
     */
77
    public function add($name, $isRequired)
78
    {
79
        $this->data[$name] = $isRequired;
80
81
        if (!isset($dataValidatorChain[$name])) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dataValidatorChain seems to never exist and therefore isset should always be false.
Loading history...
82
            $this->dataValidatorChain[$name] = new ValidatorChain();
83
        }
84
85
        return $this->dataValidatorChain[$name];
86
    }
87
}
88