| 1 |
<?php |
| 2 |
if (!class_exists('AccuaConditionalReplacer')) { |
| 3 |
class AccuaConditionalReplacer { |
| 4 |
protected $map = array(); |
| 5 |
protected $search = null; |
| 6 |
protected $replace = null; |
| 7 |
protected $matches = null; |
| 8 |
|
| 9 |
function __construct(array $map = array()){ |
| 10 |
$this->map = $map; |
| 11 |
} |
| 12 |
|
| 13 |
function appendPattern(array $map = array()){ |
| 14 |
$this->map = $map + $this->map; |
| 15 |
$this->search = null; |
| 16 |
$this->replace = null; |
| 17 |
$this->matches = null; |
| 18 |
} |
| 19 |
|
| 20 |
function setPattern(array $map = array()){ |
| 21 |
$this->map = $map; |
| 22 |
$this->search = null; |
| 23 |
$this->replace = null; |
| 24 |
$this->matches = null; |
| 25 |
} |
| 26 |
|
| 27 |
function doReplace($subject) { |
| 28 |
if ($this->matches === null){ |
| 29 |
$this->search = array(); |
| 30 |
$this->replace = array(); |
| 31 |
$this->matches = array(); |
| 32 |
foreach ($this->map as $s => $r) { |
| 33 |
$s = (string) $s; |
| 34 |
$r = (string) $r; |
| 35 |
$this->search[] = '{'.$s.'}'; |
| 36 |
$this->replace[] = $r; |
| 37 |
if (trim($r) !== '') { |
| 38 |
$this->matches[$s] = true; |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
return str_replace($this->search, $this->replace, preg_replace_callback( |
| 44 |
// /\[\s*newsletter_if\s*(\{[^\}]*\})\s*(then)?\s*("[^"]*"|'[^']*')\s*((else)?\s*("[^"]*"|'[^']*'))?\]/ims |
| 45 |
//'/\\[\\s*newsletter_if\\s*(\\{[^\\}]*\\})\\s*(then)?\\s*("[^"]*"|\'[^\']*\')\\s*((else)?\\s*("[^"]*"|\'[^\']*\'))?\\]/ims', |
| 46 |
// /\[\s*newsletter_if\s*(\{[^\}]*\})\s*(then|\?)?\s*("[^"]*"|'[^']*'|`[^`]*`|\[[^\]]*\])\s*((else|\:)?\s*("[^"]*"|'[^']*'|`[^`]*`|\[[^\]]*\]))?\s*\]/ims |
| 47 |
'/\\[\\s*(accua_if|newsletter_if|form_if)\\s*\\{([^\\}]*)\\}\\s*(then|\\?)?\\s*("[^"]*"|\'[^\']*\'|`[^`]*`|\\[[^\\]]*\\])\\s*((else|\\:)?\\s*("[^"]*"|\'[^\']*\'|`[^`]*`|\\[[^\\]]*\\]))?\\s*\\]/is', |
| 48 |
array($this, '_doReplaceMatch'), |
| 49 |
$subject |
| 50 |
)); |
| 51 |
|
| 52 |
} |
| 53 |
|
| 54 |
function _doReplaceMatch($match){ |
| 55 |
if (isset($match[2]) && isset($this->matches[$match[2]])){ |
| 56 |
@ $ret = $match[4]; |
| 57 |
} else { |
| 58 |
@ $ret = $match[7]; |
| 59 |
} |
| 60 |
if (is_string($ret) && strlen($ret) > 2){ |
| 61 |
return substr($ret,1,-1); |
| 62 |
} |
| 63 |
return ''; |
| 64 |
} |
| 65 |
} |
| 66 |
} |