@@ -44,20 +44,25 @@ class PHPEncoder
44
44
public function __construct (array $ options = [], array $ encoders = null )
45
45
{
46
46
$ this ->options = self ::$ defaultOptions ;
47
- $ this ->encoders = $ encoders !== null ? [] : [
48
- new Encoder \NullEncoder (),
49
- new Encoder \BooleanEncoder (),
50
- new Encoder \IntegerEncoder (),
51
- new Encoder \FloatEncoder (),
52
- new Encoder \StringEncoder (),
53
- new Encoder \ArrayEncoder (),
54
- new Encoder \GMPEncoder (),
55
- new Encoder \ObjectEncoder (),
56
- ];
57
-
58
- foreach ($ encoders ?: [] as $ encoder ) {
59
- $ this ->addEncoder ($ encoder );
47
+ $ this ->encoders = [];
48
+
49
+ if ($ encoders === null ) {
50
+ $ this ->encoders = [
51
+ new Encoder \NullEncoder (),
52
+ new Encoder \BooleanEncoder (),
53
+ new Encoder \IntegerEncoder (),
54
+ new Encoder \FloatEncoder (),
55
+ new Encoder \StringEncoder (),
56
+ new Encoder \ArrayEncoder (),
57
+ new Encoder \GMPEncoder (),
58
+ new Encoder \ObjectEncoder (),
59
+ ];
60
+ } else {
61
+ foreach ($ encoders as $ encoder ) {
62
+ $ this ->addEncoder ($ encoder );
63
+ }
60
64
}
65
+
61
66
foreach ($ options as $ option => $ value ) {
62
67
$ this ->setOption ($ option , $ value );
63
68
}
@@ -160,17 +165,9 @@ public function getAllOptions(array $overrides = [])
160
165
* @return string The PHP code that represents the given value
161
166
* @throws \RuntimeException On recursion or upon reaching maximum depth
162
167
*/
163
- private function generate ($ value , $ depth , array $ options , $ recursion = [])
168
+ private function generate ($ value , $ depth , array $ options , array $ recursion = [])
164
169
{
165
- if ($ options ['recursion.detect ' ]) {
166
- if (array_search ($ value , $ recursion , true ) !== false ) {
167
- if ($ options ['recursion.ignore ' ]) {
168
- $ value = null ;
169
- } else {
170
- throw new \RuntimeException ('Recursion detected ' );
171
- }
172
- }
173
-
170
+ if ($ this ->detectRecursion ($ value , $ options , $ recursion )) {
174
171
$ recursion [] = $ value ;
175
172
}
176
173
@@ -185,6 +182,30 @@ private function generate($value, $depth, array $options, $recursion = [])
185
182
return $ this ->encodeValue ($ value , $ depth , $ options , $ callback );
186
183
}
187
184
185
+ /**
186
+ * Attempts to detect circular references in values.
187
+ * @param mixed $value Value to try for circular reference
188
+ * @param array $options List of encoder options
189
+ * @param array $recursion Upper values in the encoding tree
190
+ * @return boolean True if values should be recorded, false if not
191
+ */
192
+ private function detectRecursion (& $ value , array $ options , array $ recursion )
193
+ {
194
+ if ($ options ['recursion.detect ' ]) {
195
+ if (array_search ($ value , $ recursion , true ) !== false ) {
196
+ if ($ options ['recursion.ignore ' ]) {
197
+ $ value = null ;
198
+ } else {
199
+ throw new \RuntimeException ('Recursion detected ' );
200
+ }
201
+ }
202
+
203
+ return true ;
204
+ }
205
+
206
+ return false ;
207
+ }
208
+
188
209
/**
189
210
* Encodes the value using one of the encoders that supports the value type.
190
211
* @param mixed $value Value to encode
0 commit comments