PHP code example of double-break / php-batcher

1. Go to this page and download the library: Download double-break/php-batcher library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

double-break / php-batcher example snippets


use DoubleBreak\PhpBatcher\Batcher\Generic;

$batcher = new Generic(5, function (array $batch) {
    // Process the batch
    echo "Processing batch: " . implode(", ", $batch) . PHP_EOL;
});

$batcher->add("Item 1");
$batcher->add("Item 2");
$batcher->add("Item 3");
$batcher->add("Item 4");
$batcher->add("Item 5"); // Important: WILL NOT trigger the flush callback
$batcher->add("Item 6"); // This triggers the automatic flush before adding the new item

$batcher->flush(); // Manually flush remaining items


use DoubleBreak\PhpBatcher\Batcher\Generic;

$batcher = new Generic(11, function($batch, $state) {
    echo "Batch number: " . $state->get('batchNumber') . "\n";
    echo "Total processed items: " . $state->get('totalProcessedItems') . "\n";
    echo "Batch size: " . count($batch) . "\n";
    echo "Items in batch: " . implode(", ", $batch) . "\n";
    echo "Is FIRST: " . ($state->get('ordinality') == Generic::ORDINALITY_FIRST ? 'true' : 'false') . "\n";
    echo "Is LAST: " . ($state->get('ordinality') == Generic::ORDINALITY_LAST ? 'true' : 'false') . "\n";
    echo "Is MIDDLE: " . ($state->get('ordinality') == Generic::ORDINALITY_MIDDLE ? 'true' : 'false') . "\n";
    echo "\n\n";
});

$numbers = range(1, 104);
foreach ($numbers as $number) {
    $batcher->add($number);
}
$batcher->flush();


use DoubleBreak\PhpBatcher\Batcher\Generic;

function submit($data) {
    // Simulate a submission process
    echo "Submitting:" . PHP_EOL;
    print_r($data);
    echo PHP_EOL;

    // Simulates downstream logic managing tracking key creation or reuse
    return $data['isFirst'] ? uniqid() : $data['trackingKey'];
}

$batcher = new Generic(11, function($batch, $state) {
    $trackingKey = $state->get('myTrackingKey');

    $data = [
        'isFirst' => $state->get('ordinality') == Generic::ORDINALITY_FIRST,
        'isLast' => $state->get('ordinality') == Generic::ORDINALITY_LAST,
        'items' => $batch,
    ];

    if ($trackingKey !== null) {
        $data['trackingKey'] = $trackingKey;
    }

    $trackingKey = submit($data);

    $state->set('myTrackingKey', $trackingKey);
});

$numbers = range(1, 104);
foreach ($numbers as $number) {
    $batcher->add($number);
}
$batcher->flush();
bash
composer