Completed
Push — master ( df948c...389d06 )
by Alexandre
01:21
created

ValidationField   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 200
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 93.75%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 19
lcom 1
cbo 1
dl 0
loc 200
ccs 45
cts 48
cp 0.9375
rs 10
c 3
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A addError() 0 4 1
A addTypeError() 0 15 2
A isValid() 0 3 1
A merge() 0 4 1
A getField() 0 3 1
A setField() 0 4 1
A getValidation() 0 3 1
A getName() 0 3 1
A setName() 0 4 1
A getType() 0 3 2
A val() 0 3 2
A hasVal() 0 3 1
A getErrorCount() 0 3 1
A hasType() 0 3 1
A isSparse() 0 3 1
1
<?php
2
/**
3
 * @author Todd Burry <[email protected]>
4
 * @copyright 2009-2017 Vanilla Forums Inc.
5
 * @license MIT
6
 */
7
8
namespace Garden\Schema;
9
10
/**
11
 * A parameters class for field validation.
12
 */
13
class ValidationField {
14
    /**
15
     * @var array|Schema
16
     */
17
    private $field;
18
19
    /**
20
     * @var Validation
21
     */
22
    private $validation;
23
24
    /**
25
     * @var string
26
     */
27
    private $name;
28
29
    /**
30
     * @var bool
31
     */
32
    private $sparse;
33
34
    /**
35
     * Construct a new {@link ValidationField} object.
36
     *
37
     * @param Validation $validation The validation object that contains errors.
38
     * @param array|Schema $field The field definition.
39
     * @param string $name The path to the field.
40
     * @param bool $sparse Whether this is a sparse validation or not.
41
     */
42 144
    public function __construct(Validation $validation, $field, $name, $sparse = false) {
43 144
        $this->field = $field;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
44 144
        $this->validation = $validation;
45 144
        $this->name = $name;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
46 144
        $this->sparse = $sparse;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
47 144
    }
48
49
    /**
50
     * Add a validation error.
51
     *
52
     * @param string $error The message code.
53
     * @param int|array $options An array of additional information to add to the error entry or a numeric error code.
54
     * @return $this
55
     * @see Validation::addError()
56
     */
57 22
    public function addError($error, $options = []) {
58 22
        $this->validation->addError($this->getName(), $error, $options);
59 22
        return $this;
60
    }
61
62
    /**
63
     * Add an invalid type error.
64
     *
65
     * @param string $type The type that was checked.
66
     * @return $this
67
     */
68 38
    public function addTypeError($type = '') {
69 38
        $type = $type ?: $this->getType();
70
71 38
        $this->validation->addError(
72 38
            $this->getName(),
73 38
            'invalid',
74
            [
75 38
                'type' => $type,
76 38
                'messageCode' => '{field} is not a valid {type}.',
77
                'status' => 422
78 38
            ]
79 38
        );
80
81 38
        return $this;
82
    }
83
84
    /**
85
     * Check whether or not this field is has errors.
86
     *
87
     * @return bool Returns true if the field has no errors, false otherwise.
88
     */
89 63
    public function isValid() {
90 63
        return $this->getValidation()->isValidField($this->getName());
91
    }
92
93
    /**
94
     * Merge a validation object to this one.
95
     *
96
     * @param Validation $validation The validation object to merge.
97
     * @return $this
98
     */
99
    public function merge(Validation $validation) {
100
        $this->getValidation()->merge($validation, $this->getName());
101
        return $this;
102
    }
103
104
    /**
105
     * Get the field.
106
     *
107
     * @return array|Schema Returns the field.
108
     */
109 144
    public function getField() {
110 144
        return $this->field;
111
    }
112
113
    /**
114
     * Set the field.
115
     *
116
     * This method is only meant to be called from within the schema class.
117
     *
118
     * @param array|Schema $field The new field.
119
     * @return $this
120
     */
121 79
    public function setField($field) {
122 79
        $this->field = $field;
123 79
        return $this;
124
    }
125
126
    /**
127
     * Get the validation.
128
     *
129
     * @return Validation Returns the validation.
130
     */
131 144
    public function getValidation() {
132 144
        return $this->validation;
133
    }
134
135
    /**
136
     * Get the name.
137
     *
138
     * @return string Returns the name.
139
     */
140 144
    public function getName() {
141 144
        return $this->name;
142
    }
143
144
    /**
145
     * Set the name.
146
     *
147
     * This method is only meant to be called from within the schema class.
148
     *
149
     * @param string $name The new name.
150
     * @return $this
151
     */
152 84
    public function setName($name) {
153 84
        $this->name = $name;
154 84
        return $this;
155
    }
156
157
    /**
158
     * Get the field type.
159
     *
160
     * @return string|string[]|null Returns a type string, array of type strings, or null if there isn't one.
161
     */
162 144
    public function getType() {
163 144
        return isset($this->field['type']) ? $this->field['type'] : null;
164
    }
165
166
    /**
167
     * Whether or not the field has a given type.
168
     *
169
     * @param string $type The single type to test.
170
     * @return bool Returns **true** if the field has the given type or **false** otherwise.
171
     */
172 27
    public function hasType($type) {
173 27
        return in_array($type, (array)$this->getType());
174
    }
175
176
    /**
177
     * Get a value fom the field.
178
     *
179
     * @param string $key The key to look at.
180
     * @param mixed $default The default to return if the key isn't found.
181
     * @return mixed Returns a value or the default.
182
     */
183 144
    public function val($key, $default = null) {
184 144
        return isset($this->field[$key]) ? $this->field[$key] : $default;
185
    }
186
187
    /**
188
     * Whether or not the field has a value.
189
     *
190
     * @param string $key The key to look at.
191
     * @return bool Returns **true** if the field has a key or **false** otherwise.
192
     */
193 18
    public function hasVal($key) {
194 18
        return array_key_exists($key, $this->field);
195
    }
196
197
    /**
198
     * Get the error count for this field.
199
     */
200 61
    public function getErrorCount() {
201 61
        return $this->getValidation()->getErrorCount($this->getName());
202
    }
203
204
    /**
205
     * Whether or not this is a sparse validation..
206
     *
207
     * @return bool Returns **true** if this is a sparse validation or **false** otherwise.
208
     */
209 3
    public function isSparse() {
210 3
        return $this->sparse;
211
    }
212
}
213