Completed
Push — master ( 2ed0f3...63bd6b )
by Indra
02:29
created

Validator::createValidatorException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 2
1
<?php
2
3
namespace IndraGunawan\RestService\Validator;
4
5
use IndraGunawan\RestService\Exception\ValidatorException;
6
use Sirius\Validation\Validator as SiriusValidator;
7
8
class Validator
9
{
10
    /**
11
     * @var \Sirius\Validation\Validator
12
     */
13
    private $validator;
14
15
    /**
16
     * @var array
17
     */
18
    private $datas;
19
20
    /**
21
     * @var array
22
     */
23
    private $rules;
24
25 2
    public function __construct()
26
    {
27 2
        $this->validator = new SiriusValidator();
28 2
        $this->rules = [];
29 2
        $this->datas = [];
30 2
    }
31
32
    /**
33
     * Add validator rules.
34
     *
35
     * @param string $name
36
     * @param array  $detail
37
     * @param string $value
38
     */
39 2
    public function add($name, array $detail, $value = '')
40
    {
41 2
        if (!$value) {
42 2
            $value = $detail['defaultValue'];
43
        }
44 2
        $this->datas[$name] = $value;
45
46 2
        if (isset($detail['rule']) && $detail['rule']) {
47 2
            $this->rules[$name] = $detail['rule'];
48
        }
49 2
    }
50
51
    /**
52
     * Check is data valid.
53
     *
54
     * @return bool
55
     */
56 1
    public function isValid()
57
    {
58 1
        return $this->validate($this->datas);
59
    }
60
61
    /**
62
     * Check is input datas valid.
63
     *
64
     * @return bool
65
     */
66 2
    public function validate(array $datas)
67
    {
68 2
        foreach ($this->rules as $field => $rule) {
69 2
            $this->validator->add($field, $rule);
70
        }
71
72 2
        $this->datas = $datas;
73
74 2
        return $this->validator->validate($datas);
75
    }
76
77
    /**
78
     * Get datas.
79
     *
80
     * @return array
81
     */
82 2
    public function getDatas()
83
    {
84 2
        return $this->datas;
85
    }
86
87
    /**
88
     * Get rules.
89
     *
90
     * @return array
91
     */
92 2
    public function getRules()
93
    {
94 2
        return $this->rules;
95
    }
96
97
    /**
98
     * Get all error messages.
99
     *
100
     * @return array
101
     */
102
    public function getMessages()
103
    {
104
        return $this->validator->getMessages();
105
    }
106
107
    /**
108
     * Get first error message.
109
     *
110
     * @return array
111
     */
112
    public function getFirstMessage()
113
    {
114
        $messages = $this->getMessages();
115
        reset($messages);
116
        $field = key($messages);
117
118
        return [
119
            'field' => $field,
120
            'message' => (string) $messages[$field][0],
121
        ];
122
    }
123
124
    /**
125
     * Create ValidatorException.
126
     *
127
     * @return ValidationException
128
     */
129
    public function createValidatorException()
130
    {
131
        $message = $this->getFirstMessage();
132
133
        return new ValidatorException($message['field'], $message['message']);
134
    }
135
}
136