|
1 | 1 | # StringifyModifier |
2 | 2 |
|
3 | | -The `StringifyModifier` converts values to strings using a `Stringifier` instance. This is the default modifier used by `PlaceholderFormatter`. |
| 3 | +The `StringifyModifier` always converts values to strings, regardless of their type. |
| 4 | + |
| 5 | +> **Note:** When used directly (without [StringPassthroughModifier](StringPassthroughModifier.md)), this modifier quotes strings. In that case, [RawModifier](RawModifier.md) can provide a `|raw` pipe for unquoted output. The default chain includes `StringPassthroughModifier`, which bypasses stringification for strings. |
4 | 6 |
|
5 | 7 | ## Behavior |
6 | 8 |
|
7 | | -- Strings pass through unchanged |
8 | | -- Other types are converted using the configured stringifier |
9 | | -- Throws `InvalidModifierPipeException` if an unrecognized pipe is passed |
| 9 | +| Pipe | Any Value | |
| 10 | +| ------ | ---------------- | |
| 11 | +| (none) | Stringified | |
| 12 | +| Other | Throws exception | |
| 13 | + |
| 14 | +All values are converted to strings using the stringifier. Unlike `StringPassthroughModifier`, even strings are passed through the stringifier, which may or may not modify them. |
10 | 15 |
|
11 | 16 | ## Usage |
12 | 17 |
|
| 18 | +The `StringifyModifier` is the default modifier in `PlaceholderFormatter`. You can also create instances with custom stringifiers: |
| 19 | + |
13 | 20 | ```php |
14 | 21 | use Respect\StringFormatter\PlaceholderFormatter; |
| 22 | +use Respect\StringFormatter\Modifiers\StringifyModifier; |
| 23 | +use Respect\Stringifier\HandlerStringifier; |
| 24 | + |
| 25 | +$stringifier = HandlerStringifier::create(); |
| 26 | +$formatter = new PlaceholderFormatter( |
| 27 | + ['name' => 'John', 'active' => true, 'items' => [1, 2]], |
| 28 | + new StringifyModifier($stringifier), |
| 29 | +); |
15 | 30 |
|
16 | | -$formatter = new PlaceholderFormatter([ |
17 | | - 'name' => 'John', |
18 | | - 'active' => true, |
19 | | - 'data' => ['x' => 1], |
20 | | -]); |
| 31 | +echo $formatter->format('User: {{name}}'); |
| 32 | +// Output: User: "John" |
21 | 33 |
|
22 | | -echo $formatter->format('{{name}} is {{active}}'); |
23 | | -// Output: John is true |
| 34 | +echo $formatter->format('Active: {{active}}'); |
| 35 | +// Output: Active: `true` |
24 | 36 |
|
25 | | -echo $formatter->format('Data: {{data}}'); |
26 | | -// Output: Data: ["x":1] |
| 37 | +echo $formatter->format('Items: {{items}}'); |
| 38 | +// Output: Items: `[1, 2]` |
27 | 39 | ``` |
28 | 40 |
|
| 41 | +## Examples |
| 42 | + |
| 43 | +| Parameters | Template | Output | |
| 44 | +| ------------------------ | ------------ | --------- | |
| 45 | +| `['name' => 'John']` | `{{name}}` | `"John"` | |
| 46 | +| `['count' => 42]` | `{{count}}` | `42` | |
| 47 | +| `['price' => 19.99]` | `{{price}}` | `19.99` | |
| 48 | +| `['active' => true]` | `{{active}}` | `true` | |
| 49 | +| `['active' => false]` | `{{active}}` | `false` | |
| 50 | +| `['value' => null]` | `{{value}}` | `null` | |
| 51 | +| `['items' => [1, 2, 3]]` | `{{items}}` | `[1,2,3]` | |
| 52 | +| `['data' => ['a' => 1]]` | `{{data}}` | `["a":1]` | |
| 53 | + |
29 | 54 | ## Custom Stringifier |
30 | 55 |
|
31 | 56 | ```php |
32 | | -use Respect\StringFormatter\PlaceholderFormatter; |
33 | 57 | use Respect\StringFormatter\Modifiers\StringifyModifier; |
34 | 58 | use Respect\Stringifier\Stringifier; |
35 | 59 |
|
36 | | -$formatter = new PlaceholderFormatter( |
37 | | - ['data' => $value], |
38 | | - new StringifyModifier($customStringifier), |
39 | | -); |
| 60 | +final readonly class CustomStringifier implements Stringifier |
| 61 | +{ |
| 62 | + public function stringify(mixed $raw): string|null |
| 63 | + { |
| 64 | + return is_bool($raw) ? ($raw ? 'YES' : 'NO') : json_encode($raw); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +$modifier = new StringifyModifier(new CustomStringifier()); |
| 69 | +echo $modifier->modify(true, null); |
| 70 | +// Output: YES |
40 | 71 | ``` |
41 | 72 |
|
42 | | -See the [Respect\Stringifier documentation](https://github.com/Respect/Stringifier) for details on stringifiers. |
| 73 | +## Examples |
| 74 | + |
| 75 | +| Parameters | Template | Output | |
| 76 | +| ----------------------------- | ----------- | ------------------- | |
| 77 | +| `['name' => 'John']` | `{{name}}` | `"John"` | |
| 78 | +| `['count' => 42]` | `{{count}}` | `"42"` | |
| 79 | +| `['on' => true]` | `{{on}}` | `` `true` `` | |
| 80 | +| `['off' => false]` | `{{off}}` | `` `false` `` | |
| 81 | +| `['items' => [1, 2]]` | `{{items}}` | `` `[1, 2]` `` | |
| 82 | +| `['obj' => (object)['a'=>1]]` | `{{obj}}` | `` `stdClass {}` `` | |
| 83 | +| `['val' => null]` | `{{val}}` | `` `null` `` | |
0 commit comments