Skip to content

Commit 6a8d66c

Browse files
committed
Change how to stringify enumerations
Currently, the `ObjectStringifier` is taking care of enumerations, but it's better to clarify that the raw value is an enumeration rather than treat it as an object. The representation might be confused with a constant, but that's how PHP does anyways. Signed-off-by: Henrique Moody <[email protected]>
1 parent 25a2ccc commit 6a8d66c

File tree

6 files changed

+139
-0
lines changed

6 files changed

+139
-0
lines changed

src/Stringifiers/ClusterStringifier.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public static function createDefault(): self
5353
new ThrowableObjectStringifier($jsonEncodableStringifier, $quoter),
5454
new StringableObjectStringifier($jsonEncodableStringifier, $quoter),
5555
new JsonSerializableObjectStringifier($jsonEncodableStringifier, $quoter),
56+
new EnumerationStringifier($quoter),
5657
new ObjectWithDebugInfoStringifier($arrayStringifier, $quoter),
5758
new ObjectStringifier($stringifier, $quoter, self::MAXIMUM_DEPTH, self::MAXIMUM_NUMBER_OF_PROPERTIES),
5859
$arrayStringifier,
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 Respect\Stringifier\Quoter;
14+
use Respect\Stringifier\Stringifier;
15+
use UnitEnum;
16+
17+
use function sprintf;
18+
19+
final class EnumerationStringifier implements Stringifier
20+
{
21+
public function __construct(
22+
private readonly Quoter $quoter
23+
) {
24+
}
25+
26+
public function stringify(mixed $raw, int $depth): ?string
27+
{
28+
if (!$raw instanceof UnitEnum) {
29+
return null;
30+
}
31+
32+
return $this->quoter->quote(sprintf('%s::%s', $raw::class, $raw->name), $depth);
33+
}
34+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
enum BackedEnumeration: string
12+
{
13+
case BAZ = 'baz';
14+
case QUX = 'qux';
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
enum BasicEnumeration
12+
{
13+
case FOO;
14+
case BAR;
15+
}
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+
BasicEnumeration::FOO,
10+
BackedEnumeration::QUX,
11+
);
12+
?>
13+
--EXPECT--
14+
`BasicEnumeration::FOO`
15+
`BackedEnumeration::QUX`
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 BackedEnumeration;
14+
use BasicEnumeration;
15+
use PHPUnit\Framework\Attributes\CoversClass;
16+
use PHPUnit\Framework\Attributes\Test;
17+
use PHPUnit\Framework\TestCase;
18+
use Respect\Stringifier\Stringifiers\EnumerationStringifier;
19+
use Respect\Stringifier\Test\Double\FakeQuoter;
20+
21+
#[CoversClass(EnumerationStringifier::class)]
22+
final class EnumerationStringifierTest extends TestCase
23+
{
24+
private const DEPTH = 0;
25+
26+
#[Test]
27+
public function itShouldNotStringifyWhenRawValueIsNotAnEnumeration(): void
28+
{
29+
$sut = new EnumerationStringifier(new FakeQuoter());
30+
31+
self::assertNull($sut->stringify(1, self::DEPTH));
32+
}
33+
34+
#[Test]
35+
public function itShouldStringifyRawValueWhenItIsBasicEnumeration(): void
36+
{
37+
$quoter = new FakeQuoter();
38+
39+
$sut = new EnumerationStringifier($quoter);
40+
41+
$actual = $sut->stringify(BasicEnumeration::BAR, self::DEPTH);
42+
$expected = $quoter->quote('BasicEnumeration::BAR', self::DEPTH);
43+
44+
self::assertSame($expected, $actual);
45+
}
46+
47+
#[Test]
48+
public function itShouldStringifyRawValueWhenItIsBackedEnumeration(): void
49+
{
50+
$quoter = new FakeQuoter();
51+
52+
$sut = new EnumerationStringifier($quoter);
53+
54+
$actual = $sut->stringify(BackedEnumeration::BAZ, self::DEPTH);
55+
$expected = $quoter->quote('BackedEnumeration::BAZ', self::DEPTH);
56+
57+
self::assertSame($expected, $actual);
58+
}
59+
}

0 commit comments

Comments
 (0)