Skip to content
Search
Examples

Transformations

Description

Chain multiple conditions for multi-tier logic, similar to if-elseif-else chains. Useful for categorizing data into multiple levels, implementing grading systems, or determining status based on several criteria.

Documentation

Code

<?php

declare(strict_types=1);

use function Flow\ETL\DSL\{data_frame, from_array, lit, ref, to_output, 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_output(truncate: false))
    ->run();
Contributors

Built in the open.

Join us on GitHub
scroll back to top