Completed
Push — master ( 121fac...aa6513 )
by Harry Osmar
06:34 queued 03:36
created

Validation::addField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
1
<?php
2
3
namespace PluginSimpleValidate;
4
5
use PluginSimpleValidate\Libraries\Language;
6
7
class Validation implements \PluginSimpleValidate\Contracts\Validation
8
{
9
    /**
10
     * @var array
11
     * array of Field
12
     */
13
    private $fields;
14
15
    /**
16
     * @var array
17
     */
18
    private $errors;
19
20
    /**
21
     * @var Language
22
     */
23
    private $language;
24
25
    /**
26
     * @var bool
27
     */
28
    private $status;
29
30
    /**
31
     * Validation constructor.
32
     * @param Language $language
33
     */
34 2
    public function __construct(Language $language)
35
    {
36 2
        $this->language = $language;
37 2
        $this->fields = [];
38 2
        $this->errors = [];
39 2
        $this->status = false;
40 2
    }
41
42
    /**
43
     * @param \PluginSimpleValidate\Contracts\Field $field
44
     * @return $this
45
     */
46 2
    public function addField(\PluginSimpleValidate\Contracts\Field $field)
47
    {
48 2
        $this->fields[$field->getName()] = $field;
49 2
        return $this;
50
    }
51
52
    /**
53
     * @param bool $break_when_error
54
     * @return bool
55
     */
56 2 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...
57
    {
58 2
        $this->emptyErrors();
59
60
        /** @var Field $field */
61 2
        foreach ($this->fields as $field) {
62 2
            if (!$field->isValid($this->language)) {
63 2
                $this->status = false;
64 2
                $this->errors[$field->getName()] = $field->getErrors();
65
66
                /**
67
                 * break when there is any field error
68
                 */
69 2
                if ($break_when_error) {
70 2
                    break;
71
                }
72
            }
73
        }
74
75 2
        if (empty($this->errors)) {
76
            $this->status = true;
77
        }
78
79 2
        return $this->status;
80
    }
81
82
    /**
83
     * @return array
84
     */
85 2
    public function getErrors()
86
    {
87 2
        return $this->errors;
88
    }
89
90
    /**
91
     * @return $this
92
     */
93 2
    protected function emptyErrors()
94
    {
95 2
        $this->errors = [];
96 2
        return $this;
97
    }
98
99
    /**
100
     * @param string $fieldName
101
     * @return array|Field|null
102
     */
103
    public function getFields(string $fieldName = '')
104
    {
105
        if (empty($fieldName)) {
106
            return $this->fields;
107
        }
108
109
        return isset($this->fields[$fieldName]) ? $this->fields[$fieldName] : null;
110
    }
111
112
    /**
113
     * @return bool
114
     */
115
    public function getStatus(): bool
116
    {
117
        return $this->status;
118
    }
119
}