componenta/serialize-interceptor

Symfony Serializer result interceptor for Componenta

Maintainers

Package info

github.com/componenta/serialize-interceptor

pkg:composer/componenta/serialize-interceptor

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-06-16 23:20 UTC

This package is auto-updated.

Last update: 2026-06-17 15:44:05 UTC


README

Result interceptor for componenta/interceptor that serializes callable return values through Symfony\Component\Serializer\SerializerInterface.

Русская документация

Boundary

This package contains only the #[Serialize] attribute and SerializeInterceptor. It does not configure Symfony Serializer for the application: the container must provide SerializerInterface.

Use componenta/interceptor directly when you only need the callable interceptor pipeline.

Installation

composer require componenta/serialize-interceptor

Requirements

  • PHP 8.4+
  • componenta/interceptor
  • symfony/serializer

Quick Start

use Componenta\Interceptor\Http\Attribute\Respond;
use Componenta\Interceptor\Serialization\Attribute\Serialize;

final class UserController
{
    #[Respond(200, 'application/json')]
    #[Serialize(context: ['groups' => ['user:read']])]
    public function show(): User
    {
        return $this->user;
    }
}

#[Serialize] extends Componenta\Interceptor\Attribute\Intercept, so it is read by the standard AttributeInterceptor. SerializeInterceptor is created through the DI factory, and SerializerInterface is resolved from the container.

Dynamic Context

Context can be built from already resolved method parameters. The array key is the parameter name, and the value is the public property name:

use Componenta\Interceptor\Serialization\Attribute\Serialize;

final class PostController
{
    #[Serialize(context: [
        'query' => 'includeAuthor',
        Serialize::ATTR_MAP => ['includeAuthor' => 'with_author'],
    ])]
    public function index(PostListQuery $query): array
    {
        return $this->posts->list($query);
    }
}

If $query->includeAuthor is true, serializer context receives ['with_author' => true].

Return-Level Context

A method may return special keys when context depends on the result:

use Componenta\Interceptor\Serialization\Attribute\Serialize;

return [
    Serialize::ATTR_DATA => $post,
    Serialize::ATTR_CONTEXT => ['groups' => ['post:detail']],
];

ATTR_CONTEXT is merged with attribute context and wins on duplicate keys.

Ordering With Other Interceptors

If the result must become an HTTP response, place the response interceptor above #[Serialize]:

#[Respond(200, 'application/json')] // outer layer
#[Serialize]                        // inner layer, receives the raw result first
public function show(): User {}

Details: componenta/interceptor describes layer ordering and AttributeInterceptor.

License

MIT