Proveedor y procesador reutilizable Entidad->Dto
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
With a Subscription, click any sentence in the script to jump to that part of the video!
Login Subscribe¡Nuestro UserAPI es ahora una clase de recursos API totalmente funcional! Tenemos nuestroEntityToDtoStateProvider, que llama al proveedor de estado central de Doctrine, y que nos proporciona todo lo bueno, como la consulta, el filtrado y la paginación. Luego, aquí abajo, aprovechamos el sistema MicroMapper para convertir los objetos $entity en objetosUserApi.
Y hacemos lo mismo en el procesador. Utilizamos MicroMapper para pasar deUserApi a nuestra entidad User... y luego llamamos al procesador de estado central de Doctrine para que se encargue de guardar o borrar. ¡Me encanta!
Nuestro sueño es crear un DragonTreasureApi y repetir toda esta magia. Y si podemos hacer que estas clases de procesador y proveedor sean completamente genéricas... eso va a ser superfácil. Así que ¡hagámoslo!
Hacer genérico al proveedor
Empieza en el proveedor. Si buscas "usuario", sólo hay un lugar: donde le decimos a MicroMapper en qué clase convertir nuestro $entity. ¿Podemos... obtener esto dinámicamente? Aquí arriba, nuestro proveedor recibe el $operation y el $context. Volquemos ambos.
Como esto está en nuestro proveedor... podemos ir a actualizar la ruta Colección y... ¡boom! Se trata de una operación GetCollection... y compruébalo. El objeto de la operación almacena la clase ApiResource a la que está asociado
Así que por aquí, es sencillo: $resourceClass = $operation->getClass(). Ahora que tenemos eso, aquí abajo, conviértelo en un argumento - string $resourceClass - y pásalo en su lugar. Por último, tenemos que añadir $resourceClass como argumento cuando llamemos a mapEntityToDto() ahí... y justo ahí. Elimina la declaración use que ya no necesitamos y... así de fácil... ¡sigue funcionando!
Hacer que el procesador sea genérico
¡Ya estamos en marcha! Dirígete al procesador y busca "usuario". Ah, tenemos el mismo problema excepto que, esta vez, necesitamos la clase de entidad User.
¡Vale! Arriba, dd($operation). Y para ello, necesitamos ejecutar una de nuestras pruebas:
symfony php bin/phpunit --filter=testPostToCreateUser
Y... ¡ya está! Vemos la operación Post... y la clase es, por supuesto,UserApi. Pero esta vez necesitamos la clase User. Recuerda: en UserApi, utilizamos stateOptions para decir que UserApi está vinculada a la entidad User. Y ahora, podemos leer esta información de la operación. Si nos desplazamos un poco hacia abajo... ahí está: la propiedad stateOptions con el objeto Options, y entityClass dentro.
¡Genial! De vuelta al procesador, hacia arriba... quita el dd() y empieza por $stateOptions = $operation->getStateOptions(). Luego, para ayudar a mi editor (y también por si configuro algo mal), assert($stateOptions instanceof Options)(el del ORM Doctrine).
Puedes utilizar diferentes clases Options para $stateOptions... como si estuvieras obteniendo datos de ElasticSearch, pero sabemos que estamos utilizando esta de Doctrine. A continuación, digamos $entityClass = $stateOptions->getEntityClass().
Y... no necesitamos este assert() de aquí abajo, entonces pasa $entityClass amapDtoToEntity(). Por último, úsalo con string $entityClass... y pásalo también aquí.
Cuando ahora busquemos "usuario"... podemos deshacernos de las dos declaraciones use... y... ¡ya estamos limpios! ¡Es genérico! ¡Prueba el test!
symfony php bin/phpunit --filter=testPostToCreateUser
¡Ya está! ¡Estamos listos! ¡Tenemos un proveedor y un procesador reutilizables! A continuación, creemos una clase DragonTreasureApi, repitamos esta magia, ¡y veamos lo rápido que conseguimos que las cosas encajen!
8 Comments
I'm having issues with my PHPStan telling me getStateOptions is from an internal class which it is. I'm using this in my provider where I can reach out for the stateOptions via $operation and $context. However both access the ApiPlatform\Metadata\Metadata class which is an internal one. Is there a different way to reach out for this data?
Hey @Arthur
I think you can access that data through an ApiPlatform state provider.
Here are the docs about them: https://api-platform.com/docs/core/state-providers/
Cheers!
Hey @MolloKhan thanks for reaching out! That's indeed my problem. As stated in the docs I'm implementing the ProviderInterface. However via this class I'm not able to retrieve e.g. the stateoptions because this routes me via the ApiPlatform\Metadata\Metadata class.
So the solution works fine and I have working code. However PHPStan tells me not to use internal classes. Which I ofcourse can ignore. However I'm looking for a neat solution for this :) which perhaps there isn't.
Ohh gotcha, yea... in theory, you should not rely on internal classes, but you could add a couple of tests so they will fail as soon as those classes change in a new version.
It seems the example provided does not work when using Writable Relation Fields with serialization groups.
Trying to create a new object though a relationship throws the dreaded
Unable to generate an IRI for the item of type \"App\\ApiResource\\EntityApi\""error. This seems to only affect Post Operations due to relationships not being properly hydrated during serialization. Get, Get Collection, and Patch all seem to work fine here.For example, Lets say you have two entities: Business - OneToOne - Address. If you were to try create a new business by posting
{ "name":"Little Caesars", "address": { "city": "Detroit" }}to your/api/business/endpoint you would see the following errorUnable to generate an IRI for the item of type \"App\\ApiResource\\AddressApi\"". This is happening because the example in the script does not fully hydrate the DTO before api-platform tries to serialize.Below is a quick and dirty fix that resolved the issue for me on my end.
Hope this helps someone!
Thanks for posting this! This is indeed a problem! I'm fine with your solution! Some other users have pointed out that you can also fix this in the mapper, which I quite like! Here's a description of that approach: https://symfonycasts.com/screencast/api-platform-extending#comment-31551 - if it's unclear let me know, but I think you'll understand the approach.
Cheers!
One issue I came across with the generic/custom
StateProcessoris that not all Entities useid/getId()as their identifier.So in my DTO class, I leveraged the
extraPropertiesarray in the#[ApiResource]to expose that information to theStateProcessor.The upside is that this works! The downside is that I already told ApiPlatform what my resource's ID is in my DTO class in the properties using the
ApiPropertyattribute. But there doesn't seem to be a way to get that information in theprocessmethod of theStateProcessor. Would be nice to not have to add thisextraPropertiesconfig, but hey, this does the trick!Any thoughts about gotchas with this approach?
DTO class
DtoToEntityStateProcessor class
Hey @LCBWeb!
What a cool use of
extraProperties! Well done 👏. But I hear what you mean: this info should already be available. Grabbing the info looks oddly complex. The closest I could find is what's done in this class: https://github.com/api-platform/core/blob/main/src/Api/IdentifiersExtractor.phpIf you poke in there and have success, let me know. Otherwise, it's a tiny bit of duplication, but I think your solution is fine too. And if you only have a few different ids, you could always use a
method_exists()to check for the 2-3 possible getter methods for your id and determine it that way.Cheers!
"Houston: no signs of life"
Start the conversation!