context = adp_context(); $this->template = ""; $this->availableReplacements = array(); } public function withContext(Context $context) { $this->context = $context; } /** * @param string $template */ public function setTemplate($template) { if ( ! is_string($template)) { return; } $this->template = $template; $this->availableReplacements = array(); if (preg_match_all("/{{([^ {}]+)}}/", $template, $matches) !== false) { if (isset($matches[1]) && is_array($matches[1])) { $this->availableReplacements = $matches[1]; } } } /** * @return string */ public function getTemplate() { return $this->template; } /** * @return array */ public function getAvailableReplacements() { return $this->availableReplacements; } /** * @param array $replacements * * @return string */ public function applyReplacements($replacements) { if ( ! is_array($replacements)) { return ""; } $newReplacements = array(); foreach ($this->availableReplacements as $key) { if ( ! isset($replacements[$key])) { $replacements[$key] = ""; } $newReplacements["{{" . $key . "}}"] = $replacements[$key]; } return str_replace(array_keys($newReplacements), array_values($newReplacements), $this->template); } }