acfbentveld / parser
A package to replace words in a text with values from a array. Also supports aliases and excluded properties
Installs: 1 181
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 3
Forks: 0
Open Issues: 0
pkg:composer/acfbentveld/parser
Requires
- php: ^7.1
Requires (Dev)
- phpunit/phpunit: ^7.0
README
A package to replace words in a text with values from a array. Also supports aliases and excluded properties.
Installation
You can install the package via composer:
composer require acfbentveld/Parser
Basic Usage
Parser::text('Hello [who]')->values(['who' => 'world'])->parse(); // Hello world Parser::text('Hello {who}')->values(['who' => 'world'])->tags(['{', '}'])->parse(); // Hello world Parser::text('Hello [who]')->values(['who' => 'world'])->exclude(['who'])->parse(); // Hello [who] Parser::text('Hello [what]')->values(['who' => 'world'])->aliases(['what' => 'who'])->parse(); // Hello world
Using arrays as values
$values = [ 'user' => [ 'name' => [ 'first_name' => 'Foo', 'last_name' => 'Bar' ], 'email' => '[email protected]' ] ]; $input = "[user.name.first_name][user.name.last_name] - [user.email]"; $result = Parser::text($input)->values($values)->parse();
will generate FooBar - [email protected]
Available methods
All methods can be chained together like text()->values()->aliases() and can be in any order.
But you always have to start with the text() function.
text
This sets the string you want to parse
$parser = Parser::text('string')
values
This sets the values to use while parsing. Must be a array
$parser->values([]);
tags
Tags are the characters around the keys you want to parse. Default [ and ]
$parser->tags(['{','}']);
exclude
Sets the keys which are excluded from parsing
$parser->exclude(['key', 'key2']);
aliases
Sets the aliases. Aliases can be used to map a value to a different name.
So for example you can set the aliases to ['name' => 'username'] to map username to name
$parser->exclude(['alias', 'value key']);
parse
Parses the text and returns the parsed string
$parser->exclude(['alias', 'value key']);
Testing
composer test
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.