PHP code example of simsoft / data-flow
1. Go to this page and download the library: Download simsoft/data-flow 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/ */
simsoft / data-flow example snippets
(new DataFlow())
->from($source) // Extract: where data comes from
->transform($logic) // Transform: reshape, filter, validate
->load($destination) // Load: where data goes
->run();
use Simsoft\DataFlow\DataFlow;
(new DataFlow())
->from([1, 2, 3])
->transform(function($num) {
return $num * 2;
})
->load(function($num) {
echo $num . PHP_EOL;
})
->run();
// Output:
// 2
// 4
// 6
use Simsoft\DataFlow\DataFlow;
(new DataFlow())
->from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
->transform(function($num) {
return $num * 2;
})
->limit(5) // output only 5 data.
->load(function($num) {
echo $num . PHP_EOL;
})
->run();
// Output:
// 2
// 4
// 6
// 8
// 10
use Simsoft\DataFlow\DataFlow;
(new DataFlow())
->from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
->filter(function($num) {
// The call back should return bool.
// In this case, return even number only.
return $num % 2 === 0;
})
->load(function($num) {
echo $num . PHP_EOL;
})
->run();
// Output:
// 2
// 4
// 6
// 8
// 10
(new DataFlow())
->from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
->chunk(3) // set chunk size
->load(function(array $chunk, $key) {
echo $key . '=' . json_encode($chunk, JSON_THROW_ON_ERROR) . PHP_EOL;
})
->run();
// Output:
// 0=[1,2,3]
// 1=[4,5,6]
// 2=[7,8,9]
// 3=[10]
(new DataFlow())
->from([
['First Name' => 'John', 'Last Name' => 'Doe', 'age' => 20],
['First Name' => 'Jane', 'Last Name' => 'Doe', 'age' => 30],
['First Name' => 'John', 'Last Name' => 'Smith', 'age' => 50],
['First Name' => 'Jane', 'Last Name' => 'Smith', 'age' => 60],
])
->map([
// rename the key
'first_name' => 'First Name',
'last_name' => 'Last Name',
// customise data via callback method.
'full_name' => fn($data) => $data['first_name'] . ' ' . $data['last_name'],
'senior' => fn($data) => $data['age'] > 30 ? 'Yes' : 'No',
])
->load(function($data) {
echo $data['full_name'] . ' is ' . $data['age'] . ' years old. ' . $data['senior'] . PHP_EOL;
})
->run();
// Output:
// John Doe is 20 years old. No
// Jane Doe is 30 years old. Yes
// John Smith is 50 years old. Yes
// Jane Smith is 60 years old. Yes
(new DataFlow())
->from([
['first_name' => 'John', 'last_name' => 'Doe', 'age' => 20, 'status' => 'active', 'internal_id' => 'x99'],
['first_name' => 'Jane', 'last_name' => 'Smith', 'age' => 30, 'status' => 'inactive', 'internal_id' => 'x42'],
])
->setNewMap([
'name' => fn($row) => $row['first_name'] . ' ' . $row['last_name'],
'age' => 'age',
])
->load(function($data) {
// $data contains ONLY 'name' and 'age' — no 'status', 'internal_id', etc.
echo json_encode($data) . PHP_EOL;
})
->run();
// Output:
// {"name":"John Doe","age":20}
// {"name":"Jane Smith","age":30}
(new DataFlow())
->from([
['name' => 'John', 'email' => '[email protected] '],
['name' => 'Jane', 'email' => '[email protected] '],
['name' => 'Bob', 'email' => '[email protected] '],
])
->map(['full_name' => fn($row) => strtoupper($row['name'])])
->preview(2) // show first 2 rows then stop
->run();
// Output:
// Key: 0
// Value: array (
// 'name' => 'John',
// 'email' => '[email protected] ',
// 'full_name' => 'JOHN',
// )
//
// Key: 1
// Value: array (
// 'name' => 'Jane',
// 'email' => '[email protected] ',
// 'full_name' => 'JANE',
// )
$flow1 = (new DataFlow())
->from([1, 2, 3])
->transform(function($num) {
return $num * 2;
});
(new DataFlow())
->from($flow1) // connect flow1 to flow2.
->transform(function($num) {
return $num * 3;
})
->load(function($num) {
echo $num . PHP_EOL;
})
->run();
// Output:
// 6
// 12
// 18
use Simsoft\DataFlow\DataFlow;
$result = (new DataFlow())
->from([1, 2, 3, 4, 5])
->transform(fn($n) => $n * 2)
->load(fn($n) => $n)
->run();
echo "Processed: {$result->getProcessedRows()} rows\n";
echo "Duration: " . round($result->getDurationMs()) . "ms\n";
echo "Peak memory: " . round($result->getPeakMemoryBytes() / 1024) . " KB\n";
use Simsoft\DataFlow\DataFlow;
use Simsoft\DataFlow\Enums\ErrorStrategy;
$result = (new DataFlow())
->from($records)
->transform(
(new MyTransformer())
->withErrorStrategy(ErrorStrategy::Skip) // skip failing rows
->withName('validator')
)
->load(fn($row) => $row)
->run();
echo "Processed: {$result->getProcessedRows()}\n";
echo "Failed: {$result->getFailedRows()}\n";
$result = (new DataFlow())
->from($records)
->transform(fn($row) => $row)
->load(new DatabaseLoader())
->dryRun()
->run();
echo "Would process: {$result->getProcessedRows()} rows\n";
// No data was actually written
use Simsoft\DataFlow\DataFlow;
$result = (new DataFlow())
->from($largeDataset)
->withLogger($psrLogger)
->onProgress(function (int $count, float $elapsedMs) {
echo "\r Processed {$count} rows...";
}, interval: 1000)
->onError(function (\Throwable $e, mixed $row, string $stage) {
error_log("[{$stage}] {$e->getMessage()}");
})
->transform(fn($row) => $row)
->load(fn($row) => $row)
->run();
shell
php scripts/verify-release.php <version>