Skip to content
Newer
Older
100644 178 lines (155 sloc) 4.68 KB
1beaee9 @borisguery * Added Doctrine 2 EntitySerializer
authored Aug 13, 2011
1 <?php
2 /**
3 * Bgy Library
4 *
5 * LICENSE
6 *
7 * This program is free software. It comes without any warranty, to
8 * the extent permitted by applicable law. You can redistribute it
9 * and/or modify it under the terms of the Do What The Fuck You Want
10 * To Public License, Version 2, as published by Sam Hocevar. See
11 * http://sam.zoy.org/wtfpl/COPYING for more details.
12 *
13 * @category Bgy
14 * @package Bgy\Doctrine
15 * @author Boris Guéry <guery.b@gmail.com>
16 * @license http://sam.zoy.org/wtfpl/COPYING
17 * @link http://borisguery.github.com/bgylibrary
243116e @borisguery * Added references
authored Aug 16, 2011
18 * @see https://gist.github.com/1034079#file_serializable_entity.php
1beaee9 @borisguery * Added Doctrine 2 EntitySerializer
authored Aug 13, 2011
19 */
20
21 namespace Bgy\Doctrine;
22
23 use Doctrine\ORM\Mapping\ClassMetadata,
24 Doctrine\Common\Util\Inflector,
25 Doctrine\ORM\EntityManager,
26 Exception;
27
28 class EntitySerializer
29 {
30
31 /**
32 * @var Doctrine\ORM\EntityManager
33 */
34 protected $_em;
35
8032374 @wildlyinaccurate Add recursion to EntitySerializer
wildlyinaccurate authored Jun 16, 2012
36 /**
37 * @var int
38 */
39 protected $_recursionDepth = 0;
40
41 /**
42 * @var int
43 */
44 protected $_maxRecursionDepth = 0;
45
1beaee9 @borisguery * Added Doctrine 2 EntitySerializer
authored Aug 13, 2011
46 public function __construct($em)
47 {
48 $this->setEntityManager($em);
49 }
50
51 /**
52 *
53 * @return Doctrine\ORM\EntityManager
54 */
55 public function getEntityManager()
56 {
57 return $this->_em;
58 }
59
60 public function setEntityManager(EntityManager $em)
61 {
62 $this->_em = $em;
63
64 return $this;
65 }
66
67 protected function _serializeEntity($entity)
68 {
69 $className = get_class($entity);
70 $metadata = $this->_em->getClassMetadata($className);
71
72 $data = array();
73
74 foreach ($metadata->fieldMappings as $field => $mapping) {
75 $value = $metadata->reflFields[$field]->getValue($entity);
76 $field = Inflector::tableize($field);
77 if ($value instanceof \DateTime) {
78 // We cast DateTime to array to keep consistency with array result
79 $data[$field] = (array)$value;
80 } elseif (is_object($value)) {
81 $data[$field] = (string)$value;
82 } else {
83 $data[$field] = $value;
84 }
85 }
86
87 foreach ($metadata->associationMappings as $field => $mapping) {
88 $key = Inflector::tableize($field);
89 if ($mapping['isCascadeDetach']) {
75f484d @borisguery * Checked for null relationship on isCascadeDetach
authored Oct 20, 2011
90 $data[$key] = $metadata->reflFields[$field]->getValue($entity);
91 if (null !== $data[$key]) {
92 $data[$key] = $this->_serializeEntity($data[$key]);
93 }
1beaee9 @borisguery * Added Doctrine 2 EntitySerializer
authored Aug 13, 2011
94 } elseif ($mapping['isOwningSide'] && $mapping['type'] & ClassMetadata::TO_ONE) {
95 if (null !== $metadata->reflFields[$field]->getValue($entity)) {
8032374 @wildlyinaccurate Add recursion to EntitySerializer
wildlyinaccurate authored Jun 16, 2012
96 if ($this->_recursionDepth < $this->_maxRecursionDepth) {
97 $this->_recursionDepth++;
98 $data[$key] = $this->_serializeEntity(
1beaee9 @borisguery * Added Doctrine 2 EntitySerializer
authored Aug 13, 2011
99 $metadata->reflFields[$field]
100 ->getValue($entity)
101 );
8032374 @wildlyinaccurate Add recursion to EntitySerializer
wildlyinaccurate authored Jun 16, 2012
102 $this->_recursionDepth--;
103 } else {
104 $data[$key] = $this->getEntityManager()
105 ->getUnitOfWork()
106 ->getEntityIdentifier(
107 $metadata->reflFields[$field]
108 ->getValue($entity)
109 );
110 }
1beaee9 @borisguery * Added Doctrine 2 EntitySerializer
authored Aug 13, 2011
111 } else {
112 // In some case the relationship may not exist, but we want
113 // to know about it
114 $data[$key] = null;
115 }
116 }
117 }
118
119 return $data;
120 }
121
122 /**
123 * Serialize an entity to an array
124 *
125 * @param The entity $entity
126 * @return array
127 */
128 public function toArray($entity)
129 {
fee6074 @borisguery * Fixed method name
authored Aug 14, 2011
130 return $this->_serializeEntity($entity);
1beaee9 @borisguery * Added Doctrine 2 EntitySerializer
authored Aug 13, 2011
131 }
132
133
134 /**
135 * Convert an entity to a JSON object
136 *
137 * @param The entity $entity
138 * @return string
139 */
140 public function toJson($entity)
141 {
142 return json_encode($this->toArray($entity));
143 }
144
145 /**
146 * Convert an entity to XML representation
147 *
148 * @param The entity $entity
149 * @throws Exception
150 */
151 public function toXml($entity)
152 {
153 throw new Exception('Not yet implemented');
154 }
155
8032374 @wildlyinaccurate Add recursion to EntitySerializer
wildlyinaccurate authored Jun 16, 2012
156 /**
157 * Set the maximum recursion depth
158 *
159 * @param int $maxRecursionDepth
160 * @return void
161 */
162 public function setMaxRecursionDepth($maxRecursionDepth)
163 {
164 $this->_maxRecursionDepth = $maxRecursionDepth;
165 }
166
167 /**
168 * Get the maximum recursion depth
169 *
170 * @return int
171 */
172 public function getMaxRecursionDepth()
173 {
174 return $this->_maxRecursionDepth;
175 }
176
177 }
Something went wrong with that request. Please try again.