ArrayValidator::__construct()   B
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 19
nc 1
nop 1
dl 0
loc 33
rs 8.8571
c 0
b 0
f 0
1
<?php
2
namespace Comfort\Validator;
3
4
use Comfort\Comfort;
5
use Comfort\Exception\ValidationException;
6
use Comfort\ValidationError;
7
8
class ArrayValidator extends AbstractValidator
9
{
10
    /**
11
     * ArrayValidator constructor.
12
     * @param Comfort $comfort
13
     */
14
    public function __construct(Comfort $comfort)
15
    {
16
        parent::__construct($comfort);
17
18
        $this->toBool(false);
19
20
        $this->add(function ($value, $nameKey) {
21
            if (!is_array($value)) {
22
                return $this->createError('array.not_array', $value, $nameKey);
23
            }
24
        });
25
26
        $this->errorHandlers += [
27
            'array.min' => [
28
                'message' => '%s must contains at least %s elements'
29
            ],
30
            'array.max' => [
31
                'message' => '%s must contain no more than %s elements'
32
            ],
33
            'array.length' => [
34
                'message' => '%s must only contain %s elements'
35
            ],
36
            'array.unique' => [
37
                'message' => '%s is not an array containing all unique values'
38
            ],
39
            'array.items.validation_failure' => [
40
                'message' => '%s has an item that fails validation'
41
            ],
42
            'array.not_array' => [
43
                'message' => '%s is not an array'
44
            ],
45
        ];
46
    }
47
48
    /**
49
     * Given a set of keys apply the specified validation rules
50
     *
51
     * @param array $definition
52
     * @return $this
53
     */
54
    public function keys(array $definition)
55
    {
56
        return $this->add(function (&$value) use ($definition) {
57
            /**
58
             * @var string $key
59
             * @var AbstractValidator $validator
60
             */
61
            foreach ($definition as $key => $validator) {
62
                if (!$validator instanceof AbstractValidator) {
63
                    return $this->createError('array.invalid_keys_entry');
64
                }
65
66
                $validator->toBool(false);
67
                $validatorValue = isset($value[$key]) ? $value[$key] : null;
68
                $result = $validator($validatorValue, $key);
69
                if ($result instanceof ValidationError) {
70
                    throw new ValidationException($result->getKey(), $result->getMessage());
71
                }
72
73
                if (isset($value[$key])) {
74
                    $value[$key] = $result;
75
                }
76
            }
77
78
            return $value;
79
        });
80
    }
81
82
    /**
83
     * Validate array has more than $min elements
84
     *
85
     * @param $min
86
     * @return $this
87
     */
88
    public function min($min)
89
    {
90
        return $this->add(function ($value, $nameKey) use ($min) {
91
            if (count($value) < $min) {
92
                return $this->createError('array.min', $value, $nameKey);
93
            }
94
        });
95
    }
96
97
    /**
98
     * Validate array has only up to $max characters
99
     *
100
     * @param $max
101
     * @return $this
102
     */
103
    public function max($max)
104
    {
105
        return $this->add(function ($value, $nameKey) use ($max) {
106
            if (count($value) > $max) {
107
                return $this->createError('array.max', $value, $nameKey);
108
            }
109
        });
110
    }
111
112
    /**
113
     * Validate array contains exactly $length elements
114
     *
115
     * @param $length
116
     * @return $this
117
     */
118
    public function length($length)
119
    {
120
        return $this->add(function ($value, $nameKey) use ($length) {
121
            if (count($value) != $length) {
122
                return $this->createError('array.length', $value, $nameKey);
123
            }
124
        });
125
    }
126
127
    /**
128
     * Validate array is unique
129
     *
130
     * @return $this
131
     */
132
    public function unique()
133
    {
134
        return $this->add(function ($value, $nameKey) {
135
            if (md5(serialize($value)) != md5(serialize(array_unique($value)))) {
136
                return $this->createError('array.unique', $value, $nameKey);
137
            }
138
        });
139
    }
140
141
    /**
142
     * Apply $definition to individual items in the array,
143
     * used in case of multi-dimensional array
144
     *
145
     * @param AbstractValidator $definition
146
     * @return $this
147
     */
148
    public function items(AbstractValidator $definition)
149
    {
150
        return $this->add(function ($value, $nameKey) use ($definition) {
151
            foreach ($value as $key => $val) {
152
                $resp = $definition($val, $nameKey);
153
                if ($resp instanceof ValidationError) {
154
                    return $resp;
155
                }
156
157
                $value[$key] = empty($resp) ? $val : $resp;
158
            }
159
160
            return $value;
161
        });
162
163
        return $this;
0 ignored issues
show
Unused Code introduced by
return $this; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
164
    }
165
}
166