|
<?php |
|
// app/code/MyNamespace/MyModule/Model/Template/Filter.php |
|
namespace MyModule\MyNamespace\Model\Template; |
|
|
|
/** |
|
* Class Filter |
|
* |
|
* @note Contains redundat code with \LS\EmailExtend\Framework\Filter\Template |
|
* @see \MyModule\MyNamespace\Framework\Filter\Template |
|
* @see \Magento\Email\Model\Template\Filter |
|
* @see \Magento\Framework\Filter\Template |
|
*/ |
|
class Filter extends \Magento\Email\Model\Template\Filter |
|
{ |
|
const CONSTRUCTION_IF_CONDITION_OPERANDS = "/([^\s=><!]+)\s*((?:>=)|(?:<=)|\>|<|(?:===)|(?:!==)|(?:==)|(?:!=)){1}\s*(.*+)/si"; |
|
|
|
/**#@+ |
|
* Construction logic regular expression |
|
*/ |
|
const CONSTRUCTION_OUTER_IF_PATTERN = '/{{outer_if\s*(.*?)}}(.*?)({{outer_else}}(.*?))?{{\\/outer_if\s*}}/si'; |
|
|
|
const CONSTRUCTION_INNER_IF_PATTERN = '/{{inner_if\s*(.*?)}}(.*?)({{inner_else}}(.*?))?{{\\/inner_if\s*}}/si'; |
|
|
|
const CONSTRUCTION_DEPEND_PATTERN = '/{{depend\s*(.*?)}}(.*?){{\\/depend\s*}}/si'; |
|
|
|
//const CONSTRUCTION_IF_PATTERN = '/{{if\s*(.*?)}}(.*?)({{else}}(.*?))?{{\\/if\s*}}/si'; |
|
|
|
/** |
|
* Add inner_if, outer_if and operands inside if(s) statements from function ifExtendedDirective |
|
* |
|
* Filter the string as template. |
|
* |
|
* @param string $value |
|
* @return string |
|
* @throws \Exception |
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity) |
|
*/ |
|
public function filter($value) |
|
{ |
|
// "outer_if" operand should be first |
|
foreach ([ |
|
self::CONSTRUCTION_OUTER_IF_PATTERN => 'ifExtendedDirective', |
|
] as $pattern => $directive) { |
|
if (preg_match_all($pattern, $value, $constructions, PREG_SET_ORDER)) { |
|
foreach($constructions as $construction) { |
|
$replacedValue = ''; |
|
$expression = null; |
|
if ($pattern == self::CONSTRUCTION_OUTER_IF_PATTERN) |
|
if (!preg_match(self::CONSTRUCTION_IF_CONDITION_OPERANDS, $construction[1], $expression)) |
|
continue; |
|
$callback = [$this, $directive]; |
|
if (!is_callable($callback)) { |
|
continue; |
|
} |
|
try { |
|
$replacedValue = call_user_func($callback, $construction, $expression); |
|
} catch (\Exception $e) { |
|
throw $e; |
|
} |
|
$value = str_replace($construction[0], $replacedValue, $value); |
|
} |
|
} |
|
} |
|
|
|
// "inner_if" operand should be second |
|
foreach ([ |
|
self::CONSTRUCTION_INNER_IF_PATTERN => 'ifExtendedDirective', |
|
] as $pattern => $directive) { |
|
if (preg_match_all($pattern, $value, $constructions, PREG_SET_ORDER)) { |
|
foreach($constructions as $construction) { |
|
$replacedValue = ''; |
|
$expression = null; |
|
if ($pattern == self::CONSTRUCTION_INNER_IF_PATTERN) |
|
if (!preg_match(self::CONSTRUCTION_IF_CONDITION_OPERANDS, $construction[1], $expression)) |
|
continue; |
|
$callback = [$this, $directive]; |
|
if (!is_callable($callback)) { |
|
continue; |
|
} |
|
try { |
|
$replacedValue = call_user_func($callback, $construction, $expression); |
|
} catch (\Exception $e) { |
|
throw $e; |
|
} |
|
$value = str_replace($construction[0], $replacedValue, $value); |
|
} |
|
} |
|
} |
|
|
|
// "depend", "if", and "template" directives should be third |
|
foreach ([ |
|
self::CONSTRUCTION_DEPEND_PATTERN => 'dependDirective', |
|
self::CONSTRUCTION_IF_PATTERN => 'ifDirective', |
|
self::CONSTRUCTION_TEMPLATE_PATTERN => 'templateDirective', |
|
] as $pattern => $directive) { |
|
if (preg_match_all($pattern, $value, $constructions, PREG_SET_ORDER)) { |
|
foreach ($constructions as $construction) { |
|
$replacedValue = ''; |
|
$expression = null; |
|
if ($pattern == self::CONSTRUCTION_IF_PATTERN) { |
|
if (preg_match(self::CONSTRUCTION_IF_CONDITION_OPERANDS, $construction[1], $expression)) { |
|
$directive = "ifExtendedDirective"; |
|
} |
|
} |
|
$callback = [$this, $directive]; |
|
if (!is_callable($callback)) { |
|
continue; |
|
} |
|
try { |
|
if ($expression != null) { |
|
$replacedValue = call_user_func($callback, $construction, $expression); |
|
} else { |
|
$replacedValue = call_user_func($callback, $construction); |
|
} |
|
} catch (\Exception $e) { |
|
throw $e; |
|
} |
|
$value = str_replace($construction[0], $replacedValue, $value); |
|
} |
|
} |
|
} |
|
|
|
// Magento 2.3+ compatibility |
|
if (method_exists($this,'filterFor')) { |
|
$value = $this->filterFor($value); |
|
} |
|
|
|
if (preg_match_all(self::CONSTRUCTION_PATTERN, $value, $constructions, PREG_SET_ORDER)) { |
|
foreach ($constructions as $construction) { |
|
$replacedValue = ''; |
|
$callback = [$this, $construction[1] . 'Directive']; |
|
if (!is_callable($callback)) { |
|
continue; |
|
} |
|
try { |
|
$replacedValue = call_user_func($callback, $construction); |
|
} catch (\Exception $e) { |
|
throw $e; |
|
} |
|
$value = str_replace($construction[0], $replacedValue, $value); |
|
} |
|
} |
|
|
|
$value = $this->afterFilter($value); |
|
return $value; |
|
} |
|
|
|
/** |
|
* Extend if directive |
|
* |
|
* @param array $construction |
|
* @param array $expression |
|
* @return string |
|
*/ |
|
public function ifExtendedDirective($construction, $expression) |
|
{ |
|
if (count($this->templateVars) == 0) { |
|
return $construction[0]; |
|
} |
|
|
|
$expression[3] = trim($expression[3], "'"); |
|
$expression[3] = trim($expression[3], "\""); |
|
|
|
switch ($expression[2]) { |
|
case '>=': |
|
$condition = ($this->getVariable($expression[1], '') >= $expression[3]); |
|
break; |
|
case '<=': |
|
$condition = ($this->getVariable($expression[1], '') <= $expression[3]); |
|
break; |
|
case '>': |
|
$condition = ($this->getVariable($expression[1], '') > $expression[3]); |
|
break; |
|
case '<': |
|
$condition = ($this->getVariable($expression[1], '') < $expression[3]); |
|
break; |
|
case '===': |
|
$condition = ($this->getVariable($expression[1], '') === $expression[3]); |
|
break; |
|
case '!==': |
|
$condition = ($this->getVariable($expression[1], '') !== $expression[3]); |
|
break; |
|
case '==': |
|
$condition = ($this->getVariable($expression[1], '') == $expression[3]); |
|
break; |
|
case '!=': |
|
$condition = ($this->getVariable($expression[1], '') != $expression[3]); |
|
break; |
|
default: |
|
$condition = false; |
|
} |
|
|
|
if ($condition) { |
|
return $construction[2]; |
|
} else { |
|
if (isset($construction[3]) && isset($construction[4])) { |
|
return $construction[4]; |
|
} else { |
|
return ''; |
|
} |
|
} |
|
} |
|
} |