Each::validate()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 9.4222
c 0
b 0
f 0
cc 5
nc 5
nop 1
crap 5
1
<?php
2
/**
3
 * Particle.
4
 *
5
 * @link      http://github.com/particle-php for the canonical source repository
6
 * @copyright Copyright (c) 2005-2016 Particle (http://particle-php.com)
7
 * @license   https://github.com/particle-php/validator/blob/master/LICENSE New BSD License
8
 */
9
namespace Particle\Validator\Rule;
10
11
use Particle\Validator\Rule;
12
use Particle\Validator\ValidationResult;
13
use Particle\Validator\Validator;
14
15
/**
16
 * This rule is for validating nested arrays.
17
 *
18
 * @package Particle\Validator\Rule
19
 */
20
class Each extends Rule
21
{
22
    const NOT_AN_ARRAY = 'Each::NOT_AN_ARRAY';
23
24
    const NOT_AN_ARRAY_ITEM = 'Each::NOT_AN_ARRAY_ITEM';
25
26
    /**
27
     * The message templates which can be returned by this validator.
28
     *
29
     * @var array
30
     */
31
    protected $messageTemplates = [
32
        self::NOT_AN_ARRAY => '{{ name }} must be an array',
33
        self::NOT_AN_ARRAY_ITEM => 'Each {{ name }} item must be an array',
34
    ];
35
36
    /**
37
     * @var callable
38
     */
39
    protected $callback;
40
41
    /**
42
     * @param callable $callback
43
     */
44 6
    public function __construct(callable $callback)
45
    {
46 6
        $this->callback = $callback;
47 6
    }
48
49
    /**
50
     * Validates if $value is array, validate each inner array of $value, and return result.
51
     *
52
     * @param mixed $value
53
     * @return bool
54
     */
55 7
    public function validate($value)
56
    {
57 7
        if (!is_array($value)) {
58 1
            return $this->error(self::NOT_AN_ARRAY);
59
        }
60
61 6
        $result = true;
62 6
        foreach ($value as $index => $innerValue) {
63 6
            if (!is_array($innerValue)) {
64 1
                return $this->error(self::NOT_AN_ARRAY_ITEM);
65
            }
66
67 5
            $result = $this->validateValue($index, $innerValue) && $result;
68 5
        }
69 5
        return $result;
70
    }
71
72
    /**
73
     * This method will spawn a new validator, validate an inner array, and return its result.
74
     *
75
     * @param string $index
76
     * @param mixed $value
77
     * @return bool
78
     */
79 5
    protected function validateValue($index, $value)
80
    {
81 5
        $innerValidator = new Validator();
82
83 5
        call_user_func($this->callback, $innerValidator);
84
85 5
        $result = $innerValidator->validate($value);
86
87 5
        if (!$result->isValid()) {
88 3
            $this->handleError($index, $result);
89 3
            return false;
90
        }
91
92 3
        return true;
93
    }
94
95
    /**
96
     * @param mixed $index
97
     * @param ValidationResult $result
98
     */
99 3
    protected function handleError($index, $result)
100
    {
101 3
        foreach ($result->getFailures() as $failure) {
102 3
            $failure->overwriteKey(
103 3
                sprintf('%s.%s.%s', $this->key, $index, $failure->getKey())
104 3
            );
105
106 3
            $this->messageStack->append($failure);
107 3
        }
108 3
    }
109
}
110