Classes

File util/UrlSerializedParameterEncoder.class.php

File util/UrlSerializedParameterEncoder.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: 63: 64: 65: 66: 
<?php
/**
 * @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 - 2010 02 28
 * @contributor mipel <mipel@phpboost.com>
*/

class UrlSerializedParameterEncoder
{
    public static function encode(array $parameters)
    {
        return self::encode_array_values($parameters);
    }

    private static function encode_array_values(array $array)
    {
        $serialized_parameters = array();
        foreach ($array as $key => $value)
        {
            $serialized_parameters[] = self::encode_parameter($key, $value);
        }
        return join(',', $serialized_parameters);
    }

    private static function encode_array(array $array)
    {
        return '{' . self::encode_array_values($array) . '}';
    }

    private static function encode_parameter($key, $value)
    {
        return self::encode_name($key) . self::encode_value($value);
    }

    private static function encode_name($key)
    {
        if (is_string($key) && preg_match('`^[a-z][a-z0-9]*$`iu', $key))
        {
            return $key . ':';
        }
        return '';
    }

    private static function encode_value($value)
    {
        if (is_array($value))
        {
            return self::encode_array($value);
        }
        else
        {
            return self::encode_string($value);
        }
    }

    private static function encode_string($value)
    {
        return addcslashes((string) $value, ':{}\\,');
    }
}
?>