Each   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A filter() 0 10 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-2016 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
            foreach ($values as $key => $value) {
44 1
                $values[$key] = $this->filterValue($value);
45 1
            }
46 1
        }
47
48 2
        return $values;
49
    }
50
51
    /**
52
     * Filter a given value with a new filter instance in a callable
53
     *
54
     * @param $value
55
     * @return array
56
     */
57 1
    protected function filterValue($value)
58
    {
59 1
        $filter = new Filter();
60
61 1
        call_user_func($this->callable, $filter);
62
63 1
        return $filter->filter($value);
64
    }
65
}
66