Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
teebbstudios
teebblog
提交
2c0b5f97
T
teebblog
项目概览
teebbstudios
/
teebblog
通知
0
Star
5
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
teebblog
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
2c0b5f97
编写于
8月 02, 2021
作者:
伟
伟伟权
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
117. PostAwareNromalizer
上级
df2c3493
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
94 addition
and
15 deletion
+94
-15
src/Entity/Post.php
src/Entity/Post.php
+21
-0
src/Serializer/Normalizer/PostAwareNormalizer.php
src/Serializer/Normalizer/PostAwareNormalizer.php
+58
-0
src/Serializer/Normalizer/PostNormalizer.php
src/Serializer/Normalizer/PostNormalizer.php
+15
-15
未找到文件。
src/Entity/Post.php
浏览文件 @
2c0b5f97
...
...
@@ -98,6 +98,9 @@ class Post
// #[ApiProperty(readableLink: true, writableLink: true)]
private
$author
;
#[Groups(['post:read', 'post:item:get'])]
private
$postImageUrl
;
public
function
__construct
()
{
$this
->
comments
=
new
ArrayCollection
();
...
...
@@ -245,4 +248,22 @@ class Post
return
$this
;
}
/**
* @return mixed
*/
public
function
getPostImageUrl
()
{
return
$this
->
postImageUrl
;
}
/**
* @param mixed $postImageUrl
*/
public
function
setPostImageUrl
(
$postImageUrl
):
void
{
$this
->
postImageUrl
=
$postImageUrl
;
}
}
src/Serializer/Normalizer/PostAwareNormalizer.php
0 → 100644
浏览文件 @
2c0b5f97
<?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
src/Serializer/Normalizer/PostNormalizer.php
浏览文件 @
2c0b5f97
...
...
@@ -17,7 +17,7 @@ use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use
Symfony\Component\Serializer\Normalizer\NormalizerInterface
;
use
Symfony\Component\Serializer\Normalizer\ObjectNormalizer
;
class
PostNormalizer
implements
NormalizerInterface
,
DenormalizerInterface
,
CacheableSupportsMethodInterface
class
PostNormalizer
implements
DenormalizerInterface
,
CacheableSupportsMethodInterface
{
private
$normalizer
;
/**
...
...
@@ -36,20 +36,20 @@ class PostNormalizer implements NormalizerInterface, DenormalizerInterface, Cach
$this
->
userRepository
=
$userRepository
;
}
public
function
normalize
(
$object
,
$format
=
null
,
array
$context
=
[]):
array
{
$data
=
$this
->
normalizer
->
normalize
(
$object
,
$format
,
$context
);
$request
=
$this
->
requestStack
->
getCurrentRequest
();
// Here: add, edit, or delete some data
$data
[
'post_image_url'
]
=
$request
->
getSchemeAndHttpHost
()
.
'/uploads/images/'
.
$data
[
'postImage'
];
return
$data
;
}
public
function
supportsNormalization
(
$data
,
$format
=
null
):
bool
{
return
$data
instanceof
\
App\Entity\Post
;
}
//
public function normalize($object, $format = null, array $context = []): array
//
{
//
$data = $this->normalizer->normalize($object, $format, $context);
//
//
$request = $this->requestStack->getCurrentRequest();
//
// Here: add, edit, or delete some data
//
$data['post_image_url'] = $request->getSchemeAndHttpHost() .'/uploads/images/'. $data['postImage'];
//
return $data;
//
}
//
//
public function supportsNormalization($data, $format = null): bool
//
{
//
return $data instanceof \App\Entity\Post;
//
}
public
function
hasCacheableSupportsMethod
():
bool
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录