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: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194:
<?php
class UrlSerializedParameterParser
{
private static $param_name_regex = '`^([a-z][a-z0-9-]*):`iu';
private static $escape_char = '\\';
private static $parameter_separator = ',';
private static $composed_parameter_start_char = '{';
private static $composed_parameter_end_char = '}';
private $args;
private $args_length;
private $args_index = 0;
private $parameters = array();
public function __construct($args)
{
$this->args = $args;
$this->args_length = TextHelper::strlen($this->args);
$this->parse();
}
public function get_parameters()
{
return $this->parameters;
}
private function parse()
{
while (!$this->is_ended())
{
$this->parse_next_parameter($this->parameters);
}
}
private function parse_next_parameter(array & $parameters)
{
if ($this->is_named())
{
$name = $this->parse_parameter_name();
$value = $this->parse_parameter_value();
$parameters[$name] = $value;
}
else
{
$value = $this->parse_parameter_value();
$parameters[] = $value;
}
}
private function is_named()
{
if (preg_match(self::$param_name_regex, $this->get_remaining_args()))
{
return true;
}
}
private function parse_parameter_name()
{
$matches = array();
preg_match(self::$param_name_regex, $this->get_remaining_args(), $matches);
$name = $matches[1];
$this->consume_chars(TextHelper::strlen($name) + 1);
return $name;
}
private function parse_parameter_value()
{
if ($this->is_parameter_composed())
{
return $this->parse_composed_parameter();
}
else
{
return $this->parse_simple_parameter();
}
}
private function is_parameter_composed()
{
return !$this->is_ended() && $this->assert_next_character_is(self::$composed_parameter_start_char);
}
private function parse_composed_parameter()
{
$values = array();
$this->consume_next_char();
while (!$this->is_composed_parameter_ended())
{
$this->parse_next_parameter($values);
}
$this->consume_if(self::$composed_parameter_end_char);
$this->consume_if(self::$parameter_separator);
return $values;
}
private function is_composed_parameter_ended()
{
return $this->is_ended() || $this->assert_next_character_is(self::$composed_parameter_end_char);
}
private function parse_simple_parameter()
{
$value = '';
$length = $this->get_nb_remaining_chars();
$escaped = false;
for ($i = 0; $i < $length; $i++)
{
$current = $this->consume_next_char();
if (!$escaped)
{
if ($current == self::$escape_char)
{
$escaped = true;
continue;
}
if ($current == self::$parameter_separator)
{
break;
}
if ($current == self::$composed_parameter_end_char)
{
$this->rollback_last_char_consumed();
break;
}
}
$escaped = false;
$value .= $current;
}
return $value;
}
private function consume_chars($nb_characters_to_consume)
{
$this->args_index += $nb_characters_to_consume;
}
private function consume_next_char()
{
return $this->args[$this->args_index++];
}
private function rollback_last_char_consumed()
{
$this->args_index--;
}
private function is_ended()
{
return $this->args_index >= $this->args_length;
}
private function assert_next_character_is($char)
{
return $this->args[$this->args_index] == $char;
}
private function consume_if($char)
{
if (!$this->is_ended() && $this->assert_next_character_is($char))
{
$this->consume_next_char();
}
}
private function get_nb_remaining_chars()
{
return $this->args_length - $this->args_index;
}
private function get_remaining_args()
{
return TextHelper::substr($this->args, $this->args_index);
}
private function serialize_parameters($parameters)
{
}
}
?>