This repository has been archived by the owner on Oct 16, 2019. It is now read-only.
/
EntitySerializer.php
177 lines (155 loc) · 4.68 KB
/
EntitySerializer.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
171
172
173
174
175
176
177
<?php
/**
* Bgy Library
*
* LICENSE
*
* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details.
*
* @category Bgy
* @package Bgy\Doctrine
* @author Boris Guéry <guery.b@gmail.com>
* @license http://sam.zoy.org/wtfpl/COPYING
* @link http://borisguery.github.com/bgylibrary
* @see https://gist.github.com/1034079#file_serializable_entity.php
*/
namespace Bgy\Doctrine;
use Doctrine\ORM\Mapping\ClassMetadata,
Doctrine\Common\Util\Inflector,
Doctrine\ORM\EntityManager,
Exception;
class EntitySerializer
{
/**
* @var Doctrine\ORM\EntityManager
*/
protected $_em;
/**
* @var int
*/
protected $_recursionDepth = 0;
/**
* @var int
*/
protected $_maxRecursionDepth = 0;
public function __construct($em)
{
$this->setEntityManager($em);
}
/**
*
* @return Doctrine\ORM\EntityManager
*/
public function getEntityManager()
{
return $this->_em;
}
public function setEntityManager(EntityManager $em)
{
$this->_em = $em;
return $this;
}
protected function _serializeEntity($entity)
{
$className = get_class($entity);
$metadata = $this->_em->getClassMetadata($className);
$data = array();
foreach ($metadata->fieldMappings as $field => $mapping) {
$value = $metadata->reflFields[$field]->getValue($entity);
$field = Inflector::tableize($field);
if ($value instanceof \DateTime) {
// We cast DateTime to array to keep consistency with array result
$data[$field] = (array)$value;
} elseif (is_object($value)) {
$data[$field] = (string)$value;
} else {
$data[$field] = $value;
}
}
foreach ($metadata->associationMappings as $field => $mapping) {
$key = Inflector::tableize($field);
if ($mapping['isCascadeDetach']) {
$data[$key] = $metadata->reflFields[$field]->getValue($entity);
if (null !== $data[$key]) {
$data[$key] = $this->_serializeEntity($data[$key]);
}
} elseif ($mapping['isOwningSide'] && $mapping['type'] & ClassMetadata::TO_ONE) {
if (null !== $metadata->reflFields[$field]->getValue($entity)) {
if ($this->_recursionDepth < $this->_maxRecursionDepth) {
$this->_recursionDepth++;
$data[$key] = $this->_serializeEntity(
$metadata->reflFields[$field]
->getValue($entity)
);
$this->_recursionDepth--;
} else {
$data[$key] = $this->getEntityManager()
->getUnitOfWork()
->getEntityIdentifier(
$metadata->reflFields[$field]
->getValue($entity)
);
}
} else {
// In some case the relationship may not exist, but we want
// to know about it
$data[$key] = null;
}
}
}
return $data;
}
/**
* Serialize an entity to an array
*
* @param The entity $entity
* @return array
*/
public function toArray($entity)
{
return $this->_serializeEntity($entity);
}
/**
* Convert an entity to a JSON object
*
* @param The entity $entity
* @return string
*/
public function toJson($entity)
{
return json_encode($this->toArray($entity));
}
/**
* Convert an entity to XML representation
*
* @param The entity $entity
* @throws Exception
*/
public function toXml($entity)
{
throw new Exception('Not yet implemented');
}
/**
* Set the maximum recursion depth
*
* @param int $maxRecursionDepth
* @return void
*/
public function setMaxRecursionDepth($maxRecursionDepth)
{
$this->_maxRecursionDepth = $maxRecursionDepth;
}
/**
* Get the maximum recursion depth
*
* @return int
*/
public function getMaxRecursionDepth()
{
return $this->_maxRecursionDepth;
}
}