如何在 api-platform 结果上添加额外信息 [英] How to add an extra information on api-platform result
本文介绍了如何在 api-platform 结果上添加额外信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 symfony 和 api 平台.
我有一个资源:
/*** @ApiResource()*/信用等级{/*** @ORM\Id()* @ORM\GeneratedValue()* @ORM\Column(type="整数")*/私人 $id;/*** @ORM\Column(type="整数")*/私人价值;}
/api/credits
的结果是:
<代码>{"@context": "/api/contexts/Credit","@id": "/api/credits","@type": "hydra:Collection",九头蛇:成员":[{"@id": "/api/credits/1","@type": "信用",身份证":1,价值":200,"createdAt": "2019-03"},{"@id": "/api/credits/2","@type": "信用",身份证":2,价值":200,"createdAt": "2019-04"}],九头蛇:totalItems":2}
<块引用>
我想在我的结果中添加一个额外的信息,比如 totalValues :400(所有结果的值"之和)
我该怎么办
预期结果:
<代码> {"@context": "/api/contexts/Credit","@id": "/api/credits","@type": "hydra:Collection",九头蛇:成员":[{"@id": "/api/credits/1","@type": "信用",身份证":1,价值":200,"createdAt": "2019-03"},{"@id": "/api/credits/2","@type": "信用",身份证":2,价值":200,"createdAt": "2019-04"}],"hydra:totalItems": 2,总价值":400}
解决方案
解决方案是像这样实现 NormalizerInterface 和 NormalizerAwareInterface :
ApiCollectionNormalizer:
命名空间 App\Serializer;使用 Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;使用 Symfony\Component\Serializer\Normalizer\NormalizerInterface;最终类 ApiCollectionNormalizer 实现 NormalizerInterface、NormalizerAwareInterface{/*** @var NormalizerInterface|NormalizerAwareInterface*/私人 $ 装饰;公共函数 __construct(NormalizerInterface $decorated){if (!$decorated instanceof NormalizerAwareInterface) {抛出新的 \InvalidArgumentException(sprintf('装饰的规范化器必须实现 %s.', NormalizerAwareInterface::class));}$this->decorated = $decorated;}/*** @inheritdoc*/公共函数 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'] 作为 $credit) {$totalValues += $credit['value'];}$data['totalValues'] = $totalValues;}}返回 $data;}/*** @inheritdoc*/公共函数supportsNormalization($data, $format = null){return $this->decorated->supportsNormalization($data, $format);}/*** @inheritdoc*/公共函数 setNormalizer(NormalizerInterface $normalizer){$this->decorated->setNormalizer($normalizer);}}
services.yaml:
'App\Serializer\ApiCollectionNormalizer':装饰:'api_platform.hydra.normalizer.collection'参数:['@App\Serializer\ApiCollectionNormalizer.inner']
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
}
解决方案
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' ]
这篇关于如何在 api-platform 结果上添加额外信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文