Download the PHP package jesseschutt/token-replacer without Composer
On this page you can find all versions of the php package jesseschutt/token-replacer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jesseschutt/token-replacer
More information about jesseschutt/token-replacer
Files in jesseschutt/token-replacer
Package token-replacer
Short Description Define tokens and replace the occurrences in strings.
License MIT
Homepage https://github.com/jesseschutt/token-replacer
Informations about the package token-replacer
Token Replacer
Credit
This package is a fork of the original, which was created by Jamie Holly of HollyIT.
Installation
Configuration
You can publish the configuration file by running the following command:
This will create a token-replacer.php
file in your config
directory. Here you can set the default token start and end characters, the default token separator, and the default transformers.
Instructions
This package allows you to define tokens that can be replaced in strings. Instead of a simple str_replace
, Token Replacer lets you add options to each token. Let's start with an example.
There is a certain anatomy to tokens, so let's take a look at the {{ date:m }}
. This is a default token format, but this format is configurable globally and per instance.
Part | Name | Global Setting | Local Setting |
---|---|---|---|
{{ | Token Start | config('token-replacer.default_start_token') |
$instance->startToken() |
date | Token Name | --- | --- |
: | Token Separator | config('token-replacer.default_ token_separator') |
$instance->tokenSeparator() |
m | Options | --- | --- |
}} | Token End | config('token-replacer.default_end_token') |
$instance->endToken() |
Transformers
The replacement of tokens is handled via a transformer. A transformer can be a closure or a simple class.
Transformers can be added to each instance of the TokenReplacer or added globally by adding them to the default_transformers
array in the configuration file.
Note: Global transformers do not receive any data when instantiated. DateTransformer
and AuthTransformer
are the only two built-in transformers that are eligible for global use.
Per instance tokens are added via $instance->with({token name}, {class name, transformer instance or closure});
For closure based transformers the signature is:
Included Transformers
All these transformers live under the \JesseSchutt\TokenReplacer\Transformers
namespace.
Transformer | Description | Instantiating |
---|---|---|
ArrayTransformer | Extracts a value from an array based on a key. | The array |
DateTransfomer | Extract parts of a date by PHP date format tokens. | Date object, Carbon, date string or null (assumes now()) |
FileTransformer | Extract parts from a file based on pathinfo(). | The path string |
ObjectTransformer | Extract a property from an object. | The object |
UrlTransformer | Extract based on parse_url() |
The URL string |
There are also special Laravel transformers residing in \JesseSchutt\TokenReplacer\Transformers\Laravel
Transformer | Description | Instantiating |
---|---|---|
AuthTransformer | Extract a property from the auth (user) object | none |
DotArrayTransformer | Extract a property using Laravel's dot syntax | the array |
ModelTransformer | Extract a property from an Eloquent model. For date properties, you can add the date part by specifying the format after the property name (ie: model:created_at,m) | The model |
UploadFileTransformer | Extract the basename or extension from an UploadedFile object | The upload |
Post-processing
None of the transformers do any further processing except extracting the item. If you want to do further processing, such as escaping the replacements, you can define an onReplace
callback:
This callback will run for every token occurrence found, so you can further filter down what tokens
to operate on by checking the $token
property.
Invalid or missing tokens
By default, invalid and missing tokens will remain in the string. To prevent this, you can set removeEmpty()
on the TokenReplacer.