Passed
Push — master ( a2ac4d...245815 )
by Lee
01:42
created

Validator::getMessageCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 1
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Lavibi\Popoya;
4
5
class Validator implements ValidatorInterface
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
     * Error message
23
     *
24
     * @var string
25
     */
26
    protected $message;
27
28
    /**
29
     * Error code
30
     *
31
     * @var string
32
     */
33
    protected $messageCode;
34
35
    /**
36
     * Index array of data list need to validate
37
     *
38
     * 1 is required
39
     * 0 is optional
40
     *
41
     * @var string[]
42
     */
43
    protected $data;
44
45
    /**
46
     * @var mixed[]
47
     */
48
    protected $values;
49
50
    /**
51
     * @param $name
52
     *
53
     * @return ValidatorChain
54
     */
55 1
    public function isRequired($name)
56
    {
57 1
        return $this->add($name, static::IS_REQUIRED);
58
    }
59
60
    /**
61
     * @param $name
62
     *
63
     * @return ValidatorChain
64
     */
65 2
    public function isOptional($name)
66
    {
67 2
        return $this->add($name, static::IS_OPTIONAL);
68
    }
69
70 2
    public function isValid($values)
71
    {
72 2
        foreach ($this->data as $name => $isRequired) {
73 2
            if ($isRequired === static::IS_OPTIONAL) {
74 2
                if (!isset($values[$name])) {
75 2
                    continue;
76
                }
77
            }
78
79 2
            if (!isset($values[$name])) {
80 1
                return false;
81
            }
82
83 2
            if (!$this->dataValidatorChain[$name]->isValid($values[$name])) {
84
                return false;
85
            }
86
        }
87
88 2
        return true;
89
    }
90
91
    public function getMessage()
92
    {
93
        // TODO: Implement getMessage() method.
94
    }
95
96
    public function getMessageCode()
97
    {
98
        // TODO: Implement getMessageCode() method.
99
    }
100
101
    public function getStandardValue()
102
    {
103
        // TODO: Implement getStandardValue() method.
104
    }
105
106
    /**
107
     * @param $name
108
     * @param $isRequired
109
     *
110
     * @return ValidatorChain
111
     */
112 2
    public function add($name, $isRequired)
113
    {
114 2
        $this->data[$name] = $isRequired;
115
116 2
        if (!isset($this->dataValidatorChain[$name])) {
117 2
            $this->dataValidatorChain[$name] = new ValidatorChain();
118
        }
119
120 2
        return $this->dataValidatorChain[$name];
121
    }
122
}
123