Pipes

Pipes in Assegai are a way to transform the input or output of a handler method. They are executed before or after the method they are bound to, and they can be used to do things like validate request data, parse input, and format output.

Custom pipes

To create a Pipe in Assegai, you will need to create a class that implements the IPipeTransform interface. This interface has a single method called transform, which takes in the value to be transformed and any metadata that you may want to pass along.

Here's an example of a Pipe that validates a request body:

src/Util/Validation/ValidationPipe.php
use Assegai\Core\Interfaces\IPipeTransform;
use Assegai\Core\Attributes\Injectable;
use stdClass;

#[Injectable]
class ValidationPipe implements IPipeTransform
{
  public function transform(mixed $value, array|stdClass|null $metaData = null): mixed
  {
    // Validation code goes here    return $value;
  }
}

Binding pipes

To bind a Pipe to a handler method, you will need to use an attribute. In this example, we're using the #[Post] attribute to bind the ValidationPipe to a handler that creates a new resource:

#[Post]
public function create(#[Body(pipes: ValidationPipe::class)] object $requestBody)
{
  // Handler code goes here}

Built-in pipes

Assegai comes with several built-in pipes that you can use out of the box. Here's a list of them:

Name Description
ValidationPipe Validates request data using the validation rules specified in the attribute
ParseIntPipe Parses an integer from a string
ParseFloatPipe Parses a float from a string
ParseBoolPipe Parses a boolean from a string
ParseArrayPipe Parses an array from a string
ParseUUIDPipe Parses a UUID from a string
ParseEnumPipe Parses a value from a set of predefined options
DefaultValuePipe Sets a default value if the input is null
ParseFilePipe Parses an uploaded file from the request

You can use these pipes by importing them and specifying them in the parameter attributes of your handler method.