flow php

Example: When

Topic: Transformations

Code

<?php

declare(strict_types=1);

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

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

data_frame()
    ->read(from_array([
        ['id' => 1, 'email' => '[email protected]', 'active' => true, 'tags' => ['foo', 'bar']],
        ['id' => 2, 'email' => '[email protected]', 'active' => false, 'tags' => ['biz', 'bar']],
        ['id' => 3, 'email' => '[email protected]', 'active' => true, 'tags' => ['bar', 'baz']],
    ]))
    ->collect()
    ->withEntry(
        'is_special',
        when(
            ref('active')->isTrue()->and(ref('tags')->contains('foo')),
            lit(true),
            lit(false)
        )
    )
    ->write(to_stream(__DIR__ . '/output.txt', truncate: false))
    ->run();

Output

+----+---------------------+--------+---------------+------------+
| id |               email | active |          tags | is_special |
+----+---------------------+--------+---------------+------------+
|  1 | [email protected] |   true | ["foo","bar"] |       true |
|  2 | [email protected] |  false | ["biz","bar"] |      false |
|  3 | [email protected] |   true | ["bar","baz"] |      false |
+----+---------------------+--------+---------------+------------+
3 rows

Contributors

Join us on GitHub external resource
scroll back to top