Skip to content
Snippets Groups Projects
Commit a68b704d authored by Laurent Destailleur's avatar Laurent Destailleur
Browse files

FIX Must not return error when vars does not exists

parent 80e10158
No related branches found
No related tags found
No related merge requests found
...@@ -82,9 +82,55 @@ class Segment implements IteratorAggregate, Countable ...@@ -82,9 +82,55 @@ class Segment implements IteratorAggregate, Countable
*/ */
public function merge() public function merge()
{ {
$this->xmlParsed .= str_replace(array_keys($this->vars), array_values($this->vars), $this->xml); // We concat all file, later we will // To provide debug information on line number processed
// Note: To change style of table-row, we should make a replace here to add attribute global $count;
// table:style-name="Table4.A2" if (empty($count)) $count=1;
else $count++;
if (empty($this->savxml)) $this->savxml = $this->xml; // Sav content of line at first line merged, so we will reuse original for next steps
$this->xml = $this->savxml;
$tmpvars = $this->vars; // Store into $tmpvars so we won't modify this->vars when completing data with empty values
// 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';
preg_match_all($reg, $this->xml, $matches, PREG_SET_ORDER);
//var_dump($tmpvars);exit;
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]]))
{
$tmpvars[$match[1]] = ''; // Not defined, so we set it to '', we just need entry into this->vars for next loop
}
}
// 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
foreach($tmpvars as $key => $value)
{
// If value is true (not 0 nor false nor null nor empty string)
if ($value)
{
// Remove the IF tag
$this->xml = str_replace('[!-- IF '.$key.' --]', '', $this->xml);
// Remove everything between the ELSE tag (if it exists) and the ENDIF tag
$reg = '@(\[!--\sELSE\s' . $key . '\s--\](.*))?\[!--\sENDIF\s' . $key . '\s--\]@smU'; // U modifier = all quantifiers are non-greedy
$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
{
// 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
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
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)
$this->xml = preg_replace($reg, '', $this->xml);
}
}
$this->xmlParsed .= str_replace(array_keys($tmpvars), array_values($tmpvars), $this->xml);
if ($this->hasChildren()) { if ($this->hasChildren()) {
foreach ($this->children as $child) { foreach ($this->children as $child) {
$this->xmlParsed = str_replace($child->xml, ($child->xmlParsed=="")?$child->merge():$child->xmlParsed, $this->xmlParsed); $this->xmlParsed = str_replace($child->xml, ($child->xmlParsed=="")?$child->merge():$child->xmlParsed, $this->xmlParsed);
...@@ -103,6 +149,7 @@ class Segment implements IteratorAggregate, Countable ...@@ -103,6 +149,7 @@ class Segment implements IteratorAggregate, Countable
} }
} }
$this->file->close(); $this->file->close();
return $this->xmlParsed; return $this->xmlParsed;
} }
/** /**
...@@ -136,7 +183,7 @@ class Segment implements IteratorAggregate, Countable ...@@ -136,7 +183,7 @@ class Segment implements IteratorAggregate, Countable
public function setVars($key, $value, $encode = true, $charset = 'ISO-8859') public function setVars($key, $value, $encode = true, $charset = 'ISO-8859')
{ {
if (strpos($this->xml, $this->odf->getConfig('DELIMITER_LEFT') . $key . $this->odf->getConfig('DELIMITER_RIGHT')) === false) { if (strpos($this->xml, $this->odf->getConfig('DELIMITER_LEFT') . $key . $this->odf->getConfig('DELIMITER_RIGHT')) === false) {
throw new SegmentException("var $key not found in {$this->getName()}"); //throw new SegmentException("var $key not found in {$this->getName()}");
} }
$value=$this->odf->htmlToUTFAndPreOdf($value); $value=$this->odf->htmlToUTFAndPreOdf($value);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment