Skip to content

Instantly share code, notes, and snippets.

@mingalevme
Last active September 29, 2023 21:27
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mingalevme/04702bb7e5e361448cbe44cb7b3895d5 to your computer and use it in GitHub Desktop.
Save mingalevme/04702bb7e5e361448cbe44cb7b3895d5 to your computer and use it in GitHub Desktop.
Decoding
class GoogleMapsQueryArgsDeserializer
{
public static function deserialize(string $input): array
{
$params = explode('!', trim($input, '!'));
foreach ($params as $i => $param) {
$params[$i] = urldecode($param);
}
return static::decode($params);
}
protected static function decode(array $params): array
{
$data = [];
for ($i=0; $i<count($params); $i++) {
$param = $params[$i];
if (preg_match('/^(\d+)m(\d+)/', $param, $matches)) {
$id = intval($matches[1]);
$length = intval($matches[2]);
$data[$id] = static::decode(array_slice($params, $i+1, $length));
$i = $i + $length;
} elseif (preg_match('/^(\d+)([fdibesuv])(.*)$/', $param, $matches)) {
$id = intval($matches[1]);
$type = $matches[2];
$value = $matches[3];
if ($type === 'i' || $type === 'e' || $type === 'u') {
$data[$id] = intval($value);
} elseif ($type === 'f') {
$data[$id] = floatval($value);
} elseif ($type === 'd') {
$data[$id] = doubleval($value);
} elseif ($type === 'b') {
$data[$id] = boolval($value);
} elseif ($type === 's' || $type === 'v') {
$data[$id] = strval($value);
}
} else {
throw new \RuntimeException('Unknown param format: ' . $param);
}
}
return $data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment