forked from Respect/Stringifier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompositeStringifier.php
More file actions
97 lines (82 loc) · 3.36 KB
/
CompositeStringifier.php
File metadata and controls
97 lines (82 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
/*
* This file is part of Respect/Stringifier.
* Copyright (c) Henrique Moody <[email protected]>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
namespace Respect\Stringifier\Stringifiers;
use DateTimeInterface;
use Respect\Stringifier\Quoters\StandardQuoter;
use Respect\Stringifier\Stringifier;
use function array_unshift;
final class CompositeStringifier implements Stringifier
{
private const int MAXIMUM_DEPTH = 3;
private const int MAXIMUM_NUMBER_OF_ITEMS = 5;
private const int MAXIMUM_NUMBER_OF_PROPERTIES = self::MAXIMUM_NUMBER_OF_ITEMS;
private const int MAXIMUM_LENGTH = 120;
/** @var Stringifier[] */
private array $stringifiers = [];
public function __construct(Stringifier ...$stringifiers)
{
$this->stringifiers = $stringifiers;
}
public static function createDefault(): self
{
$quoter = new StandardQuoter(self::MAXIMUM_LENGTH);
$stringifier = new self(
new InfiniteNumberStringifier($quoter),
new NotANumberStringifier($quoter),
new ResourceStringifier($quoter),
new BoolStringifier($quoter),
new NullStringifier($quoter),
new DeclaredStringifier($quoter),
$jsonEncodableStringifier = new JsonEncodableStringifier(),
);
$stringifier->prependStringifier(
$arrayStringifier = new ArrayStringifier(
$stringifier,
$quoter,
self::MAXIMUM_DEPTH,
self::MAXIMUM_NUMBER_OF_ITEMS,
),
);
$stringifier->prependStringifier(
new ObjectStringifier(
$stringifier,
$quoter,
self::MAXIMUM_DEPTH,
self::MAXIMUM_NUMBER_OF_PROPERTIES,
),
);
$stringifier->prependStringifier(new CallableStringifier($stringifier, $quoter));
$stringifier->prependStringifier(
new FiberObjectStringifier(new CallableStringifier($stringifier, $quoter, closureOnly: false), $quoter),
);
$stringifier->prependStringifier(new EnumerationStringifier($quoter));
$stringifier->prependStringifier(new ObjectWithDebugInfoStringifier($arrayStringifier, $quoter));
$stringifier->prependStringifier(new ArrayObjectStringifier($arrayStringifier, $quoter));
$stringifier->prependStringifier(new JsonSerializableObjectStringifier($jsonEncodableStringifier, $quoter));
$stringifier->prependStringifier(new StringableObjectStringifier($jsonEncodableStringifier, $quoter));
$stringifier->prependStringifier(new ThrowableObjectStringifier($jsonEncodableStringifier, $quoter));
$stringifier->prependStringifier(new DateTimeStringifier($quoter, DateTimeInterface::ATOM));
$stringifier->prependStringifier(new IteratorObjectStringifier($stringifier, $quoter));
return $stringifier;
}
public function prependStringifier(Stringifier $stringifier): void
{
array_unshift($this->stringifiers, $stringifier);
}
public function stringify(mixed $raw, int $depth): string|null
{
foreach ($this->stringifiers as $stringifier) {
$string = $stringifier->stringify($raw, $depth);
if ($string === null) {
continue;
}
return $string;
}
return null;
}
}