From a62ee5ba49d0fcfbd4c42d5d94516d4e4ecce2e7 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Sun, 29 Nov 2026 15:38:36 +0000 Subject: [PATCH] Encode array-like objects by their elements in to_string() --- CHANGELOG.md | 1 + src/FnDispatcher.php | 51 ++++++++++++++++++++++++++- tests/FnDispatcherTest.php | 71 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d02a227..a7614a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ * Fixed list functions and flatten to accept array-like objects. * Fixed empty array-like objects to be falsy. * Fixed length() and reverse() to read objects with __toString() as strings. +* Fixed to_string() to encode array-like objects by their elements. * Changed array-like objects that are not Traversable to error when their elements are listed. * Improved the performance of flatten on large arrays. diff --git a/src/FnDispatcher.php b/src/FnDispatcher.php index d789ee0..508697e 100644 --- a/src/FnDispatcher.php +++ b/src/FnDispatcher.php @@ -253,12 +253,61 @@ private function fn_to_string(array $args) return $v; } elseif (is_object($v) && !($v instanceof \JsonSerializable) + && !($v instanceof \ArrayAccess && $v instanceof \Countable) && method_exists($v, '__toString') ) { return (string) $v; } - return json_encode($v); + return json_encode(self::listed($v)); + } + + /** + * json_encode() would write an array-like object's properties. Values + * that hold no such object are returned as they are, not copied. + */ + private static function listed($value, &$changed = false) + { + $changed = false; + + if ($value instanceof \JsonSerializable) { + return $value; + } elseif (is_array($value)) { + $elements = $value; + } elseif ($value instanceof \stdClass) { + $elements = get_object_vars($value); + } elseif ($value instanceof \ArrayAccess && $value instanceof \Countable) { + $elements = Utils::toArray($value); + $changed = true; + } else { + return $value; + } + + $listed = []; + foreach ($elements as $key => $element) { + if (is_array($element) || is_object($element)) { + $result = self::listed($element, $elementChanged); + if ($elementChanged) { + $listed[$key] = $result; + } + } + } + + if ($listed !== []) { + // Assigning to a referenced element would change the caller's value. + $copy = []; + foreach ($elements as $key => $element) { + $copy[$key] = array_key_exists($key, $listed) ? $listed[$key] : $element; + } + $elements = $copy; + $changed = true; + } + + if (!$changed) { + return $value; + } + + return $value instanceof \stdClass ? (object) $elements : $elements; } private function fn_to_number(array $args) diff --git a/tests/FnDispatcherTest.php b/tests/FnDispatcherTest.php index ce83ce2..8a9585b 100644 --- a/tests/FnDispatcherTest.php +++ b/tests/FnDispatcherTest.php @@ -16,10 +16,24 @@ public function testConvertsToString(): void $std = new \stdClass(); $std->foo = 'bar'; $this->assertEquals('{"foo":"bar"}', $fn('to_string', [$std])); + $this->assertEquals('[1,2]', $fn('to_string', [new \ArrayObject([1, 2])])); + $this->assertEquals('{"foo":[1,2]}', $fn('to_string', [new \ArrayObject(['foo' => new \ArrayObject([1, 2])])])); + $this->assertEquals('{"0":[[1,2]]}', $fn('to_string', [(object) [[new \ArrayObject([1, 2])]]])); + $this->assertEquals('[1,2]', $fn('to_string', [new _TestStringArrayObject([1, 2])])); $this->assertEquals('foo', $fn('to_string', [new _TestStringClass()])); $this->assertEquals('"foo"', $fn('to_string', [new _TestJsonStringClass()])); } + public function testToStringEncodesWhatJsonEncodes(): void + { + $fn = new FnDispatcher(); + $list = $original = new \ArrayObject([1, 2]); + $this->assertEquals('[[1,2],"tail"]', $fn('to_string', [[&$list, 'tail']])); + $this->assertSame($original, $list); + $this->assertEquals('["foo"]', $fn('to_string', [[new _TestJsonStdClass()]])); + $this->assertEquals('{"list":[1,2]}', $fn('to_string', [new _TestPrivateStorage()])); + } + public function testMergeAcceptsStdClassObjects(): void { $fn = new FnDispatcher(); @@ -381,6 +395,14 @@ public function __toString(): string } } +class _TestStringArrayObject extends \ArrayObject +{ + public function __toString(): string + { + return 'foo'; + } +} + class _TestJsonStringClass implements \JsonSerializable { public function __toString(): string @@ -394,6 +416,55 @@ public function jsonSerialize(): string } } +class _TestJsonStdClass extends \stdClass implements \JsonSerializable +{ + public $list; + + public function __construct() + { + $this->list = new \ArrayObject([1, 2]); + } + + public function jsonSerialize(): string + { + return 'foo'; + } +} + +class _TestPrivateStorage extends \stdClass +{ + public $list; + private $storage; + + public function __construct() + { + $this->list = new \ArrayObject([1, 2]); + $this->storage = new _TestUnlistable(); + } +} + +class _TestUnlistable implements \ArrayAccess, \Countable +{ + public function offsetExists($offset): bool + { + return false; + } + + #[\ReturnTypeWillChange] + public function offsetGet($offset) + { + return null; + } + + public function offsetSet($offset, $value): void {} + public function offsetUnset($offset): void {} + + public function count(): int + { + return 0; + } +} + class _TestComparableString { private $value;