-
Notifications
You must be signed in to change notification settings - Fork 2
/
CommentNormalizer.php
171 lines (142 loc) · 4.86 KB
/
CommentNormalizer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
namespace Progracqteur\WikipedaleBundle\Resources\Normalizer;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Progracqteur\WikipedaleBundle\Entity\Model\Comment;
use Progracqteur\WikipedaleBundle\Resources\Normalizer\NormalizerSerializerService;
use Progracqteur\WikipedaleBundle\Resources\Normalizer\NormalizingException;
use Progracqteur\WikipedaleBundle\Resources\Normalizer\UserNormalizer;
use Progracqteur\WikipedaleBundle\Resources\Normalizer\PlaceNormalizer;
/**
* Description of PlaceNormalizer
*
* @author julien [at] fastre [point] info & marcducobu [at] gmail [point] com
*/
class CommentNormalizer implements NormalizerInterface, DenormalizerInterface {
/**
*
* @var Progracqteur\WikipedaleBundle\Resources\Normalizer\NormalizerSerializerService
*/
private $service;
/**
* Comment being denormalized
* (useful for recursive denormalization)
* @var Progracqteur\WikipedaleBundle\Entity\Model\Comment
*/
private $currentComment;
public function __construct(NormalizerSerializerService $service)
{
$this->service = $service;
}
public function denormalize($data, $class, $format = null, array $context = array()) {
//TODO à adapter lorsque le json envoyé sera corrigé
/*if ($data['id'] === null)
{*/
$p = new Comment();
/*}
else
{
$p = $this->service->getManager()
->getRepository('ProgracqteurWikipedaleBundle:Model\\Comment')
->find($data['id']);
if ($p === null)
{
throw new \Exception("Le commentaire recherché n'existe pas");
}
}*/
if (isset($data['placeId']))
{
$place = $this->service->getManager()
->getRepository('ProgracqteurWikipedaleBundle:Model\\Place')
->find($data['placeId']);
if ($place === null)
{
throw new \Exception("place with id ".$data['placeId']." not found");
}
$p->setPlace($place);
}
if (isset($data['text']))
$p->setContent($data['text']);
if (isset($data['creator']))
{
$userNormalizer = $this->service->getUserNormalizer();
if ($userNormalizer->supportsDenormalization($data['creator'],
$class,
$format))
{
$u = $userNormalizer->denormalize($data['creator'],
$class,
$format);
$p->setCreator($u);
}
}
if (isset($data['published']))
{
$p->setPublished($data['published']);
}
if (isset($data['type']))
{
switch($data['type'])
{
case 'moderator_manager' :
$p->setType(Comment::TYPE_MODERATOR_MANAGER);
break;
default :
$p->setType(Comment::TYPE_PUBLIC);
break;
}
}
return $p;
}
/**
*
* @param \Progracqteur\WikipedaleBundle\Entity\Model\Comment $object
* @param string $format
* @return array
*/
public function normalize($object, $format = null, array $context = array()) {
return array(
'entity' => 'comment',
'id' => $object->getId(),
'text' => $object->getContent(),
'published' => $object->getPublished(),
'creationDate' => $this->service->getDateNormalizer()->normalize($object->getCreationDate(), $format),
'createDate' => $this->service->getDateNormalizer()->normalize($object->getCreationDate(), $format),
'creator' => $this->service->getUserNormalizer()->normalize($object->getCreator(), $format),
//'place' => $this->service->getPlaceNormalizer()->normalize($object->getPlace(), $format),
'placeId' => $object->getPlace()->getId(),
'type' => $object->getType()
);
}
public function supportsNormalization($data, $format = null) {
if ($data instanceof Comment)
{
return true;
} else
{
return false;
}
}
public function supportsDenormalization($data, $type, $format = null)
{
if ($data['entity'] === 'comment')
{
return true; //true;
} else
{
return false;
}
}
private function setCurrentComment(Comment $comment)
{
$this->currentComment = $comment;
}
/*
*
* @return Progracqteur\WikipedaleBundle\Entity\Model\Comment
*/
public function getCurrentComment()
{
return $this->currentComment;
}
}