PHP code example of tcds-io / php-jackson-symfony

1. Go to this page and download the library: Download tcds-io/php-jackson-symfony library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tcds-io / php-jackson-symfony example snippets


// config/bundles.php
return [
    // ...
    Tcds\Io\Jackson\Symfony\JacksonBundle::class => ['all' => true],
];

use Symfony\Component\Routing\Attribute\Route;
use Tcds\Io\Jackson\Symfony\Attributes\JacksonInject;
use Tcds\Io\Jackson\Symfony\Attributes\JacksonResponse;

class FooBarController
{
    /**
     * @param list<Foo> $items
     * @return list<Foo>
     */
    #[Route('/resource', methods: ['POST'])]
    #[JacksonResponse]
    public function list(#[JacksonInject] array $items): array
    {
        return $items;
    }

    #[Route('/resource/{id}', methods: ['POST'])]
    #[JacksonResponse]
    public function read(int $id, #[JacksonInject] Foo $foo): Foo
    {
        return new Foo(
            id: $id,
            a: $foo->a,
            b: $foo->b,
            type: $foo->type,
        );
    }
}

use Symfony\Component\Routing\Attribute\Route;
use Tcds\Io\Jackson\Symfony\Attributes\JacksonInject;
use Tcds\Io\Jackson\Symfony\Attributes\JacksonResponse;

#[Route('/greet', methods: ['POST'])]
final readonly class GreetController
{
    #[JacksonResponse(status: 201, headers: ['X-Resource' => 'greeting'])]
    public function __invoke(#[JacksonInject] Greeting $greeting): Greeting
    {
        return $greeting;
    }
}

use Symfony\Component\Routing\Attribute\Route;
use Tcds\Io\Jackson\Symfony\Attributes\JacksonInject;
use Tcds\Io\Jackson\Symfony\Attributes\JacksonResponse;

class GreetController
{
    #[Route('/greet', methods: ['POST'])]
    #[JacksonResponse(status: 201, headers: ['X-Resource' => 'greeting'])]
    public function store(#[JacksonInject] Greeting $greeting): Greeting
    {
        return $greeting;
    }
}

use App\Services\AuthTokenService;
use Psr\Container\ContainerInterface;
use Tcds\Io\Jackson\ObjectMapper;

return [
    'mappers' => [
        // Simple automatic request and response mapping
        Address::class => [],

        // Custom readers and writers
        Foo::class => [
            'reader' => fn(array $data) => new Foo($data['a'], $data['b']),
            'writer' => fn(Foo $foo) => ['a' => $foo->a, 'b' => $foo->b],
        ],

        // Control how sensitive objects are exposed in responses
        Account::class => [
            'writer' => fn(Account $account) => [
                'id' => $account->id,
                'name' => $account->name,
                // 'apiKey' => $account->apiKey, // exclude sensitive fields
            ],
        ],
    ],
    'params' => fn(ContainerInterface $container, ObjectMapper $mapper) => [
        'userId' => $container->get(AuthTokenService::class)->userId(),
    ],
];

use Symfony\Component\Routing\Attribute\Route;

class FooBarController
{
    #[Route('/resource/{id}', methods: ['POST'])]
    public function read(int $id, Foo $foo): Foo
    {
        return new Foo(
            id: $id,
            a: $foo->a,
            b: $foo->b,
            type: $foo->type,
        );
    }
}

use App\Services\AuthTokenService;
use Psr\Container\ContainerInterface;

return [
    'params' => fn(ContainerInterface $container) => [
        'userId' => $container->get(AuthTokenService::class)->userId(),
    ],
    // ...
];

readonly class InvoiceQuery
{
    public function __construct(
        public int $userId,
        public ?string $customer = null,
    ) {}
}

use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Tcds\Io\Jackson\Exception\UnableToParseValue;

return [
    'errors' => [
        'request' => fn(UnableToParseValue $e) => new JsonResponse([
            'error' => $e->getMessage(),
            'hint' => 'Check the request body format.',
        ], Response::HTTP_UNPROCESSABLE_ENTITY),
    ],
    // ...
];
bash
bin/console jackson:configure # creates config/jackson.php
bash
bin/console jackson:configure # creates config/jackson.php