Completed
Pull Request — master (#411)
by Anton
04:55
created

ArrayInput   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 38
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 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