|
| 1 | +# QuoteModifier |
| 2 | + |
| 3 | +The `QuoteModifier` adds proper quoting to string values using a configurable quoter. |
| 4 | + |
| 5 | +## Purpose |
| 6 | + |
| 7 | +The `|quote` modifier applies string quoting to values, handling escape sequences and special characters according to the configured quoter implementation. |
| 8 | + |
| 9 | +## Behavior |
| 10 | + |
| 11 | +The modifier only processes string values. For non-string values, it passes them to the next modifier in the chain. |
| 12 | + |
| 13 | +### Quoting Behavior |
| 14 | + |
| 15 | +- Strings are quoted using the configured `Quoter` instance (default: `CodeQuoter` with 256 character limit) |
| 16 | +- Non-string values are passed to the next modifier unchanged |
| 17 | +- Handles escape sequences and special characters according to the quoter implementation |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | +```php |
| 22 | +use Respect\StringFormatter\PlaceholderFormatter; |
| 23 | +use Respect\StringFormatter\Modifier\QuoteModifier; |
| 24 | +use Respect\StringFormatter\Modifier\StringifyModifier; |
| 25 | + |
| 26 | +$formatter = new PlaceholderFormatter([ |
| 27 | + 'name' => 'John "Johnny" Doe', |
| 28 | + 'path' => 'C:\Users\Files\My Document.txt', |
| 29 | + 'status' => 'It\'s working', |
| 30 | + 'age' => 30, |
| 31 | + 'data' => ['x' => 1] |
| 32 | +]); |
| 33 | + |
| 34 | +echo $formatter->format('{{name|quote}}'); |
| 35 | +// Outputs: '"John \"Johnny\" Doe"' |
| 36 | + |
| 37 | +echo $formatter->format('{{status|quote}}'); |
| 38 | +// Outputs: '"It\'s working"' |
| 39 | + |
| 40 | +echo $formatter->format('{{age|quote}}'); |
| 41 | +// Outputs: "30" (passed to StringifyModifier, no quoting applied) |
| 42 | +``` |
| 43 | + |
| 44 | +## Examples |
| 45 | + |
| 46 | +### Basic Quoting |
| 47 | + |
| 48 | +| Parameters | Template | Output | |
| 49 | +| ---------- | -------- | ------ | |
| 50 | +| `['name' => 'John']` | `"{{name|quote}}"` | `"\"John\""` | |
| 51 | +| `['text' => 'Hello "World"']` | `"{{text|quote}}"` | `"\"Hello \\\"World\\\"\""` | |
| 52 | +| `['path' => 'C:\temp']` | `"{{path|quote}}"` | `"\"C:\\temp\""` | |
| 53 | + |
| 54 | +### Escape Sequences |
| 55 | + |
| 56 | +| Parameters | Template | Output | |
| 57 | +| ---------- | -------- | ------ | |
| 58 | +| `['text' => "Line1\nLine2"]` | `"{{text|quote}}"` | `"\"Line1\\nLine2\""` | |
| 59 | +| `['text' => "Tab\tSeparated"]` | `"{{text|quote}}"` | `"\"Tab\\tSeparated\""` | |
| 60 | +| `['text' => "Back\\Slash"]` | `"{{text|quote}}"` | `"\"Back\\\\Slash\""` | |
| 61 | + |
| 62 | +### Non-string Values |
| 63 | + |
| 64 | +| Parameters | Template | Output | |
| 65 | +| ---------- | -------- | ------ | |
| 66 | +| `['age' => 30]` | `"{{age|quote}}"` | `"30"` | |
| 67 | +| `['active' => true]` | `"{{active|quote}}"` | `"true"` | |
| 68 | +| `['items' => ['a', 'b']]` | `"{{items|quote}}"` | `["a","b"]` | |
| 69 | + |
| 70 | +## Custom Quoter |
| 71 | + |
| 72 | +You can create a `QuoteModifier` with a custom quoter: |
| 73 | + |
| 74 | +```php |
| 75 | +use Respect\StringFormatter\PlaceholderFormatter; |
| 76 | +use Respect\StringFormatter\Modifier\QuoteModifier; |
| 77 | +use Respect\StringFormatter\Modifier\RawModifier; |
| 78 | +use Respect\StringFormatter\Modifier\StringifyModifier; |
| 79 | +use Respect\Stringifier\Quoters\CodeQuoter; |
| 80 | + |
| 81 | +// Custom quoter with different character limit |
| 82 | +$customQuoter = new CodeQuoter(512); |
| 83 | +$quoteModifier = new QuoteModifier($rawModifier, $customQuoter); |
| 84 | + |
| 85 | +$formatter = new PlaceholderFormatter(['text' => $value], $quoteModifier); |
| 86 | +``` |
| 87 | + |
| 88 | +## Implementation Notes |
| 89 | + |
| 90 | +The `QuoteModifier`: |
| 91 | + |
| 92 | +1. Checks if the pipe value is `"quote"` and the input value is a string |
| 93 | +2. If both conditions are met, applies the configured quoter to the string |
| 94 | +3. Otherwise, passes the value to the next modifier in the chain |
| 95 | +4. Uses `CodeQuoter` by default with a 256 character limit |
| 96 | + |
| 97 | +## Common Use Cases |
| 98 | + |
| 99 | +1. **CSV output** - Properly quote fields containing special characters |
| 100 | +2. **Code generation** - Quote string literals in generated code |
| 101 | +3. **Configuration files** - Ensure string values are properly escaped |
| 102 | +4. **Logging** - Prepare string values for logging with proper escaping |
| 103 | + |
| 104 | +## Integration |
| 105 | + |
| 106 | +`QuoteModifier` is typically positioned early in the chain: |
| 107 | + |
| 108 | +```php |
| 109 | +QuoteModifier -> RawModifier -> StringifyModifier |
| 110 | +``` |
| 111 | + |
| 112 | +This ensures string quoting is applied before other processing, while still allowing raw output and stringification fallbacks. |
0 commit comments