Completed
Push — master ( 004e52...d9f32b )
by
unknown
04:06 queued 31s
created

Validation::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
ccs 6
cts 6
cp 1
crap 1
rs 9.4285
1
<?php
2
3
namespace PluginSimpleValidate;
4
5
use PluginSimpleValidate\Contracts\BaseField;
6
use PluginSimpleValidate\Libraries\Language;
7
8
class Validation implements \PluginSimpleValidate\Contracts\Validation
9
{
10
    /**
11
     * @var array
12
     * array of Field
13
     */
14
    private $fields;
15
16
    /**
17
     * @var array
18
     */
19
    private $errors;
20
21
    /**
22
     * @var Language
23
     */
24
    private $language;
25
26
    /**
27
     * @var bool
28
     */
29
    private $status;
30
31
    /**
32
     * Validation constructor.
33
     * @param Language $language
34
     */
35 6
    public function __construct(Language $language)
36
    {
37 6
        $this->language = $language;
38 6
        $this->fields = [];
39 6
        $this->errors = [];
40 6
        $this->status = false;
41 6
    }
42
43
    /**
44
     * @param BaseField $field
45
     * @return $this
46
     */
47 6
    public function addField(BaseField $field)
48
    {
49 6
        $this->fields[$field->getName()] = $field;
50 6
        return $this;
51
    }
52
53
    /**
54
     * @param bool $break_when_error
55
     * @return bool
56
     */
57 6 View Code Duplication
    public function run($break_when_error = false) : bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
    {
59 6
        $this->emptyErrors();
60
61
        /** @var Field $field */
62 6
        foreach ($this->fields as $field) {
63 6
            if (!$field->isValid($this->language)) {
64 6
                $this->status = false;
65 6
                $this->errors[$field->getName()] = $field->getErrors();
66
67
                /**
68
                 * break when there is any field error
69
                 */
70 6
                if ($break_when_error) {
71 6
                    break;
72
                }
73
            }
74
        }
75
76 6
        if (empty($this->errors)) {
77
            $this->status = true;
78
        }
79
80 6
        return $this->status;
81
    }
82
83
    /**
84
     * @return array
85
     */
86 6
    public function getErrors()
87
    {
88 6
        return $this->errors;
89
    }
90
91
    /**
92
     * @return $this
93
     */
94 6
    protected function emptyErrors()
95
    {
96 6
        $this->errors = [];
97 6
        return $this;
98
    }
99
100
    /**
101
     * @param string $fieldName
102
     * @return array|Field|null
103
     */
104
    public function getFields(string $fieldName = '')
105
    {
106
        if (empty($fieldName)) {
107
            return $this->fields;
108
        }
109
110
        return isset($this->fields[$fieldName]) ? $this->fields[$fieldName] : null;
111
    }
112
113
    /**
114
     * @return bool
115
     */
116 1
    public function getStatus(): bool
117
    {
118 1
        return $this->status;
119
    }
120
}