API平台如何返回深层级联数据?
创始人
2024-09-08 01:01:03
0

使用API平台的序列化组件Symfony Serializer来返回深层级联数据。

代码示例:

  1. 创建一个序列化器类,命名为CustomSerializer.php
namespace App\Serializer;

use ApiPlatform\Core\Bridge\Doctrine\Orm\Paginator;
use ApiPlatform\Core\Exception\InvalidArgumentException;
use ApiPlatform\Core\JsonLd\Serializer\ContextTrait;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;

/**
 * A custom serializer that can handle deep association data.
 */
class CustomSerializer extends Serializer
{
    use ContextTrait;

    public function __construct(
        iterable $normalizers = [],
        iterable $encoders = []
    ) {
        $normalizers = array_merge($normalizers, [
            // Doctrine paginator normalizer
            new class() implements DenormalizerInterface {
                public function denormalize($data, $class, $format = null, array $context = []) : Paginator
                {
                    return Paginator::createFromCollection($data['data'], $data['totalItems'], $context['filters']['page'], $context['filters']['itemsPerPage']);
                }

                public function supportsDenormalization($data, $type, $format = null)
                {
                    return $type === Paginator::class && $format === 'jsonapi';
                }
            },
            // Object normalizer
            new ObjectNormalizer(null, null, null, null, null, null, $this->extractAttributesToSerialize([])),
        ]);

        parent::__construct($normalizers, $encoders);
    }

    /**
     * @param string $data
     * @param string $type
     * @param string $format
     * @param array $context
     * @return object
     * @throws InvalidArgumentException
     */
    public function deserialize($data, $type, $format = null, array $context = [])
    {
        if (strpos($format ?? '', 'json') !== false) {
            $data = $this->decode($data, $format);
        }

        return parent::deserialize($data, $type, $format, $this->createContext($format, $context));
    }

    /**
     * @param mixed $data
     * @param string $format
     * @param array $context
     * @return string
     * @throws InvalidArgumentException
     */
    public function serialize($data, $format = null, array $context = [])
    {
        $context += ['json_encode_options' => JsonResponse::DEFAULT_ENCODING_OPTIONS];
        $context = $this->createContext($format, $context);
        $context['ignored_attributes'] = $this->extractAttributesToSerialize($

相关内容

热门资讯

推荐十款(微扑克系统)外挂透明... 推荐十款(微扑克系统)外挂透明挂辅助工具(透视)辅助透视(新版有挂)-哔哩哔哩 科技详细教程;757...
透视挂(wpk数据)外挂透明挂... 自定义wpk数据系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅助器,不管你是想...
透明神器!wpk开挂(WepO... 透明神器!wpk开挂(WepOke)外挂透明挂辅助APP(辅助挂)力荐教程(有挂存在)-哔哩哔哩;超...
2024教程(哈糖大菠萝洗牌)... 这是一款非常优秀的哈糖大菠萝洗牌 ia辅助检测软件,能够让你了解到哈糖大菠萝洗牌中牌率当中全部隐藏参...
必备辅助推荐(Wepoke安卓... 必备辅助推荐(Wepoke安卓)外挂透明挂辅助工具(透视)辅助透视(确实有挂)-哔哩哔哩;值得一提的...
九分钟了解(wepokeai代... 九分钟了解(wepokeai代打)外挂透明挂辅助工具(透视)透视辅助(有挂实锤)-哔哩哔哩;原来确实...
透视美元局(Wepoke教程)... 透视美元局(Wepoke教程)外挂透明挂辅助挂(辅助挂)确实是真的有挂(2020已更新)(哔哩哔哩)...
盘点十款!德州(WEPOKE)... 盘点十款!德州(WEPOKE)外挂透明挂辅助工具(辅助挂)曝光教程(有挂秘籍)-哔哩哔哩1、玩家可以...
分辨真假(wpk下载)外挂透明... 分辨真假(wpk下载)外挂透明挂辅助APP(辅助挂)软件透明挂(有挂方法)-哔哩哔哩相信很多朋友都在...
透视计算(aapoker苹果版... 透视计算(aapoker苹果版)外挂透明挂辅助软件(辅助挂)透视辅助(有挂实锤)-哔哩哔哩;原来确实...