acfbentveld/parser

A package to replace words in a text with values from a array. Also supports aliases and excluded properties

1.1.1 2018-10-02 11:51 UTC

README

Latest Version on Packagist Total Downloads Build Status StyleCI

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' => 'example@example.com'
    ]
];

$input = "[user.name.first_name][user.name.last_name] - [user.email]";

$result = Parser::text($input)->values($values)->parse();

will generate FooBar - example@example.com

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 info@acfbentveld.nl instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.