Skip to content

Commit 68e195d

Browse files
committed
Add FormatterModifier to unify placeholder modifiers
Introduce a generic FormatterModifier that dynamically instantiates and applies any formatter based on the pipe syntax. This eliminates the need to create separate modifier classes for each formatter. The modifier parses `{{placeholder|formatterName:arg1:arg2}}` syntax, resolves the formatter class via FormatterBuilder, and applies it to the value. If the formatter doesn't exist or fails, it gracefully delegates to the next modifier in the chain. Assisted-by: Copilot Assisted-by: OpenCode (ollama-cloud/glm-4.7) Assisted-by: Claude Code (Claude Opus 4.5)
1 parent 801a389 commit 68e195d

File tree

10 files changed

+3533
-7
lines changed

10 files changed

+3533
-7
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,25 @@ echo f::create()
3535
// Output: 1234 12** **** 1234
3636
```
3737

38+
### Using Formatters as Modifiers
39+
40+
The `PlaceholderFormatter` allows you to use any formatter as a modifier within templates:
41+
42+
```php
43+
use Respect\StringFormatter\PlaceholderFormatter;
44+
45+
$formatter = new PlaceholderFormatter([
46+
'date' => '2024-01-15',
47+
'amount' => '1234.56',
48+
'phone' => '1234567890',
49+
]);
50+
51+
echo $formatter->format('Date: {{date|date:Y/m/d}}, Amount: ${{amount|number:2}}, Phone: {{phone|pattern:(###) ###-####}}');
52+
// Output: Date: 2024/01/15, Amount: $1,234.56, Phone: (123) 456-7890
53+
```
54+
55+
See the [PlaceholderFormatter documentation](docs/PlaceholderFormatter.md) and [FormatterModifier documentation](docs/modifiers/FormatterModifier.md) for more details.
56+
3857
## Formatters
3958

4059
| Formatter | Description |

0 commit comments

Comments
 (0)