Completed
Pull Request — master (#411)
by Anton
06:32
created

ArrayInput::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Bluz Framework Component
4
 *
5
 * @copyright Bluz PHP Team
6
 * @link https://github.com/bluzphp/framework
7
 */
8
9
declare(strict_types=1);
10
11
namespace Bluz\Validator\Rule;
12
13
use Bluz\Validator\Exception\ComponentException;
14
15
/**
16
 * Check for array
17
 *
18
 * @package Bluz\Validator\Rule
19
 */
20
class ArrayInput extends AbstractRule
21
{
22
    /**
23
     * @var string error template
24
     */
25
    protected $template = '{{name}} must be a array';
26
27
    /**
28
     * @var callable Callback for check input array
29
     */
30
    protected $callback;
31
32
    /**
33
     * Setup validation rule
34
     *
35
     * @param  callable $callback
36
     * @throws ComponentException
37
     */
38 11
    public function __construct(callable $callback)
39
    {
40 11
        $this->callback = $callback;
41 11
    }
42
43
    /**
44
     * Check input data
45
     *
46
     * @param  string $input
47
     * @return bool
48
     */
49 10
    public function validate($input) : bool
50
    {
51 10
        if (!is_array($input)) {
52 3
            return false;
53
        }
54 7
        $filtered = array_filter($input, $this->callback);
55 7
        return count($input) == count($filtered);
56
    }
57
}
58