Completed
Pull Request — master (#357)
by Anton
03:23
created

ArrayInput   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 75%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 44
ccs 9
cts 12
cp 0.75
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
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
/**
10
 * @namespace
11
 */
12
namespace Bluz\Validator\Rule;
13
14
use Bluz\Validator\Exception\ComponentException;
15
16
/**
17
 * Check for array
18
 *
19
 * @package Bluz\Validator\Rule
20
 */
21
class ArrayInput extends AbstractRule
22
{
23
    /**
24
     * @var string error template
25
     */
26
    protected $template = '{{name}} must be a array';
27
28
    /**
29
     * @var callable Callback for check input array
30
     */
31
    protected $callback;
32
33
    /**
34
     * Setup validation rule
35
     *
36
     * @param  callable $callback
37
     * @throws ComponentException
38
     */
39 11
    public function __construct($callback)
40
    {
41 11
        if (!is_callable($callback)) {
42
            throw new ComponentException(
43
                __('"%s" is not a valid callable structure', $callback)
44
            );
45
        }
46
47 11
        $this->callback = $callback;
48 11
    }
49
50
    /**
51
     * Check input data
52
     *
53
     * @param  string $input
54
     * @return bool
55
     */
56 10
    public function validate($input)
57
    {
58 10
        if (!is_array($input)) {
59 3
            return false;
60
        }
61 7
        $filtered = array_filter($input, $this->callback);
62 7
        return sizeof($input) == sizeof($filtered);
63
    }
64
}
65