2012-04-03 22:47:41 +02:00
|
|
|
<?php
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
namespace PhpParser;
|
|
|
|
|
2014-03-27 12:51:13 +01:00
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
*/
|
2014-02-06 14:44:16 +01:00
|
|
|
class Template
|
2012-04-03 22:47:41 +02:00
|
|
|
{
|
|
|
|
protected $parser;
|
|
|
|
protected $template;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new code template from a template string.
|
|
|
|
*
|
2014-02-06 14:44:16 +01:00
|
|
|
* @param Parser $parser A parser instance
|
|
|
|
* @param string $template The template string
|
2012-04-03 22:47:41 +02:00
|
|
|
*/
|
2014-02-06 14:44:16 +01:00
|
|
|
public function __construct(Parser $parser, $template) {
|
2012-04-03 22:47:41 +02:00
|
|
|
$this->parser = $parser;
|
|
|
|
$this->template = $template;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the statements of the template with the passed in placeholders
|
|
|
|
* replaced.
|
|
|
|
*
|
|
|
|
* @param array $placeholders Placeholders
|
|
|
|
*
|
2014-02-06 14:44:16 +01:00
|
|
|
* @return Node[] Statements
|
2012-04-03 22:47:41 +02:00
|
|
|
*/
|
|
|
|
public function getStmts(array $placeholders) {
|
|
|
|
return $this->parser->parse(
|
2012-04-25 20:04:46 +02:00
|
|
|
$this->getTemplateWithPlaceholdersReplaced($placeholders)
|
2012-04-03 22:47:41 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getTemplateWithPlaceholdersReplaced(array $placeholders) {
|
|
|
|
if (empty($placeholders)) {
|
|
|
|
return $this->template;
|
|
|
|
}
|
|
|
|
|
|
|
|
return strtr($this->template, $this->preparePlaceholders($placeholders));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prepare the placeholders for replacement. This means that
|
|
|
|
* a) all placeholders will be surrounded with __.
|
|
|
|
* b) ucfirst/lcfirst variations of the placeholders are generated.
|
|
|
|
*
|
|
|
|
* E.g. for an input array of ['foo' => 'bar'] the result will be
|
|
|
|
* ['__foo__' => 'bar', '__Foo__' => 'Bar'].
|
|
|
|
*/
|
|
|
|
protected function preparePlaceholders(array $placeholders) {
|
|
|
|
$preparedPlaceholders = array();
|
|
|
|
|
|
|
|
foreach ($placeholders as $name => $value) {
|
|
|
|
$preparedPlaceholders['__' . $name . '__'] = $value;
|
|
|
|
|
|
|
|
if (ctype_lower($name[0])) {
|
|
|
|
$ucfirstName = ucfirst($name);
|
|
|
|
if (!isset($placeholders[$ucfirstName])) {
|
|
|
|
$preparedPlaceholders['__' . $ucfirstName . '__'] = ucfirst($value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctype_upper($name[0])) {
|
|
|
|
$lcfirstName = lcfirst($name);
|
|
|
|
if (!isset($placeholders[$lcfirstName])) {
|
|
|
|
$preparedPlaceholders['__' . $lcfirstName . '__'] = lcfirst($value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $preparedPlaceholders;
|
|
|
|
}
|
|
|
|
}
|