Skip to content

Commit 8465af4

Browse files
committed
Change how to stringify ArrayObject objects
Right now, the object stringifier takes care of ArrayObjects, but it is a bit ugly because of its property's name. Signed-off-by: Henrique Moody <[email protected]>
1 parent 86ae598 commit 8465af4

File tree

5 files changed

+115
-0
lines changed

5 files changed

+115
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ echo stringify(new ConcreteJsonSerializable()) . PHP_EOL;
145145
echo stringify(new WithDebugInfo()) . PHP_EOL;
146146
// `WithDebugInfo { __debugInfo() => ["info": "This is the return of __debugInfo()"] }`
147147

148+
echo stringify(new ArrayObject([1, 2, 3])) . PHP_EOL;
149+
// `ArrayObject { getArrayCopy() => [1, 2, 3] }`
150+
148151
echo stringify(new RuntimeException()) . PHP_EOL;
149152
// `RuntimeException { in file.php:119 }`
150153

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Respect/Stringifier.
5+
* Copyright (c) Henrique Moody <[email protected]>
6+
* SPDX-License-Identifier: MIT
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespace Respect\Stringifier\Stringifiers;
12+
13+
use ArrayObject;
14+
use Respect\Stringifier\Helpers\ObjectHelper;
15+
use Respect\Stringifier\Quoter;
16+
use Respect\Stringifier\Stringifier;
17+
18+
final class ArrayObjectStringifier implements Stringifier
19+
{
20+
use ObjectHelper;
21+
22+
public function __construct(
23+
private readonly Stringifier $stringifier,
24+
private readonly Quoter $quoter
25+
) {
26+
}
27+
28+
public function stringify(mixed $raw, int $depth): ?string
29+
{
30+
if (!$raw instanceof ArrayObject) {
31+
return null;
32+
}
33+
34+
return $this->quoter->quote(
35+
$this->format($raw, 'getArrayCopy() =>', $this->stringifier->stringify($raw->getArrayCopy(), $depth + 1)),
36+
$depth
37+
);
38+
}
39+
}

src/Stringifiers/CompositeStringifier.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public static function createDefault(): self
6565
$stringifier->prependStringifier(new FiberObjectStringifier($callableStringifier, $quoter));
6666
$stringifier->prependStringifier(new EnumerationStringifier($quoter));
6767
$stringifier->prependStringifier(new ObjectWithDebugInfoStringifier($arrayStringifier, $quoter));
68+
$stringifier->prependStringifier(new ArrayObjectStringifier($arrayStringifier, $quoter));
6869
$stringifier->prependStringifier(new JsonSerializableObjectStringifier($jsonEncodableStringifier, $quoter));
6970
$stringifier->prependStringifier(new StringableObjectStringifier($jsonEncodableStringifier, $quoter));
7071
$stringifier->prependStringifier(new ThrowableObjectStringifier($jsonEncodableStringifier, $quoter));
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--FILE--
2+
<?php
3+
4+
declare(strict_types=1);
5+
6+
require 'vendor/autoload.php';
7+
8+
outputMultiple(
9+
new ArrayObject([]),
10+
new ArrayObject([1, 2, 3]),
11+
);
12+
?>
13+
--EXPECT--
14+
`ArrayObject { getArrayCopy() => [] }`
15+
`ArrayObject { getArrayCopy() => [1, 2, 3] }`
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Respect/Stringifier.
5+
* Copyright (c) Henrique Moody <[email protected]>
6+
* SPDX-License-Identifier: MIT
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespace Respect\Stringifier\Test\Unit\Stringifiers;
12+
13+
use ArrayObject;
14+
use PHPUnit\Framework\Attributes\CoversClass;
15+
use PHPUnit\Framework\Attributes\Test;
16+
use PHPUnit\Framework\TestCase;
17+
use Respect\Stringifier\Stringifiers\ArrayObjectStringifier;
18+
use Respect\Stringifier\Test\Double\FakeQuoter;
19+
use Respect\Stringifier\Test\Double\FakeStringifier;
20+
use stdClass;
21+
22+
use function sprintf;
23+
24+
#[CoversClass(ArrayObjectStringifier::class)]
25+
final class ArrayObjectStringifierTest extends TestCase
26+
{
27+
private const DEPTH = 0;
28+
29+
#[Test]
30+
public function itShouldNotStringifyRawValueWhenItIsNotAnArrayObject(): void
31+
{
32+
$sut = new ArrayObjectStringifier(new FakeStringifier(), new FakeQuoter());
33+
34+
self::assertNull($sut->stringify(new stdClass(), self::DEPTH));
35+
}
36+
37+
#[Test]
38+
public function itShouldStringifyRawValueWhenItIsAnArrayObject(): void
39+
{
40+
$raw = new ArrayObject([1, 2, 3]);
41+
42+
$stringifier = new FakeStringifier();
43+
$quoter = new FakeQuoter();
44+
45+
$string = $stringifier->stringify($raw->getArrayCopy(), self::DEPTH + 1);
46+
47+
$sut = new ArrayObjectStringifier($stringifier, $quoter);
48+
49+
$actual = $sut->stringify($raw, self::DEPTH);
50+
$expected = $quoter->quote(
51+
sprintf('ArrayObject { getArrayCopy() => %s }', $string),
52+
self::DEPTH
53+
);
54+
55+
self::assertSame($expected, $actual);
56+
}
57+
}

0 commit comments

Comments
 (0)