Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(identifier): update identifier doc to use php attribute to tag service for uri transformer #2136

Open
wants to merge 1 commit into
base: 4.0
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions core/identifiers.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,60 @@ services:

</code-selector>

You can also use `#[AutoconfigureTag('api_platform.uri_variables.transformer')]` to tag the service.

```php
<?php

namespace App\Identifier;

use ApiPlatform\Api\UriVariableTransformerInterface;
use ApiPlatform\Exception\InvalidUriVariableException;
use App\Uuid;
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
use Ramsey\Uuid\Exception\InvalidUuidStringException;

#[AutoconfigureTag('api_platform.uri_variables.transformer')]
final class UuidUriVariableTransformer implements UriVariableTransformerInterface
{
/**
* Transforms a URI variable value.
*
* @param mixed $value The URI variable value to transform
* @param array $types The guessed type behind the URI variable
* @param array $context Options available to the transformer
*
* @throws InvalidUriVariableException Occurs when the URI variable could not be transformed
*/
public function transform($value, array $types, array $context = []): Uuid
{
try {
return Uuid::fromString($value);
} catch (InvalidUuidStringException $e) {
throw new InvalidUriVariableException($e->getMessage());
}
}

/**
* Checks whether the given URI variable is supported for transformation by this transformer.
*
* @param mixed $value The URI variable value to transform
* @param array $types The types to which the data should be transformed
* @param array $context Options available to the transformer
*/
public function supportsTransformation($value, array $types, array $context = []): bool
{
foreach ($types as $type) {
if (is_a($type, Uuid::class, true)) {
return true;
}
}

return false;
}
}
```

Your `PersonProvider` will now work as expected!

### Tag the Service using Laravel
Expand Down
Loading