Completed
Pull Request — master (#9)
by Ryan
02:14
created

ValidationField   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 92.86%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 17
lcom 1
cbo 1
dl 0
loc 174
ccs 39
cts 42
cp 0.9286
rs 10
c 2
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 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 addError() 0 4 1
A val() 0 3 2
A addTypeError() 0 15 2
A isValid() 0 3 1
A hasVal() 0 3 1
A getErrorCount() 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
     * Construct a new {@link ValidationField} object.
31
     *
32
     * @param Validation $validation The validation object that contains errors.
33
     * @param array|Schema $field The field definition.
34
     * @param string $name The path to the field.
35
     */
36 117
    public function __construct(Validation $validation, $field, $name) {
37 117
        $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...
38 117
        $this->validation = $validation;
39 117
        $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...
40 117
    }
41
42
    /**
43
     * Add a validation error.
44
     *
45
     * @param string $error The message code.
46
     * @param int|array $options An array of additional information to add to the error entry or a numeric error code.
47
     * @return $this
48
     * @see Validation::addError()
49
     */
50 18
    public function addError($error, $options = []) {
51 18
        $this->validation->addError($this->getName(), $error, $options);
52 18
        return $this;
53
    }
54
55
    /**
56
     * Add an invalid type error.
57
     *
58
     * @param string $type The type that was checked.
59
     * @return $this
60
     */
61 52
    public function addTypeError($type = '') {
62 52
        $type = $type ?: $this->getType();
63
64 52
        $this->validation->addError(
65 52
            $this->getName(),
66 52
            'invalid',
67
            [
68 52
                'type' => $type,
69 52
                'messageCode' => '{field} is not a valid {type}.',
70 52
                'status' => 422
71
            ]
72
        );
73
74 52
        return $this;
75
    }
76
77
    /**
78
     * Check whether or not this field is has errors.
79
     *
80
     * @return bool Returns true if the field has no errors, false otherwise.
81
     */
82 50
    public function isValid() {
83 50
        return $this->getValidation()->isValidField($this->getName());
84
    }
85
86
    /**
87
     * Merge a validation object to this one.
88
     *
89
     * @param Validation $validation The validation object to merge.
90
     * @return $this
91
     */
92
    public function merge(Validation $validation) {
93
        $this->getValidation()->merge($validation, $this->getName());
94
        return $this;
95
    }
96
97
    /**
98
     * Get the field.
99
     *
100
     * @return array|Schema Returns the field.
101
     */
102 117
    public function getField() {
103 117
        return $this->field;
104
    }
105
106
    /**
107
     * Set the field.
108
     *
109
     * This method is only meant to be called from within the schema class.
110
     *
111
     * @param array|Schema $field The new field.
112
     * @return $this
113
     */
114 76
    public function setField($field) {
115 76
        $this->field = $field;
116 76
        return $this;
117
    }
118
119
    /**
120
     * Get the validation.
121
     *
122
     * @return Validation Returns the validation.
123
     */
124 117
    public function getValidation() {
125 117
        return $this->validation;
126
    }
127
128
    /**
129
     * Get the name.
130
     *
131
     * @return string Returns the name.
132
     */
133 117
    public function getName() {
134 117
        return $this->name;
135
    }
136
137
    /**
138
     * Set the name.
139
     *
140
     * This method is only meant to be called from within the schema class.
141
     *
142
     * @param string $name The new name.
143
     * @return $this
144
     */
145 76
    public function setName($name) {
146 76
        $this->name = $name;
147 76
        return $this;
148
    }
149
150
    /**
151
     * Get the field type.
152
     *
153
     * @return string|null Returns a type string or null if there isn't one.
154
     */
155 117
    public function getType() {
156 117
        return isset($this->field['type']) ? $this->field['type'] : null;
157
    }
158
159
    /**
160
     * Get a value fom the field.
161
     *
162
     * @param string $key The key to look at.
163
     * @param mixed $default The default to return if the key isn't found.
164
     * @return mixed Returns a value or the default.
165
     */
166 116
    public function val($key, $default = null) {
167 116
        return isset($this->field[$key]) ? $this->field[$key] : $default;
168
    }
169
170
    /**
171
     * Whether or not the field has a value.
172
     *
173
     * @param string $key The key to look at.
174
     * @return bool Returns **true** if the field has a key or **false** otherwise.
175
     */
176 20
    public function hasVal($key) {
177 20
        return array_key_exists($key, $this->field);
178
    }
179
180
    /**
181
     * Get the error count for this field.
182
     */
183 49
    public function getErrorCount() {
184 49
        return $this->getValidation()->getErrorCount($this->getName());
185
    }
186
}
187