|
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\Structure; |
|
13
|
|
|
|
|
14
|
|
|
use ProxyManager\Factory\LazyLoadingValueHolderFactory; |
|
15
|
|
|
use ProxyManager\Proxy\LazyLoadingInterface; |
|
16
|
|
|
use Sulu\Component\Content\Compat\StructureInterface; |
|
17
|
|
|
use Sulu\Component\Content\ContentTypeManagerInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Factory for content-proxies. |
|
21
|
|
|
*/ |
|
22
|
|
|
class ContentProxyFactory |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var ContentTypeManagerInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
private $contentTypeManager; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var LazyLoadingValueHolderFactory |
|
31
|
|
|
*/ |
|
32
|
|
|
private $proxyFactory; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param ContentTypeManagerInterface $contentTypeManager |
|
36
|
|
|
* @param LazyLoadingValueHolderFactory $proxyFactory |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct( |
|
39
|
|
|
ContentTypeManagerInterface $contentTypeManager, |
|
40
|
|
|
LazyLoadingValueHolderFactory $proxyFactory |
|
41
|
|
|
) { |
|
42
|
|
|
$this->contentTypeManager = $contentTypeManager; |
|
43
|
|
|
$this->proxyFactory = $proxyFactory; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Create content-proxy for given structure. |
|
48
|
|
|
* |
|
49
|
|
|
* @param StructureInterface $structure |
|
50
|
|
|
* @param array $data |
|
51
|
|
|
* |
|
52
|
|
|
* @return array |
|
53
|
|
|
*/ |
|
54
|
|
View Code Duplication |
public function createContentProxy(StructureInterface $structure, array $data) |
|
|
|
|
|
|
55
|
|
|
{ |
|
56
|
|
|
return $this->proxyFactory->createProxy( |
|
57
|
|
|
\ArrayObject::class, |
|
58
|
|
|
function ( |
|
59
|
|
|
&$wrappedObject, |
|
60
|
|
|
LazyLoadingInterface $proxy, |
|
61
|
|
|
$method, |
|
62
|
|
|
array $parameters, |
|
63
|
|
|
&$initializer |
|
64
|
|
|
) use ($structure, $data) { |
|
65
|
|
|
$initializer = null; |
|
66
|
|
|
$wrappedObject = new \ArrayObject($this->resolveContent($structure, $data)); |
|
67
|
|
|
|
|
68
|
|
|
return true; |
|
69
|
|
|
} |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Resolve content from given data with the structure. |
|
75
|
|
|
* |
|
76
|
|
|
* @param StructureInterface $structure |
|
77
|
|
|
* @param array $data |
|
78
|
|
|
* |
|
79
|
|
|
* @return array |
|
80
|
|
|
*/ |
|
81
|
|
View Code Duplication |
private function resolveContent(StructureInterface $structure, array $data) |
|
|
|
|
|
|
82
|
|
|
{ |
|
83
|
|
|
$content = []; |
|
84
|
|
|
foreach ($structure->getProperties(true) as $child) { |
|
85
|
|
|
if (array_key_exists($child->getName(), $data)) { |
|
86
|
|
|
$child->setValue($data[$child->getName()]); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$contentType = $this->contentTypeManager->get($child->getContentTypeName()); |
|
90
|
|
|
$content[$child->getName()] = $contentType->getContentData($child); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $content; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Create view-proxy for given structure. |
|
98
|
|
|
* |
|
99
|
|
|
* @param StructureInterface $structure |
|
100
|
|
|
* @param array $data |
|
101
|
|
|
* |
|
102
|
|
|
* @return array |
|
103
|
|
|
*/ |
|
104
|
|
View Code Duplication |
public function createViewProxy(StructureInterface $structure, array $data) |
|
|
|
|
|
|
105
|
|
|
{ |
|
106
|
|
|
return $this->proxyFactory->createProxy( |
|
107
|
|
|
\ArrayObject::class, |
|
108
|
|
|
function ( |
|
109
|
|
|
&$wrappedObject, |
|
110
|
|
|
LazyLoadingInterface $proxy, |
|
111
|
|
|
$method, |
|
112
|
|
|
array $parameters, |
|
113
|
|
|
&$initializer |
|
114
|
|
|
) use ($structure, $data) { |
|
115
|
|
|
$initializer = null; |
|
116
|
|
|
$wrappedObject = new \ArrayObject($this->resolveView($structure, $data)); |
|
117
|
|
|
|
|
118
|
|
|
return true; |
|
119
|
|
|
} |
|
120
|
|
|
); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Resolve view from given data with the structure. |
|
125
|
|
|
* |
|
126
|
|
|
* @param StructureInterface $structure |
|
127
|
|
|
* @param array $data |
|
128
|
|
|
* |
|
129
|
|
|
* @return array |
|
130
|
|
|
*/ |
|
131
|
|
View Code Duplication |
private function resolveView(StructureInterface $structure, array $data) |
|
|
|
|
|
|
132
|
|
|
{ |
|
133
|
|
|
$view = []; |
|
134
|
|
|
foreach ($structure->getProperties(true) as $child) { |
|
135
|
|
|
if (array_key_exists($child->getName(), $data)) { |
|
136
|
|
|
$child->setValue($data[$child->getName()]); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
$contentType = $this->contentTypeManager->get($child->getContentTypeName()); |
|
140
|
|
|
$view[$child->getName()] = $contentType->getViewData($child); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
return $view; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.