Download the PHP package kappa/placeholder-processor without Composer
On this page you can find all versions of the php package kappa/placeholder-processor. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download kappa/placeholder-processor
More information about kappa/placeholder-processor
Files in kappa/placeholder-processor
Package placeholder-processor
Short Description Easy system for working with placeholders in texts
License MIT
Homepage https://github.com/Kappa-org/PlaceholderProcessor
Informations about the package placeholder-processor
Kappa\PlaceholderProcessor
Easy system for working with placeholders in texts
Content
- Requirements
- Installation
- Usages
- 1. step - Prepare custom placeholder processors
- 2. step - Create a new instance
- 3. step - Usage
- Strict mode
Requirements
Full list of dependencies you can get from Composer config file
- PHP 5.4 or higher
Installation
The best way to install Kappa\PlaceholderProcessor is using Composer
Usages
1. step - Prepare custom placeholder processors
There is a few rules how to create a custom placeholder processors. Each you placeholder
processor must implements \Kappa\PlaceholderProcessor\IPlaceholderProcessor
interface or
and it is recommended you can extend your class from
\Kappa\PlaceholderProcessor\PlaceholderProcessor
class which implements this interface and
prepares logic for easy and quick usage.
For example:
This placeholder will replace %mySuperPlaceholderProcessor%
placeholder by user name by id
which is set as external source.
You must configure your processor in configure
method. In this method you must set name
of placeholder if format %<name>%
. In this case this placeholder will be works with
%mySuperPlaceholderProcessor%
placeholder.
The second important settings is list of external sources. This list will be used for automatic
compare with $source
in run
method. You can be sure that each of item from external source
will be in $source
in run
method. This sources will be given from
TextFormatter::format($text, $sources)
.
Returns from run
method will be used for replacing placeholder.
2. step - Create a new instance
For translate placeholders you need instance of Kappa\PlaceholderProcessor\TextFormatter
You can create this instance manually
or you can use setPlaceholders(array)
method
or when you use Nette Framework you can register this package as extension
services:
mySuperProcessor:
class: MySuperPlaceholderProcessor
tags: [kappa.placeholder_processor]
extensions:
placeholderProcessor: Kappa\PlaceholderProcessor\DI\PlaceholderProcessorExtension
placeholderProcessor:
strict: true
of course you can add single processor `$textFormatter->addProcessor(new MySuperPlaceholderProcessor());`.
### 3. step - Usage
Now we have custom placeholder and instance of formatter and you can try translate this text
`Hello %mySuperPlaceholderProcessor%, %foo%`
Formatter replace `%mySuperPlaceholderProcessor%` by logic in `MySuperPlaceholderProcessor` class.
This processor gets `user_id` from external source (second argument in `format` method), find
user in database and returns his name which can be used for replacing original placeholder.
Placeholder `%foo%` will be ignored because there isn't any processor with this name.
Output: `Hello Joe, %foo%`.
### Strict mode
Strict mode is turn off by default.
When you need strict mode which throws exception when in text is placeholder which hasn't any
processor (as `%foo%` in previous example). You can turn on (or off) by `setStrictMode(true)`
method or when you use [Nette Framework](https://nette.org) in config file by `strict: true`