Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | namespace LdapRecord\Query\Pagination; |
| 4 | |
| 5 | use LdapRecord\LdapInterface; |
| 6 | use LdapRecord\Query\Builder; |
| 7 | |
| 8 | abstract class AbstractPaginator |
| 9 | { |
| 10 | /** |
| 11 | * The query builder instance. |
| 12 | * |
| 13 | * @var Builder |
| 14 | */ |
| 15 | protected $query; |
| 16 | |
| 17 | /** |
| 18 | * The filter to execute. |
| 19 | * |
| 20 | * @var string |
| 21 | */ |
| 22 | protected $filter; |
| 23 | |
| 24 | /** |
| 25 | * The amount of objects to fetch per page. |
| 26 | * |
| 27 | * @var int |
| 28 | */ |
| 29 | protected $perPage; |
| 30 | |
| 31 | /** |
| 32 | * Whether the operation is critical. |
| 33 | * |
| 34 | * @var bool |
| 35 | */ |
| 36 | protected $isCritical; |
| 37 | |
| 38 | /** |
| 39 | * Constructor. |
| 40 | * |
| 41 | * @param Builder $query |
| 42 | */ |
| 43 | public function __construct(Builder $query, $filter, $perPage, $isCritical) |
| 44 | { |
| 45 | $this->query = $query; |
| 46 | $this->filter = $filter; |
| 47 | $this->perPage = $perPage; |
| 48 | $this->isCritical = $isCritical; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Execute the pagination request. |
| 53 | * |
| 54 | * @param LdapInterface $ldap |
| 55 | * |
| 56 | * @return array |
| 57 | */ |
| 58 | public function execute(LdapInterface $ldap) |
| 59 | { |
| 60 | $pages = []; |
| 61 | |
| 62 | $this->prepareServerControls(); |
| 63 | |
| 64 | do { |
| 65 | $this->applyServerControls($ldap); |
| 66 | |
| 67 | if (! $resource = $this->query->run($this->filter)) { |
| 68 | break; |
| 69 | } |
| 70 | |
| 71 | $this->updateServerControls($ldap, $resource); |
| 72 | |
| 73 | $pages[] = $this->query->parse($resource); |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 74 | } while ($this->shouldContinue()); |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 75 | |
| 76 | $this->resetServerControls($ldap); |
| 77 | |
| 78 | return $pages; |
| 79 | } |
| 80 | |
| 81 | /** |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 82 | * Whether the paginater should continue iterating. |
| 83 | * |
| 84 | * @return bool |
| 85 | */ |
| 86 | protected function shouldContinue() |
| 87 | { |
| 88 | $cookie = (string) $this->fetchCookie(); |
| 89 | |
| 90 | return $cookie !== ''; |
| 91 | } |
| 92 | |
| 93 | /** |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 94 | * Fetch the pagination cookie. |
| 95 | * |
| 96 | * @return string |
| 97 | */ |
| 98 | abstract protected function fetchCookie(); |
| 99 | |
| 100 | /** |
| 101 | * Prepare the server controls before executing the pagination request. |
| 102 | * |
| 103 | * @return void |
| 104 | */ |
| 105 | abstract protected function prepareServerControls(); |
| 106 | |
| 107 | /** |
| 108 | * Apply the server controls. |
| 109 | * |
| 110 | * @param LdapInterface $ldap |
| 111 | * |
| 112 | * @return void |
| 113 | */ |
| 114 | abstract protected function applyServerControls(LdapInterface $ldap); |
| 115 | |
| 116 | /** |
| 117 | * Reset the server controls. |
| 118 | * |
| 119 | * @param LdapInterface $ldap |
| 120 | * |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 121 | * @return void |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 122 | */ |
| 123 | abstract protected function resetServerControls(LdapInterface $ldap); |
| 124 | |
| 125 | /** |
| 126 | * Update the server controls. |
| 127 | * |
| 128 | * @param LdapInterface $ldap |
| 129 | * @param resource $resource |
| 130 | * |
| 131 | * @return void |
| 132 | */ |
| 133 | abstract protected function updateServerControls(LdapInterface $ldap, $resource); |
| 134 | } |