Classes

File util/StringVars.class.php

File util/StringVars.class.php

 1:  2:  3:  4:  5:  6:  7:  8:  9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 
<?php
/**
 * Implements the string var replacement method
 * @package     Util
 * @copyright   &copy; 2005-2019 PHPBoost
 * @license     https://www.gnu.org/licenses/gpl-3.0.html GNU/GPL-3.0
 * @author      Loic ROUCHON <horn@phpboost.com>
 * @version     PHPBoost 5.2 - last update: 2016 11 14
 * @since       PHPBoost 3.0 - 2009 10 15
 * @contributor mipel <mipel@phpboost.com>
*/

class StringVars
{
    private $parameters;
    private $strict;

    public static function replace_vars($string, array $parameters, $strict = false)
    {
        if (empty($parameters))
        {
            return $string;
        }
        $string_var = new StringVars($strict);
        return $string_var->replace($string, $parameters);
    }

    public function __construct($strict = false)
    {
        $this->strict = $strict;
    }

    public function replace($string, array $parameters)
    {
        $this->parameters = $parameters;
        return preg_replace_callback('`:([a-z][\w_]+)`iu', array($this, 'replace_var'), $string);
    }

    private function replace_var($captures)
    {
        $varname =& $captures[1];
        if (array_key_exists($varname, $this->parameters))
        {
            return $this->set_var($this->parameters[$varname]);
        }
        if ($this->strict)
        {
            throw new RemainingStringVarException($varname);
        }
        else
        {
            return ':' . $varname;
        }
    }

    protected function set_var($parameter)
    {
        return $parameter;
    }
}
?>