Last active
September 29, 2023 21:27
-
-
Save mingalevme/04702bb7e5e361448cbe44cb7b3895d5 to your computer and use it in GitHub Desktop.
Decoding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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