Kevin Eaton

Guess what else you can do?

A few more things the hub gives you for free.

The subject travels in an envelope

The hub wraps your subject in a PipeEnvelope, and that is what each pipe receives as its first parameter (and what sendIt() returns). Leave that parameter untyped, or type it PipeEnvelope. Reads, writes and method calls go straight through to your object:

<?php

use Keaton\PipeDream\PipeHub;

$post = (object) ['title' => '  hello,   PIPE dream  '];

$envelope = PipeHub::create()
    ->addPipes([
        fn ($post) => $post->title = trim($post->title),
    ])
    ->sendIt($post);

echo get_class($envelope), PHP_EOL;
echo $envelope->title, PHP_EOL;                         // reads go straight to your object
echo $envelope->subject() === $post ? 'the same $post' : 'a copy', PHP_EOL;
Keaton\PipeDream\PipeEnvelope
hello,   PIPE dream
the same $post

Pipes can share values

The envelope also holds values of its own, for the pipes to pass along: set() one in a pipe, get() it in a later one. That makes small state machines easy:

<?php

use Keaton\PipeDream\PipeHub;

$post = (object) ['title' => 'draft: Hello, Pipe Dream'];

$envelope = PipeHub::create()
    ->addPipes([
        function ($post) {
            $post->set('state', str_starts_with($post->title, 'draft:') ? 'draft' : 'ready');
        },
        function ($post) {
            if ($post->get('state') === 'draft') {
                $post->title = trim(substr($post->title, 6));
                $post->set('state', 'in review');
            }
        },
    ])
    ->sendIt($post);

echo $envelope->get('state'), ': ', $post->title;
in review: Hello, Pipe Dream