-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerializer.php
More file actions
237 lines (208 loc) · 7 KB
/
Serializer.php
File metadata and controls
237 lines (208 loc) · 7 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
namespace Tactics\Bundle\AdminBundle;
use Symfony\Component\DependencyInjection\Container;
/**
* Description of Serializer
*
* @author Jeroen
*/
class Serializer
{
protected
$container // the service container
;
/**
* the constructor
*
* @param \Symfony\Component\DependencyInjection\Container $container
*/
public function __construct(Container $container)
{
$this->container = $container;
}
/**
* serializes the given value
*
* @param mixed $value
* @return string
*/
public function serialize($value, $depthOfAssociations = 0)
{
if(is_array($value)) {
$valueToSerialize = $this->serializeArray($value, $depthOfAssociations);
}
//check if get Id exists so that we pass an entity
elseif (is_object($value) && method_exists($value, 'getId'))
{
$valueToSerialize = $this->serializeObject($value, $depthOfAssociations);
}
else {
$valueToSerialize = $value;
}
return serialize($valueToSerialize);
}
/**
* return unserialized value
*
* @param mixed $value
* @return mixed
*/
public function unserialize($value)
{
$deserializedValue = unserialize($value);
if(is_array($deserializedValue)) {
$deserializedValue = $this->deserializeArray($deserializedValue);
}
return $deserializedValue;
}
/**
* Serializes an Entity
*
* @param Entity $object
* @param int $depthOfAssociations
* @return array
*/
public function serializeObject($object, $depthOfAssociations)
{
$serializer = $this->container->get('serializer');
return array('Object' => array('namespace' => get_class($object), 'entity' => $serializer->serialize($this->getSerializableEntity($object, $depthOfAssociations),'xml')));
}
/**
* Serializes an array
*
* @param array $array
* @param int $depthOfAssociations
* @return array
*/
public function serializeArray($array, $depthOfAssociations)
{
foreach ($array as $key => $element) {
if (is_array($element)) {
$element = $this->serializeArray($element,$depthOfAssociations);
}
elseif (is_object($element) && method_exists($element, 'getId')) {
$element = $this->serializeObject($element, $depthOfAssociations);
}
$array[$key] = $element;
}
return $array;
}
/**
* Deserializes an array
*
* @param array $array
* @return array
*/
public function deserializeArray($array)
{
foreach ($array as $arrayKey => $element) {
if (is_array($element)) {
foreach ($element as $key => $value) {
if($key === 'Object') {
$element = $this->deserializeObject($element[$key]);
}
elseif(is_array($value)) {
$element = $this->deserializeArray($array[$arrayKey]);
}
}
}
$array[$arrayKey] = $element;
}
return $array;
}
/**
* Deserializes an objectarray to an Entity
*
* @param array $objectArray
* @return Entity
*/
public function deserializeObject($objectArray)
{
$serializer = $this->container->get('serializer');
return $serializer->deserialize($objectArray['entity'], $objectArray['namespace'], 'xml');
}
/**
* Guesses a getter or setter method for a property of an Entity
*
* @param Entity $object
* @param Property $property
* @param string $getOrSet
* @return string|null
*/
public function guessMethod($object, $property, $getOrSet)
{
$tmp = explode('_', $property);
$str = '';
$i = 0;
while ($i < count($tmp))
{
$str .= ucfirst($tmp[$i]);
$i++;
}
$getter = $getOrSet.$str;
if (method_exists($object, $getter)) {
return $getter;
}
else {
return null;
}
}
/**
* Returns a Serializable entity
*
* @param Entity $object
* @param int $depthOfAssociations
* @return null|\Tactics\Bundle\AdminBundle\namespace
*/
public function getSerializableEntity($object, $depthOfAssociations)
{
if(! ($depthOfAssociations >= 0)) {
return null;
}
$reflectionObject = new \ReflectionObject($object);
$properties = $reflectionObject->getProperties();
$namespace = get_class($object);
//if its proxyclass get the real classname (Todo : this is a very ugly way to do this)
if(strrpos($namespace, 'roxies')) {
$namespace = substr($namespace, 15);
}
//cant construct DateTime or DateTimeZone, constructor needs a parameter
if(strrpos($namespace, 'ateTime'))
{
return null;
}
$returnObject = new $namespace();
foreach($properties as $property) {
$property = $property->getName();
$getMethod = $this->guessMethod($object, $property,'get');
if($getMethod && $object->$getMethod()) {
$propertyValue = $object->$getMethod();
//Property is collection/array
if(is_array($propertyValue) || method_exists($propertyValue, 'toArray')) {
$idArray = array();
foreach($propertyValue as $propvalue) {
//if it's an object save its Id
if(is_object($propvalue) && method_exists($propvalue, 'getId')) {
$propertyValue = $depthOfAssociations > 0 ? $this->getSerializableEntity($propvalue, $depthOfAssociations-1) : null;
}
//if not an object save value itself
else {
$idArray[] = $propvalue;
}
}
$propertyValue = $idArray;
}
//Property is association to other entity
elseif (is_object($propertyValue)) {
$propertyValue = $depthOfAssociations > 0 ? $this->getSerializableEntity($propertyValue, $depthOfAssociations-1) : null;
}
//If a setter for this property exists, set its value
$setter = $this->guessMethod($object, $property, 'set');
if(method_exists($object, $setter) && $propertyValue !== null) {
$returnObject->$setter($propertyValue);
}
}
}
return $returnObject;
}
}