Skip to content

Instantly share code, notes, and snippets.

@mingalevme
Last active September 29, 2023 21:27
Show Gist options
  • Select an option

  • Save mingalevme/04702bb7e5e361448cbe44cb7b3895d5 to your computer and use it in GitHub Desktop.

Select an option

Save mingalevme/04702bb7e5e361448cbe44cb7b3895d5 to your computer and use it in GitHub Desktop.

Revisions

  1. mingalevme renamed this gist Aug 7, 2019. 1 changed file with 1 addition and 1 deletion.
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    class GoogleMapsParamDeserializer
    class GoogleMapsQueryArgsDeserializer
    {
    public static function deserialize(string $input): array
    {
  2. mingalevme created this gist Aug 7, 2019.
    50 changes: 50 additions & 0 deletions GoogleParamsDeserializer.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    class GoogleMapsParamDeserializer
    {
    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;
    }
    }