Skip to content

Commit 2d67339

Browse files
committed
When checking for a list, if the value is an object that isn't traversable then the function returns false, as it will not be possible for the object to contain integer keys.
1 parent 188a13c commit 2d67339

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/JsonStream/Encoder.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,20 @@ private function _encodeString($string)
108108
*/
109109
private function _isList($value)
110110
{
111-
for($i = 0, reset($value); $i < count($value); $i++, next($value)) {
112-
if(key($value) !== $i) {
111+
// objects that are not explicitly traversable could never have integer keys, therefore they are not a list
112+
if (is_object($value) && !($value instanceof \Traversable)) {
113+
return false;
114+
}
115+
116+
// check if the array/object has only integer keys.
117+
$i = 0;
118+
foreach ($value as $key => $element) {
119+
if ($key !== $i) {
113120
return false;
114121
}
122+
$i++;
115123
}
124+
116125
return true;
117126
}
118127

0 commit comments

Comments
 (0)