Skip to content

Commit e6f4099

Browse files
committed
Improve the reusability of the concrete API
Even though this library is extensible, I acknowledge that combining a custom stringifier with the existing ones takes work. I made the API a bit more reusable and created a `Stringify` class that could facilitate engineers to use this library from a DIC. There were also a couple of name changes because "cluster" always bothered me a little. Signed-off-by: Henrique Moody <[email protected]>
1 parent be6f2f4 commit e6f4099

File tree

9 files changed

+310
-214
lines changed

9 files changed

+310
-214
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"Respect\\Stringifier\\": "src/"
2929
},
3030
"files": [
31-
"src/stringify.php"
31+
"stringify.php"
3232
]
3333
},
3434
"autoload-dev": {

src/Stringifiers/ClusterStringifier.php

Lines changed: 0 additions & 104 deletions
This file was deleted.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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 DateTimeInterface;
14+
use Respect\Stringifier\Quoters\CodeQuoter;
15+
use Respect\Stringifier\Stringifier;
16+
17+
use function array_unshift;
18+
19+
final class CompositeStringifier implements Stringifier
20+
{
21+
private const MAXIMUM_DEPTH = 3;
22+
private const MAXIMUM_NUMBER_OF_ITEMS = 5;
23+
private const MAXIMUM_NUMBER_OF_PROPERTIES = self::MAXIMUM_NUMBER_OF_ITEMS;
24+
25+
/**
26+
* @var Stringifier[]
27+
*/
28+
private array $stringifiers = [];
29+
30+
public function __construct(Stringifier ...$stringifiers)
31+
{
32+
$this->stringifiers = $stringifiers;
33+
}
34+
35+
public static function createDefault(): self
36+
{
37+
$quoter = new CodeQuoter();
38+
39+
$stringifier = new self(
40+
new InfiniteNumberStringifier($quoter),
41+
new NotANumberStringifier($quoter),
42+
new ResourceStringifier($quoter),
43+
new BoolStringifier($quoter),
44+
new NullStringifier($quoter),
45+
$jsonEncodableStringifier = new JsonEncodableStringifier(),
46+
);
47+
$stringifier->prependStringifier(
48+
$arrayStringifier = new ArrayStringifier(
49+
$stringifier,
50+
$quoter,
51+
self::MAXIMUM_DEPTH,
52+
self::MAXIMUM_NUMBER_OF_ITEMS,
53+
)
54+
);
55+
$stringifier->prependStringifier(
56+
new ObjectStringifier(
57+
$stringifier,
58+
$quoter,
59+
self::MAXIMUM_DEPTH,
60+
self::MAXIMUM_NUMBER_OF_PROPERTIES
61+
)
62+
);
63+
$stringifier->prependStringifier($callableStringifier = new CallableStringifier($stringifier, $quoter));
64+
$stringifier->prependStringifier(new FiberObjectStringifier($callableStringifier, $quoter));
65+
$stringifier->prependStringifier(new EnumerationStringifier($quoter));
66+
$stringifier->prependStringifier(new ObjectWithDebugInfoStringifier($arrayStringifier, $quoter));
67+
$stringifier->prependStringifier(new JsonSerializableObjectStringifier($jsonEncodableStringifier, $quoter));
68+
$stringifier->prependStringifier(new StringableObjectStringifier($jsonEncodableStringifier, $quoter));
69+
$stringifier->prependStringifier(new ThrowableObjectStringifier($jsonEncodableStringifier, $quoter));
70+
$stringifier->prependStringifier(new DateTimeStringifier($quoter, DateTimeInterface::ATOM));
71+
$stringifier->prependStringifier(new IteratorObjectStringifier($stringifier, $quoter));
72+
73+
return $stringifier;
74+
}
75+
76+
public function prependStringifier(Stringifier $stringifier): void
77+
{
78+
array_unshift($this->stringifiers, $stringifier);
79+
}
80+
81+
public function stringify(mixed $raw, int $depth): ?string
82+
{
83+
foreach ($this->stringifiers as $stringifier) {
84+
$string = $stringifier->stringify($raw, $depth);
85+
if ($string === null) {
86+
continue;
87+
}
88+
89+
return $string;
90+
}
91+
92+
return null;
93+
}
94+
}

src/Stringify.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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;
12+
13+
use Respect\Stringifier\Stringifiers\CompositeStringifier;
14+
15+
use function print_r;
16+
17+
final class Stringify
18+
{
19+
public const STARTING_DEPTH = 0;
20+
21+
public function __construct(
22+
private readonly Stringifier $stringifier
23+
) {
24+
}
25+
26+
public static function createDefault(): self
27+
{
28+
return new self(CompositeStringifier::createDefault());
29+
}
30+
31+
public function value(mixed $value): string
32+
{
33+
return $this->stringifier->stringify($value, self::STARTING_DEPTH) ?? print_r($value, true);
34+
}
35+
36+
public function __invoke(mixed $value): string
37+
{
38+
return $this->value($value);
39+
}
40+
}

src/stringify.php renamed to stringify.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,13 @@
1010

1111
namespace Respect\Stringifier;
1212

13-
use Respect\Stringifier\Stringifiers\ClusterStringifier;
14-
1513
function stringify(mixed $value): string
1614
{
17-
static $stringifier;
15+
static $stringify;
1816

19-
if ($stringifier === null) {
20-
$stringifier = ClusterStringifier::createDefault();
17+
if ($stringify === null) {
18+
$stringify = Stringify::createDefault();
2119
}
2220

23-
return $stringifier->stringify($value, 0) ?? '#ERROR#';
21+
return $stringify->value($value);
2422
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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\Double;
12+
13+
use Respect\Stringifier\Stringifier;
14+
15+
final class LameStringifier implements Stringifier
16+
{
17+
public function stringify(mixed $raw, int $depth): ?string
18+
{
19+
return null;
20+
}
21+
}

tests/unit/Stringifiers/ClusterStringifierTest.php

Lines changed: 0 additions & 103 deletions
This file was deleted.

0 commit comments

Comments
 (0)