webbhuset/pipeline

A library of data-manipulation functions.

v2022.10.18.0 2022-10-18 12:20 UTC

This package is auto-updated.

Last update: 2024-04-18 15:15:51 UTC


README

Documentation Status License: MIT

Pipeline is a PHP library for building reusable functions for manipulating values. Every Pipeline function is a class implementing __invoke(), thus allowing instances to be run as functions. Every function takes a Traversable as input and returns a Generator.

Documentation

Documentation is available at ReadTheDocs.

Example

<?php

use Webbhuset\Pipeline\Constructor as F;

$fun = F::Compose([
    F::Map('trim'),
    F::Filter('is_numeric'),
    F::Map('intval'),
    F::Drop(2),
    F::Multiplex(
        function ($value) {
            return $value % 10 == 0 ? 'divide' : 'double';
        },
        [
            'divide' => F::Map(function ($value) {
                return $value / 10;
            }),
            'double' => F::Map(function ($value) {
                return $value * 2;
            }),
        ]
    )
]);

$input = [
    1,
    '  23 ',
    'hello',
    '4.444',
    5.75,
    '+12e3'
];

echo json_encode(iterator_to_array($fun($input)));

// Output: [8,10,1200]