提交 2c0b5f97 编写于 作者: 伟伟权

117. PostAwareNromalizer

上级 df2c3493
...@@ -98,6 +98,9 @@ class Post ...@@ -98,6 +98,9 @@ class Post
// #[ApiProperty(readableLink: true, writableLink: true)] // #[ApiProperty(readableLink: true, writableLink: true)]
private $author; private $author;
#[Groups(['post:read', 'post:item:get'])]
private $postImageUrl;
public function __construct() public function __construct()
{ {
$this->comments = new ArrayCollection(); $this->comments = new ArrayCollection();
...@@ -245,4 +248,22 @@ class Post ...@@ -245,4 +248,22 @@ class Post
return $this; return $this;
} }
/**
* @return mixed
*/
public function getPostImageUrl()
{
return $this->postImageUrl;
}
/**
* @param mixed $postImageUrl
*/
public function setPostImageUrl($postImageUrl): void
{
$this->postImageUrl = $postImageUrl;
}
} }
<?php
namespace App\Serializer\Normalizer;
use App\Entity\Post;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Serializer\Exception\CircularReferenceException;
use Symfony\Component\Serializer\Exception\ExceptionInterface;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
class PostAwareNormalizer implements NormalizerAwareInterface, ContextAwareNormalizerInterface
{
use NormalizerAwareTrait;
/**
* @var RequestStack
*/
private RequestStack $requestStack;
private const NORMALIZE_ALREADY_CALLED = 'normalize_already_called';
public function __construct(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}
public function supportsNormalization($data, string $format = null, array $context = [])
{
if (isset($context[self::NORMALIZE_ALREADY_CALLED])) {
return false;
}
return $data instanceof Post;
}
/**
* @param Post $object
* @param string|null $format
* @param array $context
* @return array|\ArrayObject|bool|float|int|string|null
* @throws ExceptionInterface
*/
public function normalize($object, string $format = null, array $context = [])
{
$context[self::NORMALIZE_ALREADY_CALLED] = true;
$request = $this->requestStack->getCurrentRequest();
$object->setPostImageUrl($request->getSchemeAndHttpHost() . '/uploads/images/' . $object->getPostImage());
return $this->normalizer->normalize($object, $format, $context);
}
}
\ No newline at end of file
...@@ -17,7 +17,7 @@ use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; ...@@ -17,7 +17,7 @@ use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
class PostNormalizer implements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface class PostNormalizer implements DenormalizerInterface, CacheableSupportsMethodInterface
{ {
private $normalizer; private $normalizer;
/** /**
...@@ -36,20 +36,20 @@ class PostNormalizer implements NormalizerInterface, DenormalizerInterface, Cach ...@@ -36,20 +36,20 @@ class PostNormalizer implements NormalizerInterface, DenormalizerInterface, Cach
$this->userRepository = $userRepository; $this->userRepository = $userRepository;
} }
public function normalize($object, $format = null, array $context = []): array // public function normalize($object, $format = null, array $context = []): array
{ // {
$data = $this->normalizer->normalize($object, $format, $context); // $data = $this->normalizer->normalize($object, $format, $context);
//
$request = $this->requestStack->getCurrentRequest(); // $request = $this->requestStack->getCurrentRequest();
// Here: add, edit, or delete some data // // Here: add, edit, or delete some data
$data['post_image_url'] = $request->getSchemeAndHttpHost() .'/uploads/images/'. $data['postImage']; // $data['post_image_url'] = $request->getSchemeAndHttpHost() .'/uploads/images/'. $data['postImage'];
return $data; // return $data;
} // }
//
public function supportsNormalization($data, $format = null): bool // public function supportsNormalization($data, $format = null): bool
{ // {
return $data instanceof \App\Entity\Post; // return $data instanceof \App\Entity\Post;
} // }
public function hasCacheableSupportsMethod(): bool public function hasCacheableSupportsMethod(): bool
{ {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册