Completed
Push — master ( 38deb0...7544ac )
by Anton
11s
created

ArrayRule::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
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 ArrayRule extends AbstractRule
21
{
22
    /**
23
     * @var string error template
24
     */
25
    protected $description = '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
     *
37
     * @throws ComponentException
38
     */
39 10
    public function __construct(callable $callback)
40
    {
41 10
        $this->callback = $callback;
42 10
    }
43
44
    /**
45
     * Check input data
46
     *
47
     * @param  string $input
48
     *
49
     * @return bool
50
     */
51 9
    public function validate($input): bool
52
    {
53 9
        if (!is_array($input)) {
54 3
            return false;
55
        }
56 6
        $filtered = array_filter($input, $this->callback);
57 6
        return count($input) === count($filtered);
58
    }
59
}
60