flow php

UNIFIED DATA PROCESSING FRAMEWORK

composer require flow-php/etl ~0.25.0

ChangelogRelease Cycle

elephant
extract

Extracts

Read from various data sources.

arrow
transform

Transforms

Shape and optimize for your needs.

arrow
load

Loads

Store and secure in one of many available data sinks.

Examples:

Description

Using when with nested else_when conditions for multi-tier logic.

This example demonstrates how to nest multiple when functions to create complex conditional logic, similar to if-elseif-elseif-else chains in traditional programming.

The pattern evaluates conditions in order:

  1. If score >= 90, grade is 'A'
  2. Else if score >= 70, grade is 'B'
  3. Else if score >= 50, grade is 'C'
  4. Else grade is 'F'

This approach is useful for categorizing data into multiple levels or implementing grading systems, priority levels, or status determination based on multiple criteria.

composer.json
{
    "name": "flow-php/examples",
    "description": "Flow PHP - Examples",
    "license": "MIT",
    "type": "library",
    "require": {
        "flow-php/etl": "1.x-dev"
    },
    "minimum-stability": "dev"
}
code.php
<?php

declare(strict_types=1);

use function Flow\ETL\DSL\{data_frame, from_array, lit, ref, to_stream, when};

require __DIR__ . '/vendor/autoload.php';

data_frame()
    ->read(from_array([
        ['id' => 1, 'score' => 95, 'email' => '[email protected]'],
        ['id' => 2, 'score' => 75, 'email' => '[email protected]'],
        ['id' => 3, 'score' => 55, 'email' => '[email protected]'],
        ['id' => 4, 'score' => 30, 'email' => '[email protected]'],
    ]))
    ->collect()
    ->withEntry(
        'grade',
        when(
            ref('score')->greaterThanEqual(lit(90)),
            lit('A'),
            when(
                ref('score')->greaterThanEqual(lit(70)),
                lit('B'),
                when(
                    ref('score')->greaterThanEqual(lit(50)),
                    lit('C'),
                    lit('F')
                )
            )
        )
    )
    ->write(to_stream(__DIR__ . '/output.txt', truncate: false))
    ->run();

Output

+----+-------+---------------------+-------+
| id | score |               email | grade |
+----+-------+---------------------+-------+
|  1 |    95 | [email protected] |     A |
|  2 |    75 | [email protected] |     B |
|  3 |    55 | [email protected] |     C |
|  4 |    30 | [email protected] |     F |
+----+-------+---------------------+-------+
4 rows

Contributors

Join us on GitHub external resource
scroll back to top