Completed
Push — master ( fb6499...f05f74 )
by Rick
46:04 queued 17:43
created

Each::filter()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
crap 3
1
<?php
2
/**
3
 * Particle.
4
 *
5
 * @link      http://github.com/particle-php for the canonical source repository
6
 * @copyright Copyright (c) 2005-2015 Particle (http://particle-php.com)
7
 * @license   https://github.com/particle-php/Filter/blob/master/LICENSE New BSD License
8
 */
9
namespace Particle\Filter\FilterRule;
10
11
use Particle\Filter\Filter;
12
use Particle\Filter\FilterRule;
13
14
/**
15
 * Class Each
16
 *
17
 * @package Particle\Filter\FilterRule
18
 */
19
class Each extends FilterRule
20
{
21
    /**
22
     * @var callable
23
     */
24
    protected $callable;
25
26
    /**
27
     * @param callable $callable
28
     */
29 2
    public function __construct(callable $callable)
30
    {
31 2
        $this->callable = $callable;
32 2
    }
33
34
    /**
35
     * When provided with an array, the callback filter will be executed for every value
36
     *
37
     * @param mixed $values
38
     * @return array
39
     */
40 2
    public function filter($values)
41
    {
42 2
        if (!is_array($values)) {
43 1
            return $values;
44
        }
45
46 1
        foreach ($values as $key => $value) {
47 1
            $values[$key] = $this->filterValue($value);
48 1
        }
49
50 1
        return $values;
51
    }
52
53
    /**
54
     * Filter a given value with a new filter instance in a callable
55
     *
56
     * @param $value
57
     * @return array
58
     */
59 1
    protected function filterValue($value)
60
    {
61 1
        $filter = new Filter();
62
63 1
        call_user_func($this->callable, $filter);
64
65 1
        return $filter->filter($value);
66
    }
67
}
68