Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
51 changes: 50 additions & 1 deletion src/FnDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
71 changes: 71 additions & 0 deletions tests/FnDispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down