Skip to content

Commit f5f216a

Browse files
committed
Implement PlaceholderFormatter for template interpolation
I wanted to create a way to build dynamic, context-aware messages, similar to what we currently have in the Validation component. My long-term goal is to decouple string formatting from Validation and centralize it here in StringFormatter. This commit introduces the `PlaceholderFormatter`, which brings a familiar `{{placeholder}}` syntax to our toolkit. Recognizing that creating a new object for every unique template can be cumbersome, I have introduced the `formatWith()` method. This allows for additional data injection at call-time. However, to maintain predictability, I’ve established a clear hierarchy: parameters provided in the constructor are treated as the "primary" configuration and will always take precedence over call-time parameters. This ensures that a pre-configured formatter behaves consistently even when used in a dynamic context. By moving this logic here, we lay the groundwork for a more modular system where formatting is a first-class citizen rather than a utility hidden inside other components. Assisted-by: Claude Code (Opus 4.5)
1 parent 448b55e commit f5f216a

5 files changed

Lines changed: 1042 additions & 5 deletions

File tree

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ composer require respect/string-formatter
1616

1717
## Formatters
1818

19-
| Formatter | Description |
20-
| -------------------------------------------- | ------------------------------------------------ |
21-
| [MaskFormatter](docs/MaskFormatter.md) | Range-based string masking with Unicode support |
22-
| [PatternFormatter](docs/PatternFormatter.md) | Pattern-based string filtering with placeholders |
19+
| Formatter | Description |
20+
| ---------------------------------------------------- | --------------------------------------------------- |
21+
| [MaskFormatter](docs/MaskFormatter.md) | Range-based string masking with Unicode support |
22+
| [PatternFormatter](docs/PatternFormatter.md) | Pattern-based string filtering with placeholders |
23+
| [PlaceholderFormatter](docs/PlaceholderFormatter.md) | Template interpolation with placeholder replacement |
2324

2425
## Contributing
2526

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"description": "A powerful and flexible way of formatting and transforming strings",
55
"require": {
66
"symfony/polyfill-mbstring": "^1.33",
7-
"php": "^8.5"
7+
"php": "^8.5",
8+
"respect/stringifier": "^3.0"
89
},
910
"require-dev": {
1011
"phpunit/phpunit": "^12.5",

0 commit comments

Comments
 (0)