How to add an extra information on api-platform result

邮差的信 提交于 2020-08-09 07:02:36

问题


I'am using symfony and api platform.

I have a ressource with :

/**
 * @ApiResource()
 */
class Credit
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="integer")
     */
    private $value; 
}

the result of /api/credits is :

{
    "@context": "/api/contexts/Credit",
    "@id": "/api/credits",
    "@type": "hydra:Collection",
    "hydra:member": [
        {
            "@id": "/api/credits/1",
            "@type": "Credit",
            "id": 1,
            "value": 200,
            "createdAt": "2019-03"
        },
        {
            "@id": "/api/credits/2",
            "@type": "Credit",
            "id": 2,
            "value": 200,
            "createdAt": "2019-04"
        }
    ],
    "hydra:totalItems": 2
}

i want to add an extra information to my result like the totalValues : 400 ( sum of "value" of all results )

how can i do it

expected result :

  {
        "@context": "/api/contexts/Credit",
        "@id": "/api/credits",
        "@type": "hydra:Collection",
        "hydra:member": [
            {
                "@id": "/api/credits/1",
                "@type": "Credit",
                "id": 1,
                "value": 200,
                "createdAt": "2019-03"
            },
            {
                "@id": "/api/credits/2",
                "@type": "Credit",
                "id": 2,
                "value": 200,
                "createdAt": "2019-04"
            }
        ],
        "hydra:totalItems": 2,
        "totalValues": 400 
    }

回答1:


Solution is to implement NormalizerInterface and NormalizerAwareInterface like this :

ApiCollectionNormalizer :

namespace App\Serializer;

use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

final class ApiCollectionNormalizer implements NormalizerInterface, NormalizerAwareInterface
{
    /**
     * @var NormalizerInterface|NormalizerAwareInterface
     */
    private $decorated;

    public function __construct(NormalizerInterface $decorated)
    {
        if (!$decorated instanceof NormalizerAwareInterface) {
            throw new \InvalidArgumentException(
                sprintf('The decorated normalizer must implement the %s.', NormalizerAwareInterface::class)
            );
        }
        $this->decorated = $decorated;
    }

    /**
     * @inheritdoc
     */
    public function normalize($object, $format = null, array $context = [])
    {
        $data = $this->decorated->normalize($object, $format, $context);
        if ('collection' === $context['operation_type'] && 'get' === $context['collection_operation_name']) {
            if ($data['@id'] === '/api/credits') {
                $totalValues = 0;
                foreach ($data['hydra:member'] as $credit) {
                    $totalValues += $credit['value'];
                }
                $data['totalValues'] = $totalValues;
            }

        }
        return $data;
    }

    /**
     * @inheritdoc
     */
    public function supportsNormalization($data, $format = null)
    {
        return $this->decorated->supportsNormalization($data, $format);
    }

    /**
     * @inheritdoc
     */
    public function setNormalizer(NormalizerInterface $normalizer)
    {
        $this->decorated->setNormalizer($normalizer);
    }

}

services.yaml :

  'App\Serializer\ApiCollectionNormalizer':
    decorates: 'api_platform.hydra.normalizer.collection'
    arguments: [ '@App\Serializer\ApiCollectionNormalizer.inner' ]


来源:https://stackoverflow.com/questions/55921473/how-to-add-an-extra-information-on-api-platform-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!