This commit is contained in:
Laurent Destailleur 2022-01-26 13:50:49 +01:00
parent 416543a5f5
commit 6d04bac740

View File

@ -97,23 +97,20 @@ class Segment implements IteratorAggregate, Countable
// Search all tags fou into condition to complete $tmpvars, so we will proceed all tests even if not defined // Search all tags fou into condition to complete $tmpvars, so we will proceed all tests even if not defined
$reg='@\[!--\sIF\s([{}a-zA-Z0-9\.\,_]+)\s--\]@smU'; $reg='@\[!--\sIF\s([{}a-zA-Z0-9\.\,_]+)\s--\]@smU';
$matches = array();
preg_match_all($reg, $this->xml, $matches, PREG_SET_ORDER); preg_match_all($reg, $this->xml, $matches, PREG_SET_ORDER);
//var_dump($tmpvars);exit; //var_dump($tmpvars);exit;
foreach($matches as $match) // For each match, if there is no entry into this->vars, we add it foreach ($matches as $match) { // For each match, if there is no entry into this->vars, we add it
{ if (! empty($match[1]) && ! isset($tmpvars[$match[1]])) {
if (! empty($match[1]) && ! isset($tmpvars[$match[1]]))
{
$tmpvars[$match[1]] = ''; // Not defined, so we set it to '', we just need entry into this->vars for next loop $tmpvars[$match[1]] = ''; // Not defined, so we set it to '', we just need entry into this->vars for next loop
} }
} }
// Conditionals substitution // Conditionals substitution
// Note: must be done before static substitution, else the variable will be replaced by its value and the conditional won't work anymore // Note: must be done before static substitution, else the variable will be replaced by its value and the conditional won't work anymore
foreach($tmpvars as $key => $value) foreach ($tmpvars as $key => $value) {
{
// If value is true (not 0 nor false nor null nor empty string) // If value is true (not 0 nor false nor null nor empty string)
if ($value) if ($value) {
{
// Remove the IF tag // Remove the IF tag
$this->xml = str_replace('[!-- IF '.$key.' --]', '', $this->xml); $this->xml = str_replace('[!-- IF '.$key.' --]', '', $this->xml);
// Remove everything between the ELSE tag (if it exists) and the ENDIF tag // Remove everything between the ELSE tag (if it exists) and the ENDIF tag
@ -121,12 +118,11 @@ class Segment implements IteratorAggregate, Countable
$this->xml = preg_replace($reg, '', $this->xml); $this->xml = preg_replace($reg, '', $this->xml);
} }
// Else the value is false, then two cases: no ELSE and we're done, or there is at least one place where there is an ELSE clause, then we replace it // Else the value is false, then two cases: no ELSE and we're done, or there is at least one place where there is an ELSE clause, then we replace it
else else {
{
// Find all conditional blocks for this variable: from IF to ELSE and to ENDIF // Find all conditional blocks for this variable: from IF to ELSE and to ENDIF
$reg = '@\[!--\sIF\s' . $key . '\s--\](.*)(\[!--\sELSE\s' . $key . '\s--\](.*))?\[!--\sENDIF\s' . $key . '\s--\]@smU'; // U modifier = all quantifiers are non-greedy $reg = '@\[!--\sIF\s' . $key . '\s--\](.*)(\[!--\sELSE\s' . $key . '\s--\](.*))?\[!--\sENDIF\s' . $key . '\s--\]@smU'; // U modifier = all quantifiers are non-greedy
preg_match_all($reg, $this->xml, $matches, PREG_SET_ORDER); preg_match_all($reg, $this->xml, $matches, PREG_SET_ORDER);
foreach($matches as $match) { // For each match, if there is an ELSE clause, we replace the whole block by the value in the ELSE clause foreach ($matches as $match) { // For each match, if there is an ELSE clause, we replace the whole block by the value in the ELSE clause
if (!empty($match[3])) $this->xml = str_replace($match[0], $match[3], $this->xml); if (!empty($match[3])) $this->xml = str_replace($match[0], $match[3], $this->xml);
} }
// Cleanup the other conditional blocks (all the others where there were no ELSE clause, we can just remove them altogether) // Cleanup the other conditional blocks (all the others where there were no ELSE clause, we can just remove them altogether)
@ -308,4 +304,3 @@ IMG;
return $this->xmlParsed; return $this->xmlParsed;
} }
} }