The ZenPipe class provides a fluent interface for building and executing pipelines of operations.
namespace DynamikDev\ZenPipe;
class ZenPipepublic function __construct(mixed $initialValue = null)Creates a new pipeline instance.
- Parameters:
$initialValue(mixed|null): The initial value to be processed through the pipeline.
public static function make(mixed $initialValue = null): selfStatic factory method to create a new pipeline instance.
- Parameters:
$initialValue(mixed|null): The initial value to be processed through the pipeline.
- Returns: A new
ZenPipeinstance.
public function withContext(mixed $context): selfSets a context object that will be passed to all operations as the fourth parameter.
- Parameters:
$context(mixed): Any value to be passed as context (object, array, DTO, etc.)
- Returns: The
ZenPipeinstance for method chaining.
Example:
$pipeline = zenpipe($value)
->withContext(new MyContext())
->pipe(fn($v, $next, $return, MyContext $ctx) => $next($v));public function catch(callable $handler): selfSets an exception handler for the pipeline.
- Parameters:
$handler(callable): A function that receives(Throwable $e, mixed $originalValue, mixed $context)and returns a fallback value.
- Returns: The
ZenPipeinstance for method chaining.
Example:
$pipeline = zenpipe($value)
->withContext($myContext)
->pipe(fn($v, $next) => $next(riskyOperation($v)))
->catch(fn($e, $value, $ctx) => ['error' => $e->getMessage()]);If an exception occurs and no catch handler is set, the exception propagates normally.
public function pipe($operation): selfAdds an operation to the pipeline.
- Parameters:
$operation: Can be one of:callable: A function to process the valuearray{class-string, string}: A tuple of [className, methodName]array: An array of operations to be added sequentiallyMiddlewareInterface: A PSR-15 middleware (auto-detected)
- Returns: The
ZenPipeinstance for method chaining. - Throws:
\InvalidArgumentExceptionif the specified class does not exist.
Operation Parameters: Operations receive up to four parameters:
$value- The current value being processed$next- Callback to pass value to next operation$return- Callback to exit pipeline early with a value$context- The context set viawithContext()(null if not set)
public function process($initialValue = null)Executes the pipeline with the given initial value.
- Parameters:
$initialValue(mixed|null): The value to process. If not provided, uses the value from constructor.
- Returns: The processed value after running through all operations.
- Throws:
\InvalidArgumentExceptionif no initial value is provided.
public function __invoke($initialValue)Makes the pipeline instance callable.
- Parameters:
$initialValue: The value to process through the pipeline.
- Returns: The processed value after running through all operations.
public function asMiddleware(): MiddlewareInterfaceWraps the pipeline as a PSR-15 middleware.
- Returns: A
MiddlewareInterfaceinstance.
See PSR-15 Middleware in the README for usage details.