| Total Complexity | 4 |
| Total Lines | 28 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 11 | class Replacer { |
||
| 12 | /** |
||
| 13 | * Replace the {values} by $params[] values in $text |
||
| 14 | * @param string $text |
||
| 15 | * @param array[] $params |
||
| 16 | * @return string |
||
| 17 | */ |
||
| 18 | public static function replace($text, $params = []) { |
||
| 19 | $allParams = $params; |
||
| 20 | return preg_replace_callback('/{([^}]+)}/', function ($m) use ($allParams) { |
||
| 21 | // skip if is not set |
||
| 22 | if(isset($allParams[$m[1]])){ |
||
| 23 | return $allParams[$m[1]]; |
||
| 24 | } |
||
| 25 | \Yii::error('Failed to replace field: '.$m[1] ,__METHOD__); |
||
| 26 | return "{".$m[1]."}"; |
||
| 27 | }, $text); |
||
| 28 | } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * get the items in {} as array |
||
| 32 | * @param $text |
||
| 33 | * @return mixed |
||
| 34 | */ |
||
| 35 | public static function getParams($text){ |
||
| 39 | } |
||
| 40 | |||
| 43 |