78 private $results = [];
85 private $getter =
null;
92 private $options = [];
99 private $chunkSize = 25;
106 private $callback =
null;
127 private $retrievedResults = 0;
134 private $resultIndex = 0;
141 private $chunkIndex = 0;
148 private $processedResults = 0;
155 private $validGetter =
null;
169 private $incrementOffset =
true;
176 private $reportedFailures = 0;
200 public function __construct(callable $getter, array $options, $callback =
null,
int $chunk_size = 25,
bool $inc_offset =
true) {
204 $this->callback = $callback;
205 $this->chunkSize = $chunk_size;
208 if ($this->chunkSize <= 0) {
209 $this->chunkSize = 25;
218 if ($callback && is_callable($callback)) {
226 if (!isset($all_results)) {
235 $all_results =
$result && $all_results;
241 $this->callbackResult = $all_results;
250 private function getNextResultsChunk(): bool {
255 if (!isset($this->validGetter)) {
256 $this->validGetter = is_callable($this->getter);
259 if (!$this->validGetter) {
263 $limit = $this->chunkSize;
266 if ($this->limit != 0) {
267 if ($this->retrievedResults >= $this->limit) {
273 if ($this->limit < $this->chunkSize) {
274 $limit = $this->limit;
275 }
elseif ($this->retrievedResults + $this->chunkSize > $this->limit) {
277 $limit = $this->limit - $this->retrievedResults;
281 if ($this->incrementOffset) {
282 $offset = $this->offset + $this->retrievedResults;
284 $offset = $this->offset + $this->reportedFailures;
292 $options = array_merge($this->options, $current_options);
297 $this->results = call_user_func($this->getter, $options);
299 $num_results =
count($this->results);
301 if ($this->results) {
303 $this->resultIndex = 0;
305 $this->retrievedResults += $num_results;
306 if ($num_results === 0) {
309 return $this->getNextResultsChunk();
329 $this->incrementOffset = $increment;
338 $this->chunkSize =
$size;
350 $this->reportedFailures += $num;
360 #[\ReturnTypeWillChange]
362 $this->resultIndex = 0;
363 $this->retrievedResults = 0;
364 $this->processedResults = 0;
365 $this->reportedFailures = 0;
368 if ($this->chunkIndex == 0 || $this->limit > $this->chunkSize) {
369 $this->chunkIndex = 0;
370 $this->getNextResultsChunk();
377 #[\ReturnTypeWillChange]
379 return current($this->results);
385 #[\ReturnTypeWillChange]
387 return $this->processedResults;
393 #[\ReturnTypeWillChange]
396 if (($this->processedResults + 1) >= $this->limit && $this->limit > 0) {
402 if (($this->resultIndex + 1) >= $this->chunkSize) {
403 if (!$this->getNextResultsChunk()) {
408 $result = current($this->results);
412 $this->resultIndex++;
413 $result = next($this->results);
416 $this->processedResults++;
423 #[\ReturnTypeWillChange]
425 if (!is_array($this->results)) {
429 $key = key($this->results);
430 return (
$key !==
null &&
$key !==
false);
443 #[\ReturnTypeWillChange]
445 if (!is_callable($this->getter)) {
446 $inspector = new \Elgg\Debug\Inspector();
447 throw new ElggRuntimeException(
'Getter is not callable: ' . $inspector->describeCallable($this->getter));
450 $options = array_merge($this->options, [
'count' =>
true]);
452 return call_user_func($this->getter,
$options);
setIncrementOffset(bool $increment=true)
Increment the offset from the original options array? Setting to false is required for callbacks that...
__construct(callable $getter, array $options, $callback=null, int $chunk_size=25, bool $inc_offset=true)
Batches operations on any elgg_get_*() or compatible function that supports an options array.
rewind()
Implements Iterator.
reportFailure(int $num=1)
Report a number of failures during the batch execution, this will increase the internal offset by $nu...
setChunkSize(int $size=25)
Set chunk size.
count()
Count the total results available at this moment.
Exception thrown if an error which can only be found on runtime occurs.
if($who_can_change_language==='nobody') elseif($who_can_change_language==='admin_only' &&!elgg_is_admin_logged_in()) $options
if($item instanceof \ElggEntity) elseif($item instanceof \ElggRiverItem) elseif($item instanceof \ElggRelationship) elseif(is_callable([ $item, 'getType']))
_elgg_services()
Get the global service provider.
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
if($container instanceof ElggGroup && $container->guid !=elgg_get_page_owner_guid()) $key