|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link http://www.newicon.net/neon |
|
4
|
|
|
* @copyright Copyright (c) 2019 Newicon Ltd |
|
5
|
|
|
* @license http://www.newicon.net/neon/license/ |
|
6
|
|
|
* @package neon |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace neon\core\helpers; |
|
10
|
|
|
|
|
11
|
|
|
use yii\base\Component; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Defines a basic iterator for use in paginated database requests. |
|
15
|
|
|
* |
|
16
|
|
|
* To use it you can simply create one which will have a start value of 0 |
|
17
|
|
|
* and a length of 100. You can request up to 1000 items back at once. If you |
|
18
|
|
|
* need more than 1000, then you should be seriously considering why. |
|
19
|
|
|
* |
|
20
|
|
|
* To request the total during a call, set returnTotal() on the Iterator. |
|
21
|
|
|
* This will, for efficiency reasons, only set this if you are at the |
|
22
|
|
|
* beginning of the iteration, i.e. start=0. Counts are costly so should be |
|
23
|
|
|
* avoided. This way you can set the request at the beginning and use the |
|
24
|
|
|
* total value from then on. |
|
25
|
|
|
* |
|
26
|
|
|
* @package neon\core\helpers |
|
27
|
|
|
*/ |
|
28
|
|
|
class Iterator extends Component |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* The maximum number that can be returned in any one request |
|
32
|
|
|
* @const integer |
|
33
|
|
|
*/ |
|
34
|
|
|
const MAX_LENGTH = 1000; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Return the total number found |
|
38
|
|
|
* @var integer |
|
39
|
|
|
*/ |
|
40
|
|
|
public $total = 0; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* The starting position |
|
44
|
|
|
* @var integer |
|
45
|
|
|
*/ |
|
46
|
|
|
private $_start = 0; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* The number to return |
|
50
|
|
|
* @var integer |
|
51
|
|
|
*/ |
|
52
|
|
|
private $_length = 100; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Whether or not to get the total |
|
56
|
|
|
* @var boolean |
|
57
|
|
|
*/ |
|
58
|
|
|
private $_returnTotal = false; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Set the starting position |
|
62
|
|
|
* @param integer $start |
|
63
|
|
|
*/ |
|
64
|
2 |
|
public function setStart($start) |
|
65
|
|
|
{ |
|
66
|
2 |
|
$this->_start = max(0, $start); |
|
67
|
2 |
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Set the number to return |
|
71
|
|
|
* @param integer $length |
|
72
|
|
|
*/ |
|
73
|
6 |
|
public function setLength($length) |
|
74
|
|
|
{ |
|
75
|
6 |
|
$this->_length = min($length, self::MAX_LENGTH); |
|
76
|
6 |
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Get the starting position |
|
80
|
|
|
* @return integer |
|
81
|
|
|
*/ |
|
82
|
20 |
|
public function getStart() |
|
83
|
|
|
{ |
|
84
|
20 |
|
return $this->_start; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Get the number to return |
|
89
|
|
|
* @return integer |
|
90
|
|
|
*/ |
|
91
|
20 |
|
public function getLength() |
|
92
|
|
|
{ |
|
93
|
20 |
|
return $this->_length; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Use to request a total is calculated. This will only |
|
98
|
|
|
* work if the starting position is 0 for efficiency reasons |
|
99
|
|
|
* or if you force it to return a total (this should be |
|
100
|
|
|
* avoided if possible as counts are very expensive operations) |
|
101
|
|
|
*/ |
|
102
|
|
|
public function returnTotal(bool $force=false) |
|
103
|
|
|
{ |
|
104
|
|
|
$this->_returnTotal=($force? true : ($this->_start == 0)); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Whether or not to calculate and return a total |
|
109
|
|
|
* @return boolean |
|
110
|
|
|
*/ |
|
111
|
20 |
|
public function shouldReturnTotal() |
|
112
|
|
|
{ |
|
113
|
20 |
|
return $this->_returnTotal; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Convert to an array |
|
118
|
|
|
* @return |
|
119
|
|
|
* start => the start value |
|
120
|
|
|
* length => the length value |
|
121
|
|
|
* total => truthy if should return total, otherwise the total |
|
122
|
|
|
*/ |
|
123
|
|
|
public function toArray() |
|
124
|
|
|
{ |
|
125
|
|
|
return [ |
|
126
|
|
|
'start' => $this->start, |
|
|
|
|
|
|
127
|
|
|
'length' => $this->length, |
|
|
|
|
|
|
128
|
|
|
'total' => $this->_returnTotal ? true : $this->total |
|
129
|
|
|
]; |
|
130
|
|
|
} |
|
131
|
|
|
} |