-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathOffsetFilter.php
More file actions
93 lines (76 loc) · 1.92 KB
/
OffsetFilter.php
File metadata and controls
93 lines (76 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
namespace Ddeboer\DataImport\Filter;
use Ddeboer\DataImport\Exception\StopException;
/**
* This filter can be used to filter out some items from the beginning and/or
* end of the items.
*
* @author Ville Mattila <ville@eventio.fi>
*/
class OffsetFilter
{
/**
* @var integer
*/
protected $offset = 0;
/**
* @var integer|null
*/
protected $limit = null;
/**
* @var integer
*/
protected $offsetCount = 0;
/**
* @var integer
*/
protected $sliceCount = 0;
/**
* @var boolean
*/
protected $maxLimitHit = false;
/**
* @var boolean
*/
protected $stopOnMaxLimit = false;
/**
* @param integer $offset 0-based index of the item to start read from
* @param integer|null $limit Maximum count of items to read. null = no limit
*/
public function __construct($offset = 0, $limit = null, $stopOnLimit = false)
{
$this->offset = $offset;
$this->limit = $limit;
$this->stopOnMaxLimit = ($limit>0 && $stopOnLimit);
}
/**
* {@inheritdoc}
*/
public function __invoke(array $item)
{
// In case we've already filtered up to limited
if ($this->maxLimitHit) {
if($this->stopOnMaxLimit) {
throw new StopException();
}
return false;
}
$this->offsetCount++;
// We have not reached the start offset
if ($this->offsetCount < $this->offset + 1) {
return false;
}
// There is no maximum limit, so we'll return always true
if (null === $this->limit) {
return true;
}
$this->sliceCount++;
if ($this->sliceCount < $this->limit) {
return true;
} elseif ($this->sliceCount == $this->limit) {
$this->maxLimitHit = true;
return true;
}
return false;
}
}