11
11
12
12
namespace Symfony \Component \Serializer \Normalizer ;
13
13
14
- use Symfony \Component \Serializer \Exception \CircularReferenceException ;
15
14
use Symfony \Component \Serializer \Exception \MissingConstructorArgumentsException ;
16
15
use Symfony \Component \Serializer \Exception \InvalidArgumentException ;
17
16
use Symfony \Component \Serializer \Exception \LogicException ;
29
28
*/
30
29
abstract class AbstractNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface
31
30
{
31
+ use CircularReferenceTrait;
32
32
use ObjectToPopulateTrait;
33
33
use SerializerAwareTrait;
34
34
@@ -39,16 +39,6 @@ abstract class AbstractNormalizer implements NormalizerInterface, DenormalizerIn
39
39
const ALLOW_EXTRA_ATTRIBUTES = 'allow_extra_attributes ' ;
40
40
const DEFAULT_CONSTRUCTOR_ARGUMENTS = 'default_constructor_arguments ' ;
41
41
42
- /**
43
- * @var int
44
- */
45
- protected $ circularReferenceLimit = 1 ;
46
-
47
- /**
48
- * @var callable
49
- */
50
- protected $ circularReferenceHandler ;
51
-
52
42
/**
53
43
* @var ClassMetadataFactoryInterface|null
54
44
*/
@@ -83,34 +73,6 @@ public function __construct(ClassMetadataFactoryInterface $classMetadataFactory
83
73
$ this ->nameConverter = $ nameConverter ;
84
74
}
85
75
86
- /**
87
- * Set circular reference limit.
88
- *
89
- * @param int $circularReferenceLimit Limit of iterations for the same object
90
- *
91
- * @return self
92
- */
93
- public function setCircularReferenceLimit ($ circularReferenceLimit )
94
- {
95
- $ this ->circularReferenceLimit = $ circularReferenceLimit ;
96
-
97
- return $ this ;
98
- }
99
-
100
- /**
101
- * Set circular reference handler.
102
- *
103
- * @param callable $circularReferenceHandler
104
- *
105
- * @return self
106
- */
107
- public function setCircularReferenceHandler (callable $ circularReferenceHandler )
108
- {
109
- $ this ->circularReferenceHandler = $ circularReferenceHandler ;
110
-
111
- return $ this ;
112
- }
113
-
114
76
/**
115
77
* Set normalization callbacks.
116
78
*
@@ -147,56 +109,6 @@ public function setIgnoredAttributes(array $ignoredAttributes)
147
109
return $ this ;
148
110
}
149
111
150
- /**
151
- * Detects if the configured circular reference limit is reached.
152
- *
153
- * @param object $object
154
- * @param array $context
155
- *
156
- * @return bool
157
- *
158
- * @throws CircularReferenceException
159
- */
160
- protected function isCircularReference ($ object , &$ context )
161
- {
162
- $ objectHash = spl_object_hash ($ object );
163
-
164
- if (isset ($ context [static ::CIRCULAR_REFERENCE_LIMIT ][$ objectHash ])) {
165
- if ($ context [static ::CIRCULAR_REFERENCE_LIMIT ][$ objectHash ] >= $ this ->circularReferenceLimit ) {
166
- unset($ context [static ::CIRCULAR_REFERENCE_LIMIT ][$ objectHash ]);
167
-
168
- return true ;
169
- }
170
-
171
- ++$ context [static ::CIRCULAR_REFERENCE_LIMIT ][$ objectHash ];
172
- } else {
173
- $ context [static ::CIRCULAR_REFERENCE_LIMIT ][$ objectHash ] = 1 ;
174
- }
175
-
176
- return false ;
177
- }
178
-
179
- /**
180
- * Handles a circular reference.
181
- *
182
- * If a circular reference handler is set, it will be called. Otherwise, a
183
- * {@class CircularReferenceException} will be thrown.
184
- *
185
- * @param object $object
186
- *
187
- * @return mixed
188
- *
189
- * @throws CircularReferenceException
190
- */
191
- protected function handleCircularReference ($ object )
192
- {
193
- if ($ this ->circularReferenceHandler ) {
194
- return \call_user_func ($ this ->circularReferenceHandler , $ object );
195
- }
196
-
197
- throw new CircularReferenceException (sprintf ('A circular reference has been detected when serializing the object of class "%s" (configured limit: %d) ' , \get_class ($ object ), $ this ->circularReferenceLimit ));
198
- }
199
-
200
112
/**
201
113
* Gets attributes to normalize using groups.
202
114
*
@@ -404,4 +316,9 @@ protected function createChildContext(array $parentContext, $attribute)
404
316
405
317
return $ parentContext ;
406
318
}
319
+
320
+ private function getCircularReferenceLimitField ()
321
+ {
322
+ return static ::CIRCULAR_REFERENCE_LIMIT ;
323
+ }
407
324
}
0 commit comments