Completed
Push — master ( df9952...f86661 )
by Hannes
04:17 queued 02:11
created

CallableFilter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 0
dl 0
loc 26
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A filter() 0 8 3
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace hanneskod\yaysondb\Filter;
6
7
use hanneskod\yaysondb\Expression\ExpressionInterface;
8
9
/**
10
 * Filter documents matching a custom callable
11
 */
12
class CallableFilter implements FilterInterface
13
{
14
    /**
15
     * @var callable Custom filter
16
     */
17
    private $callable;
18
19
    /**
20
     * Set custom filter
21
     *
22
     * @param callable $callable Should take on argument and return a boolean
23
     */
24
    public function __construct(callable $callable)
25
    {
26
        $this->callable = $callable;
27
    }
28
29
    public function filter(\Traversable $documents): \Generator
30
    {
31
        foreach ($documents as $id => $doc) {
32
            if (($this->callable)($doc)) {
33
                yield $id => $doc;
34
            }
35
        }
36
    }
37
}
38