alexlisenkov/uuid-argument-resolver-bundle

ramsey/uuid argument resolver bundle for Symfony

1.0.4 2020-12-09 09:33 UTC

This package is auto-updated.

Last update: 2024-11-09 18:43:41 UTC


README

ramsey/uuid argument resolver bundle for Symfony

Coverage Status CI Packagist Downloads PHP Versions

Install

composer require alexlisenkov/uuid-argument-resolver-bundle

Usage

namespace App\Controllers;

use Psr\Http\Message\ResponseInterface;
use Ramsey\Uuid\UuidInterface;

class ResourceController
    /**
     * @Route("/{uuid}", name="show_resource", methods="GET")
     */
    public function show(UuidInterface $resourceUuid, ResourceRepository $resourceRepository): ResponseInterface
    {
        $resource = $resourceRepository->findOneByUuid($resourceUuid);

        if ($resource === null) {
            return new ResourceNotFoundResponse();
        }

        return new ResourceResponse($resource);
    }

Handling invalid uuid

By default, it will respond with 400 Bad Request with body Invalid UUID. But you can configure this by creating a service.

Custom response

Create a factory that creates a Psr\Http\Message\ResponseInterface.

namespace App\Factory;

use Psr\Http\Message\ResponseInterface;
use Symfony\Component\HttpFoundation\Response;

class InvalidUuidResponseFactory
{
    public static function create(): ResponseInterface
    {
        return new Response(400, [], 'Invalid UUID');
    }
}

Override the alexlisenkov.uuid_argument_resolver_bundle.uuid_invalid_response service with your factory.

alexlisenkov.uuid_argument_resolver_bundle.uuid_invalid_response:
    class: '@Psr\Http\Message\ResponseInterface'
    factory: [ 'App\Factory\InvalidUuidResponseFactory', create ]

Now an invalid UUID will return your response.

Testing

composer test

Contributing

Contributions are welcome.