%PDF- <> %âãÏÓ endobj 2 0 obj <> endobj 3 0 obj <>/ExtGState<>/ProcSet[/PDF/Text/ImageB/ImageC/ImageI] >>/Annots[ 28 0 R 29 0 R] /MediaBox[ 0 0 595.5 842.25] /Contents 4 0 R/Group<>/Tabs/S>> endobj ºaâÚÎΞ-ÌE1ÍØÄ÷{òò2ÿ ÛÖ^ÔÀá TÎ{¦?§®¥kuµùÕ5sLOšuY>endobj 2 0 obj<>endobj 2 0 obj<>endobj 2 0 obj<>endobj 2 0 obj<> endobj 2 0 obj<>endobj 2 0 obj<>es 3 0 R>> endobj 2 0 obj<> ox[ 0.000000 0.000000 609.600000 935.600000]/Fi endobj 3 0 obj<> endobj 7 1 obj<>/ProcSet[/PDF/Text/ImageB/ImageC/ImageI]>>/Subtype/Form>> stream
<?php
namespace Codeception\Util;
/**
* Basic template engine used for generating initial Cept/Cest/Test files.
*/
class Template
{
protected $template;
protected $vars = [];
protected $placeholderStart;
protected $placeholderEnd;
/**
* Takes a template string
*
* @param $template
*/
public function __construct($template, $placeholderStart = '{{', $placeholderEnd = '}}')
{
$this->template = $template;
$this->placeholderStart = $placeholderStart;
$this->placeholderEnd = $placeholderEnd;
}
/**
* Replaces {{var}} string with provided value
*
* @param $var
* @param $val
* @return $this
*/
public function place($var, $val)
{
$this->vars[$var] = $val;
return $this;
}
/**
* Sets all template vars
*
* @param array $vars
*/
public function setVars(array $vars)
{
$this->vars = $vars;
}
/**
* Fills up template string with placed variables.
*
* @return mixed
*/
public function produce()
{
$result = $this->template;
$regex = sprintf('~%s([\w\.]+)%s~m', $this->placeholderStart, $this->placeholderEnd);
$matched = preg_match_all($regex, $result, $matches, PREG_SET_ORDER);
if (!$matched) {
return $result;
}
foreach ($matches as $match) { // fill in placeholders
$placeholder = $match[1];
$value = $this->vars;
foreach (explode('.', $placeholder) as $segment) {
if (is_array($value) && array_key_exists($segment, $value)) {
$value = $value[$segment];
} else {
continue 2;
}
}
$result = str_replace($this->placeholderStart . $placeholder . $this->placeholderEnd, $value, $result);
}
return $result;
}
}