Skip to content

Latest commit

 

History

History
138 lines (96 loc) · 3.53 KB

File metadata and controls

138 lines (96 loc) · 3.53 KB

ZenPipe API Reference

The ZenPipe class provides a fluent interface for building and executing pipelines of operations.

Class Overview

namespace DynamikDev\ZenPipe;

class ZenPipe

Methods

Constructor

public function __construct(mixed $initialValue = null)

Creates a new pipeline instance.

  • Parameters:
    • $initialValue (mixed|null): The initial value to be processed through the pipeline.

make()

public static function make(mixed $initialValue = null): self

Static factory method to create a new pipeline instance.

  • Parameters:
    • $initialValue (mixed|null): The initial value to be processed through the pipeline.
  • Returns: A new ZenPipe instance.

withContext()

public function withContext(mixed $context): self

Sets 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 ZenPipe instance for method chaining.

Example:

$pipeline = zenpipe($value)
    ->withContext(new MyContext())
    ->pipe(fn($v, $next, $return, MyContext $ctx) => $next($v));

catch()

public function catch(callable $handler): self

Sets 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 ZenPipe instance 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.

pipe()

public function pipe($operation): self

Adds an operation to the pipeline.

  • Parameters:
    • $operation: Can be one of:
      • callable: A function to process the value
      • array{class-string, string}: A tuple of [className, methodName]
      • array: An array of operations to be added sequentially
      • MiddlewareInterface: A PSR-15 middleware (auto-detected)
  • Returns: The ZenPipe instance for method chaining.
  • Throws: \InvalidArgumentException if the specified class does not exist.

Operation Parameters: Operations receive up to four parameters:

  1. $value - The current value being processed
  2. $next - Callback to pass value to next operation
  3. $return - Callback to exit pipeline early with a value
  4. $context - The context set via withContext() (null if not set)

process()

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: \InvalidArgumentException if no initial value is provided.

__invoke()

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.

asMiddleware()

public function asMiddleware(): MiddlewareInterface

Wraps the pipeline as a PSR-15 middleware.

  • Returns: A MiddlewareInterface instance.

See PSR-15 Middleware in the README for usage details.