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

LimitFilter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 0
loc 43
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A filter() 0 18 4
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace hanneskod\yaysondb\Filter;
6
7
/**
8
 * Limit the number of returned documents
9
 */
10
class LimitFilter implements FilterInterface
11
{
12
    /**
13
     * @var int The number of documents to return
14
     */
15
    private $count;
16
17
    /**
18
     * @var int The offset of the first document to return
19
     */
20
    private $offset;
21
22
    /**
23
     * Set limit clause
24
     *
25
     * @param int $count  The number of documents to return
26
     * @param int $offset The offset of the first document to return
27
     */
28
    public function __construct(int $count, int $offset = 0)
29
    {
30
        $this->count = $count;
31
        $this->offset = $offset;
32
    }
33
34
    public function filter(\Traversable $documents): \Generator
35
    {
36
        $count = 0;
37
38
        foreach ($documents as $id => $doc) {
39
            $count++;
40
41
            if ($count <= $this->offset) {
42
                continue;
43
            }
44
45
            if ($count > $this->count + $this->offset) {
46
                break;
47
            }
48
49
            yield $id => $doc;
50
        }
51
    }
52
}
53