├── LICENSE ├── README.md ├── composer.json └── src └── DynamicString.php /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Stefano Azzolini 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DynamicString 2 | 3 | > Generate random strings from templates. 4 | 5 | [![Latest Stable Version](https://poser.pugx.org/lastguest/dynamic-string/v/stable.svg)](https://packagist.org/packages/lastguest/dynamic-string) [![Total Downloads](https://poser.pugx.org/lastguest/dynamic-string/downloads.svg)](https://packagist.org/packages/lastguest/dynamic-string) [![Latest Unstable Version](https://poser.pugx.org/lastguest/dynamic-string/v/unstable.svg)](https://packagist.org/packages/lastguest/dynamic-string) [![License](https://poser.pugx.org/lastguest/dynamic-string/license.svg)](https://packagist.org/packages/lastguest/dynamic-string) 6 | 7 | ## Installation 8 | 9 | Install via [composer](https://getcomposer.org/download/): 10 | 11 | ```bash 12 | $ composer require lastguest/dynamic-string 13 | ``` 14 | 15 | ### How to use 16 | 17 | ```php 18 | $generator = new DynamicString(); 19 | echo $generator->render("I want a (fried|double|spicy) (tuna|salmon|crab) (sushi|(ura|te)maki), please."); 20 | ``` 21 | 22 | ### Templates 23 | 24 | Use `()` for define a group that will be replaced by one of the contained alternatives, random selected. 25 | 26 | The `|` character is used as a separator for multiple choices. 27 | 28 | 29 | This template will render `Yes` or `No` randomly : 30 | 31 | ``` 32 | (Yes|No) 33 | ``` 34 | 35 | Groups can be nested, they will resolved with a deep-first order. 36 | 37 | This template will render `Finger`, `Fingers`, `Hand`, `Hands` randomly : 38 | 39 | ``` 40 | (Finge(r|rs)|Han(d|ds)) 41 | ``` 42 | 43 | A complete template for generate random Sushi orders : 44 | 45 | ``` 46 | I want a (fried|double|spicy) (tuna|salmon|crab) (sushi|(ura|te)maki), please. 47 | ``` 48 | 49 | Some outputs : 50 | 51 | ``` 52 | I want a spicy salmon sushi, please. 53 | I want a double tuna uramaki, please. 54 | I want a double salmon temaki, please. 55 | I want a spicy crab sushi, please. 56 | ``` 57 | 58 | ### Changing default group envelope and separator 59 | 60 | You can change the default group envelope and separator in the `DynamicString` constructor. 61 | 62 | ```php 63 | $generator = new DynamicString('<>',','); 64 | echo $generator->render(" function( myVar){};"); 65 | ``` 66 | 67 | Output: 68 | 69 | ``` 70 | private function(array myVar){}; 71 | ``` 72 | 73 | ### Other examples 74 | 75 | A fun use for DynamicString: a fantasy RPG stories generator. 76 | 77 | ```php 78 | $generator = new DynamicString(); 79 | 80 | $name = "(Quick|Fast|Wet|Rusty|Warm|Curved|Old|Terror|Notched|Smelly|Loud|Heavy|Shiny|Sparkling|Fire|Laser|Cold|Fizzle|Power|Bad|Good|Rustle|Dark|Smokey|Super|Fishy|Pointy|Flash|Beauty|Bleeding|Foo|Master|Small|Big|Red|Gray|Hairy|Magic|Broken|Sharp|Grand|Straight)(mind|blower|killer|hand|ey(e|es)|fee(t|ts)|finge(r|rs)|nose|armpi(t|ts)|blaster|rhymes|bar|sword|blade|breath)"; 81 | 82 | $race = "(lion|human|worm|rat|orc|elf|pixie|walrus|rooster|dog|parrot|rabbit)"; 83 | $class = "(rogue|paladin|warrior|cook|cleric|psychopath|fisherman|wolf)"; 84 | $player = "$name, the $race $class."; 85 | 86 | $sillystory = "(A (sloppy|quick|brave|small|gentle|cruel|fabulous|shiny) $class $race, named $name) (jumped onto|walked to|crawled under|entered|sprinted to|runned into) a( rooftop| bridge|n house| wormhole|n inn| dungeon) (shouting|whispering|saying|barfing|asking for) ((ancient|magic|modern|silly|awkward) (words|names|runes|spells|shouts|rhimes|swears|songs|tacos))."; 87 | ``` 88 | 89 | Now, we can generate a silly story with : 90 | 91 | ```php 92 | echo $generator->render($sillystory); 93 | ``` 94 | 95 | Some examples : 96 | 97 | ``` 98 | A cruel rogue orc, named Smellyarmpits jumped onto an house barfing magic tacos. 99 | A fabulous paladin pixie, named Graybar jumped onto a rooftop shouting silly runes. 100 | A brave paladin worm, named Pointyblaster runned into an inn saying awkward names. 101 | A brave psychopath rooster, named Wetsword entered a bridge whispering silly words. 102 | A shiny paladin worm, named Darkeyes walked to a bridge asking for awkward names. 103 | A small psychopath rooster, named Bleedingbar crawled under an inn saying magic songs. 104 | A sloppy warrior parrot, named Rustyblower jumped onto an house asking for ancient swears. 105 | ``` 106 | 107 | Or we can generate some fun names for our characters : 108 | 109 | ```php 110 | echo $generator->render($player); 111 | ``` 112 | 113 | ``` 114 | Badblade, the rabbit cleric. 115 | Hairykiller, the elf fisherman. 116 | Bleedingblower, the pixie psychopath. 117 | Notchedrhymes, the pixie paladin. 118 | Fastfingers, the human psychopath. 119 | Hairyfeets, the pixie fisherman. 120 | ``` 121 | 122 | ## License (MIT) 123 | 124 | Copyright (c) 2015 Stefano Azzolini 125 | 126 | Permission is hereby granted, free of charge, to any person 127 | obtaining a copy of this software and associated documentation 128 | files (the "Software"), to deal in the Software without 129 | restriction, including without limitation the rights to use, 130 | copy, modify, merge, publish, distribute, sublicense, and/or sell 131 | copies of the Software, and to permit persons to whom the 132 | Software is furnished to do so, subject to the following 133 | conditions: 134 | 135 | The above copyright notice and this permission notice shall be 136 | included in all copies or substantial portions of the Software. 137 | 138 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 139 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 140 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 141 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 142 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 143 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 144 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 145 | OTHER DEALINGS IN THE SOFTWARE. 146 | 147 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "lastguest/dynamic-string", 3 | "type": "library", 4 | "description": "Template based random string generator", 5 | "keywords": ["generator","template","random","grammar"], 6 | "homepage": "https://github.com/lastguest/DynamicString", 7 | "version": "1.0.0", 8 | "license": "MIT", 9 | "authors": [ 10 | { 11 | "name": "Stefano Azzolini", 12 | "email": "lastguest@gmail.com" 13 | } 14 | ], 15 | "require": { 16 | "php": ">=5.4" 17 | }, 18 | "minimum-stability": "stable", 19 | "autoload": { 20 | "classmap": [ "src/" ] 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/DynamicString.php: -------------------------------------------------------------------------------- 1 | 7 | */ 8 | 9 | class DynamicString { 10 | 11 | protected $group_left, 12 | $group_right, 13 | $group_rx, 14 | $replacer; 15 | 16 | /** 17 | * Create a DynamicString instance for a specified group/separator 18 | * @param string $group A String containing an empty group envelope 19 | * @param string $separator The choice separator 20 | */ 21 | 22 | public function __construct($group='()', $separator='|'){ 23 | 24 | // Check if group delimiters are balanced. 25 | $l = strlen($group); 26 | if ($l%2) throw new Exception("Group delimiter must be balanced."); 27 | 28 | // Get left-right group delimiter 29 | list($this->group_left, $this->group_right) = str_split($group, $l>>1); 30 | 31 | // Build group regular expression 32 | $this->group_rx = 33 | "(\\".$this->group_left 34 | ."([^" 35 | .$this->group_left.$this->group_right 36 | ."]+)\\".$this->group_right 37 | .")x"; 38 | 39 | $this->replacer = function($m) use ($separator){ 40 | // Get one random choice 41 | $choice = array_rand($options = explode($separator, $m[1]),1); 42 | return $options[$choice]; 43 | }; 44 | } 45 | 46 | /** 47 | * Render a new randomized instance of passed template. 48 | * @param string $template The generator template 49 | * @return string The random rendered version 50 | */ 51 | 52 | public function render($template){ 53 | 54 | // While there are groups, resolve them 55 | while (false !== strpos($template, $this->group_left)) { 56 | $template = preg_replace_callback($this->group_rx, $this->replacer, $template); 57 | } 58 | 59 | // Return rendered template 60 | return $template; 61 | } 62 | 63 | } 64 | --------------------------------------------------------------------------------