/
AbstractPaginator.php
122 lines (102 loc) · 2.39 KB
/
AbstractPaginator.php
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
namespace LdapRecord\Query\Pagination;
use LdapRecord\LdapInterface;
use LdapRecord\Query\Builder;
abstract class AbstractPaginator
{
/**
* The query builder instance.
*
* @var Builder
*/
protected $query;
/**
* The filter to execute.
*
* @var string
*/
protected $filter;
/**
* The amount of objects to fetch per page.
*
* @var int
*/
protected $perPage;
/**
* Whether the operation is critical.
*
* @var bool
*/
protected $isCritical;
/**
* Constructor.
*
* @param Builder $query
*/
public function __construct(Builder $query, $filter, $perPage, $isCritical)
{
$this->query = $query;
$this->filter = $filter;
$this->perPage = $perPage;
$this->isCritical = $isCritical;
}
/**
* Execute the pagination request.
*
* @param LdapInterface $ldap
*
* @return array
*/
public function execute(LdapInterface $ldap)
{
$pages = [];
$this->prepareServerControls();
do {
$this->applyServerControls($ldap);
if (! $resource = $this->query->run($this->filter)) {
break;
}
$this->updateServerControls($ldap, $resource);
$pages[] = $this->query->parse($resource);
} while (! empty($this->fetchCookie()));
$this->resetServerControls($ldap);
return $pages;
}
/**
* Fetch the pagination cookie.
*
* @return string
*/
abstract protected function fetchCookie();
/**
* Prepare the server controls before executing the pagination request.
*
* @return void
*/
abstract protected function prepareServerControls();
/**
* Apply the server controls.
*
* @param LdapInterface $ldap
*
* @return void
*/
abstract protected function applyServerControls(LdapInterface $ldap);
/**
* Reset the server controls.
*
* @param LdapInterface $ldap
*
* @return void
*/
abstract protected function resetServerControls(LdapInterface $ldap);
/**
* Update the server controls.
*
* @param LdapInterface $ldap
* @param resource $resource
*
* @return void
*/
abstract protected function updateServerControls(LdapInterface $ldap, $resource);
}