ChillDevSpintax is a library which offers implementation of some commonly used patterns used in Symfony2 DI.
This library is provided as Composer package. To install it simply add following dependency definition to your composer.json
file:
"chilldev/spintax": "dev-master"
Replace dev-master
with different constraint if you want to use specific version.
Note: This library requires PHP 5.4.
The simplest usage that will mostly fulfill your needs is to simple parse the spintax string and generate random variation of it:
use ChillDev\Spintax\Parser;
$spintax = Parser::parse('Schrödinger’s Cat is {dead|alive}.');
$string = $spintax->generate();
But there is much more that than that in our library. First of all nested structures are supported:
use ChillDev\Spintax\Parser;
$spintax = Parser::parse('I {love {PHP|Java|C|C++|JavaScript|Python}|hate Ruby}.');
$string = $spintax->generate();
Still not finished! With our brilliant library you can detect the path used to generate given variant and re-use it later:
use ChillDev\Spintax\Parser;
$path = [];
$spintax = Parser::parse('I {love {PHP|Java|C|C++|JavaScript|Python}|hate Ruby}.');
// since $path is empty, random values will be used for missing indices and $path will be filled with them
$string = $spintax->generate($path);
// from now you can use $path to re-create the same combination
// all these calls will keep returning same string value
$spintax->generate($path);
$spintax->generate($path);
$spintax->generate($path);
$spintax->generate($path);
// this will force generating "I love Java."
$path = [0, 1];
$spintax->generate($path);
Paths are counted from 0, each entry is next step.
You can also use partial paths to define just the starting path and all missing parts will be choosen randomly:
use ChillDev\Spintax\Parser;
$path = [0];
$spintax = Parser::parse('I {love {PHP|Java|C|C++|JavaScript|Python}|hate Ruby}.');
// this will generate one of "I love {}." variants
$string = $spintax->generate($path);
For all this there is a shortcut method Parser::replicate()
(you can use comma-separated number in a single string as second argument in this shortcut method):
use ChillDev\Spintax\Parser;
echo Parser::replicate('I {love {PHP|Java|C|C++|JavaScript|Python}|hate Ruby}.', '0,0');
For more advanced aspects see advanced usage documentation or even internals description.
- Source documentation
- GitHub page with API documentation
- Issues tracker
- Packagist package
- Chillout Development @ GitHub
- Chillout Development @ Facebook
- Post on Wrzasq.pl
Do you want to help improving this project? Simply fork it and post a pull request. You can do everything on your own, you don't need to ask if you can, just do all the awesome things you want!
This project is published under MIT license.
ChillDevSpintax is brought to you by Chillout Development.
List of contributors: