Completed
Push — master ( 705db9...4595b8 )
by Todd
50:14
created

ValidationField   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 87.18%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 1
dl 0
loc 157
ccs 34
cts 39
cp 0.8718
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 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
1
<?php
2
/**
3
 * @author Todd Burry <[email protected]>
4
 * @copyright 2009-2017 Vanilla Forums Inc.
5
 * @license Proprietary
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 90
    public function __construct(Validation $validation, $field, $name) {
37 90
        $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 90
        $this->validation = $validation;
39 90
        $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 90
    }
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 24
    public function addError($error, $options = []) {
51 24
        $this->validation->addError($this->getName(), $error, $options);
52 24
        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 35
    public function addTypeError($type = '') {
62 35
        $type = $type ?: $this->getType();
63
64 35
        $this->validation->addError(
65 35
            $this->getName(),
66 35
            'invalid',
67
            [
68 35
                'type' => $type,
69 35
                'messageCode' => '{field} is not a valid {type}.',
70
                'status' => 422
71 35
            ]
72 35
        );
73
74 35
        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
    public function isValid() {
83
        return $this->validation->isValidField($this->field);
0 ignored issues
show
Documentation introduced by
$this->field is of type array|object<Garden\Schema\Schema>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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 90
    public function getField() {
103 90
        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 72
    public function setField($field) {
115 72
        $this->field = $field;
116 72
        return $this;
117
    }
118
119
    /**
120
     * Get the validation.
121
     *
122
     * @return Validation Returns the validation.
123
     */
124 90
    public function getValidation() {
125 90
        return $this->validation;
126
    }
127
128
    /**
129
     * Get the name.
130
     *
131
     * @return string Returns the name.
132
     */
133 90
    public function getName() {
134 90
        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 72
    public function setName($name) {
146 72
        $this->name = $name;
147 72
        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 90
    public function getType() {
156 90
        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 89
    public function val($key, $default = null) {
167 89
        return isset($this->field[$key]) ? $this->field[$key] : $default;
168
    }
169
}
170