$value) { $acc .= "|$key#$value"; } return base64_url_encode($acc); } /** * Decodes a typed cursor. * * @param string $cursor A string emitted by `encode_cursor`. * @return array An array with the type of the cursor and an array of key-value pairs. The type is null and the map * empty if either there are no key-value pairs or the encoding is incorrect. */ function decode_cursor(string $cursor): array { $map = []; $type = ''; $acc = base64_url_decode($cursor); if ($acc === false || empty($acc)) { return [ null, [] ]; } $type = $acc[0]; foreach (explode('|', substr($acc, 2)) as $pair) { $pair = explode('#', $pair); if (count($pair) >= 2) { $key = $pair[0]; $value = $pair[1]; $map[$key] = $value; } } return [ $type, $map ]; }