/** * Very simple 'template' parser. Replaces (for example) {name} with the value of $vars['name'] in strings * * @param $str * @param array $vars * @param string $openDelimiter * @param string $closeDelimiter * * @return string * * @example * <?php * $result = Util::template('This is the best template parser. Created by {developerName}', ['developerName' => 'Radic']); * echo $result; // This is the best template parser. Created by Radic */ public static function template($str, array $vars = [], $openDelimiter = '{', $closeDelimiter = '}') { foreach ($vars as $k => $var) { if (is_array($var)) { $str = static::template($str, $var); } elseif (is_string($var)) { $str = Str::replace($str, $openDelimiter . $k . $closeDelimiter, $var); } } return $str; }
public function testCanUseCorrectOrderForStrReplace() { $string = Str::replace('foo', 'foo', 'bar'); $this->assertEquals('bar', $string); }