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

Each   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
c 1
b 0
f 1
lcom 1
cbo 1
dl 0
loc 49
ccs 14
cts 14
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A filter() 0 12 3
A filterValue() 0 8 1
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