Skip to content

Commit c457629

Browse files
committed
Improve the documentation in README.md
I added a lot of examples of stringified values and an example of how to create a custom stringifier and integrate it with the existing ones. Signed-off-by: Henrique Moody <[email protected]>
1 parent e6f4099 commit c457629

File tree

1 file changed

+152
-10
lines changed

1 file changed

+152
-10
lines changed

README.md

Lines changed: 152 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,169 @@ composer require respect/stringifier
1919

2020
This library requires PHP >= 8.1.
2121

22-
## Feature Guide
22+
## Usage
2323

2424
Below a quick guide of how to use the library.
2525

26-
### Namespace import
27-
28-
Respect\Stringifier is namespaced, and you can make your life easier by importing
29-
a single function into your context:
26+
### Using as a function
3027

3128
```php
32-
use function Respect\Stringifier\stringify;
29+
echo Respect\Stringifier\stringify($value);
3330
```
3431

35-
Stringifier was built using objects, the `stringify()` is a easy way to use it.
32+
### Using as an object
33+
34+
```php
35+
$stringify = Respect\Stringifier\Stringify::createDefault();
36+
37+
// with the `value` method
38+
echo $stringify->value($value);
3639

37-
### Usage
40+
// with the `__invoke` method
41+
echo $stringify($value);
42+
```
3843

39-
Simply use the function to convert any value you want to:
44+
### Examples
4045

4146
```php
42-
echo stringify($value);
47+
use function Respect\Stringifier\stringify;
48+
49+
echo stringify('string') . PHP_EOL;
50+
// "string"
51+
52+
echo stringify(implode(PHP_EOL, ['Multi-line', 'string'])) . PHP_EOL;
53+
// "Multi-line\nstring"
54+
55+
echo stringify(1) . PHP_EOL;
56+
// 1
57+
58+
echo stringify(0.5) . PHP_EOL;
59+
// 0.5
60+
61+
echo stringify(true) . PHP_EOL;
62+
// `true`
63+
64+
echo stringify(false) . PHP_EOL;
65+
// `false`
66+
67+
echo stringify(null) . PHP_EOL;
68+
// `null`
69+
70+
echo stringify(INF) . PHP_EOL;
71+
// `INF`
72+
73+
echo stringify(-INF) . PHP_EOL;
74+
// `-INF`
75+
76+
echo stringify(acos(8)) . PHP_EOL;
77+
// `NaN`
78+
79+
echo stringify([1, 2, 3]) . PHP_EOL;
80+
// `[1, 2, 3]`
81+
82+
echo stringify(['foo' => true, 'bar' => 42, 'baz' => ['qux' => INF, 'quux' => null]]) . PHP_EOL;
83+
// `["foo": true, "bar": 42, "baz": ["qux": INF, "quux": null]]`
84+
85+
echo stringify(tmpfile()) . PHP_EOL;
86+
// `resource <stream>`
87+
88+
echo stringify(BasicEnumeration::FOO) . PHP_EOL;
89+
// `BasicEnumeration::FOO`
90+
91+
echo stringify(BackedEnumeration::QUX) . PHP_EOL;
92+
// `BackedEnumeration::QUX`
93+
94+
echo stringify(new WithProperties()) . PHP_EOL;
95+
// `WithProperties { +$publicProperty=true #$protectedProperty=42 -$privateProperty="something" }`
96+
97+
echo stringify(new WithUninitializedProperties()) . PHP_EOL;
98+
// `WithUninitializedProperties { +$uninitializedProperty=*uninitialized* }`
99+
100+
echo stringify(new class { public int $property = 42; }) . PHP_EOL;
101+
// `class { +$property=42 }`
102+
103+
echo stringify(new class extends WithProperties { }) . PHP_EOL;
104+
// `WithProperties@anonymous { +$publicProperty=true #$protectedProperty=42 }`
105+
106+
echo stringify('chr') . PHP_EOL;
107+
// `chr(int $codepoint): string`
108+
109+
echo stringify([new WithMethods(), 'publicMethod']) . PHP_EOL;
110+
// `WithMethods->publicMethod(Iterator&Countable $parameter): ?static`
111+
112+
echo stringify('WithMethods::publicStaticMethod') . PHP_EOL;
113+
// `WithMethods::publicStaticMethod(int|float $parameter): void`
114+
115+
echo stringify(['WithMethods', 'publicStaticMethod']) . PHP_EOL;
116+
// `WithMethods::publicStaticMethod(int|float $parameter): void`
117+
118+
echo stringify(new WithInvoke()) . PHP_EOL;
119+
// `WithInvoke->__invoke(int $parameter = 0): never`
120+
121+
echo stringify(static fn(int $foo): string => '') . PHP_EOL;
122+
// `function (int $foo): string`
123+
124+
echo stringify(new DateTime()) . PHP_EOL;
125+
// `DateTime { 2023-04-21T11:29:03+00:00 }`
126+
127+
echo stringify(new DateTimeImmutable()) . PHP_EOL;
128+
// `DateTimeImmutable { 2023-04-21T11:29:03+00:00 }`
129+
130+
echo stringify(new Fiber('strlen')) . PHP_EOL;
131+
// `Fiber { strlen(string $string): int }`
132+
133+
echo stringify((static fn(int $number) => yield $number)(7)) . PHP_EOL;
134+
// `Generator { current() => 7 }`
135+
136+
echo stringify(new ConcreteIterator()) . PHP_EOL;
137+
// `ConcreteIterator { current() => 1 }`
138+
139+
echo stringify(new ConcreteStringable()) . PHP_EOL;
140+
// `ConcreteStringable { __toString() => "This is the return of __toString()" }`
141+
142+
echo stringify(new ConcreteJsonSerializable()) . PHP_EOL;
143+
// `ConcreteJsonSerializable { jsonSerialize() => {"0":1,"1":2,"2":3,"foo":true} }`
144+
145+
echo stringify(new WithDebugInfo()) . PHP_EOL;
146+
// `WithDebugInfo { __debugInfo() => ["info": "This is the return of __debugInfo()"] }`
147+
148+
echo stringify(new RuntimeException()) . PHP_EOL;
149+
// `RuntimeException { in file.php:119 }`
150+
151+
echo stringify(new InvalidArgumentException('This is the exception message')) . PHP_EOL;
152+
// `InvalidArgumentException { "This is the exception message" in file.php:112 }`
43153
```
44154

45155
To see more examples of how to use the library check the [integration tests](tests/integration).
156+
157+
### Custom stringifiers
158+
159+
Stringifier library is extensible, you can create your own stringifiers and use them with the `Stringify` class.
160+
161+
```php
162+
use Respect\Stringifier\Stringifier;
163+
use Respect\Stringifier\Stringifiers\CompositeStringifier;
164+
use Respect\Stringifier\Stringify;
165+
166+
$compositeStringifier = CompositeStringifier::createDefault();
167+
$compositeStringifier->prependStringifier(new class implements Stringifier {
168+
public function stringify(mixed $raw, int $depth): ?string
169+
{
170+
if (is_object($raw) && method_exists($raw, 'toString')) {
171+
return $raw->toString();
172+
}
173+
174+
return null;
175+
}
176+
});
177+
178+
$stringify = new Stringify($compositeStringifier);
179+
180+
echo $stringify->value(new class {
181+
public function toString(): string
182+
{
183+
return 'Hello, world!';
184+
}
185+
});
186+
// Hello, world!
187+
```

0 commit comments

Comments
 (0)