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) {
202 $this->getter = $getter;
203 $this->options = $options;
204 $this->callback = $callback;
205 $this->chunkSize = $chunk_size;
208 if ($this->chunkSize <= 0) {
209 $this->chunkSize = 25;
218 if ($callback && is_callable($callback)) {
219 $batch = new \ElggBatch($getter, $options, null, $chunk_size, $inc_offset);
224 $result = call_user_func($callback, $result, $getter, $options);
226 if (!isset($all_results)) {
227 if ($result ===
true || $result ===
false || $result === null) {
234 if (($result ===
true || $result ===
false || $result === null) && !is_array($all_results)) {
235 $all_results = $result && $all_results;
241 $this->callbackResult = $all_results;
250 private function getNextResultsChunk() {
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;
290 '__ElggBatch' => $this,
293 $options = array_merge($this->options, $current_options);
298 $this->results = call_user_func($this->getter, $options);
300 $num_results =
count($this->results);
302 if ($this->results) {
304 $this->resultIndex = 0;
306 $this->retrievedResults += $num_results;
307 if ($num_results === 0) {
310 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()) {
412 $this->resultIndex++;
416 $this->processedResults++;
423 #[\ReturnTypeWillChange] 425 if (!is_array($this->results)) {
429 return (
$key !== null &&
$key !==
false);
442 #[\ReturnTypeWillChange] 444 if (!is_callable($this->getter)) {
445 $inspector = new \Elgg\Debug\Inspector();
446 throw new ElggRuntimeException(
"Getter is not callable: " . $inspector->describeCallable($this->getter));
449 $options = array_merge($this->options, [
'count' =>
true]);
451 return call_user_func($this->getter, $options);
__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.
setIncrementOffset(bool $increment=true)
Increment the offset from the original options array? Setting to false is required for callbacks that...
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.
elgg_extract($key, $array, $default=null, $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
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif($item instanceof ElggRelationship) elseif(is_callable([$item, 'getType']))
_elgg_services()
Get the global service provider.