Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alexlisenkov/uuid-argument-resolver
ramsey/uuid argument resolver bundle for Symfony
https://github.com/alexlisenkov/uuid-argument-resolver
argument-parser php symfony-bundle uuid
Last synced: 29 days ago
JSON representation
ramsey/uuid argument resolver bundle for Symfony
- Host: GitHub
- URL: https://github.com/alexlisenkov/uuid-argument-resolver
- Owner: AlexLisenkov
- License: mit
- Created: 2020-12-04T10:26:22.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2020-12-09T09:35:19.000Z (almost 4 years ago)
- Last Synced: 2024-10-04T13:14:23.171Z (about 2 months ago)
- Topics: argument-parser, php, symfony-bundle, uuid
- Language: PHP
- Homepage:
- Size: 23.4 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# UUID Argument Resolver Bundle
ramsey/uuid argument resolver bundle for Symfony[![Coverage Status](https://coveralls.io/repos/github/AlexLisenkov/uuid-argument-resolver/badge.svg?branch=main)](https://coveralls.io/github/AlexLisenkov/uuid-argument-resolver?branch=main)
![CI](https://github.com/AlexLisenkov/uuid-argument-resolver/workflows/CI/badge.svg)
![Packagist Downloads](https://img.shields.io/packagist/dt/alexlisenkov/uuid-argument-resolver-bundle)
![PHP Versions](https://img.shields.io/badge/PHP-%5E7.3%20%7C%7C%20%5E8.0-blue)## Install
```shell
composer require alexlisenkov/uuid-argument-resolver-bundle
```## Usage
```php
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`.
```php
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.
```yaml
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
```shell
composer test
```## Contributing
Contributions are welcome.