|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Sulu. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) MASSIVE ART WebServices GmbH |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sulu\Bundle\ArticleBundle\Document\Serializer; |
|
13
|
|
|
|
|
14
|
|
|
use JMS\Serializer\EventDispatcher\Events; |
|
15
|
|
|
use JMS\Serializer\EventDispatcher\EventSubscriberInterface; |
|
16
|
|
|
use JMS\Serializer\EventDispatcher\ObjectEvent; |
|
17
|
|
|
use Sulu\Bundle\ArticleBundle\Document\ArticleDocument; |
|
18
|
|
|
use Sulu\Bundle\ArticleBundle\Metadata\ArticleTypeTrait; |
|
19
|
|
|
use Sulu\Component\Content\Compat\StructureManagerInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Extends serialization for articles. |
|
23
|
|
|
*/ |
|
24
|
|
|
class ArticleSubscriber implements EventSubscriberInterface |
|
25
|
|
|
{ |
|
26
|
|
|
use ArticleTypeTrait; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var StructureManagerInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
private $structureManager; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param StructureManagerInterface $structureManager |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct(StructureManagerInterface $structureManager) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->structureManager = $structureManager; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* {@inheritdoc} |
|
43
|
|
|
*/ |
|
44
|
|
|
public static function getSubscribedEvents() |
|
45
|
|
|
{ |
|
46
|
|
|
return [ |
|
47
|
|
|
[ |
|
48
|
|
|
'event' => Events::POST_SERIALIZE, |
|
49
|
|
|
'format' => 'json', |
|
50
|
|
|
'method' => 'addTypeOnPostSerialize', |
|
51
|
30 |
|
], |
|
52
|
|
|
]; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
30 |
|
* Append type to result. |
|
57
|
30 |
|
* |
|
58
|
30 |
|
* @param ObjectEvent $event |
|
59
|
30 |
|
*/ |
|
60
|
|
|
public function addTypeOnPostSerialize(ObjectEvent $event) |
|
61
|
|
|
{ |
|
62
|
|
|
$article = $event->getObject(); |
|
63
|
|
|
$visitor = $event->getVisitor(); |
|
64
|
1 |
|
$context = $event->getContext(); |
|
65
|
|
|
|
|
66
|
|
|
if (!($article instanceof ArticleDocument)) { |
|
67
|
|
|
return; |
|
68
|
1 |
|
} |
|
69
|
1 |
|
|
|
70
|
1 |
|
$structure = $this->structureManager->getStructure($article->getStructureType(), 'article'); |
|
71
|
|
|
$visitor->addData('articleType', $context->accept($this->getType($structure->getStructure()))); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|