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

ArrayRule   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 40
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A validate() 0 8 2
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