WhereMutator::__invoke()   B
last analyzed

Complexity

Conditions 7
Paths 1

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 7

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 17
ccs 10
cts 10
cp 1
rs 8.2222
cc 7
eloc 9
nc 1
nop 3
crap 7
1
<?php
2
3
namespace Underscore\Mutator;
4
5
use Underscore\Collection;
6
use Underscore\Mutator;
7
8
class WhereMutator extends Mutator
9
{
10
    /**
11
     * Remove all values that do not match the given key-value pairs.
12
     *
13
     * By default strict comparison is used.
14
     *
15
     * @param Collection $collection
16
     * @param array      $properties
17
     * @param boolean    $strict
18
     * @return Collection
19
     */
20 1
    public function __invoke($collection, array $properties, $strict = true)
21
    {
22 1
        $filter = new FilterMutator;
23
24 1
        return $filter($collection, function ($item) use ($properties, $strict) {
25 1
            foreach ($properties as $key => $value) {
26 1
                if (empty($item[$key])
27 1
                    || ($strict && $item[$key] !== $value)
28 1
                    || (!$strict && $item[$key] != $value)
29
                ) {
30 1
                    return false;
31
                }
32
            }
33
34 1
            return true;
35 1
        });
36
    }
37
}
38