ApiPlatform\Core\Util\RequestParser::parseRequestParams PHP Method

parseRequestParams() public static method

Parses request parameters from the specified source.
See also: http://stackoverflow.com/a/18209799/1529493
Author: Rok Kralj
public static parseRequestParams ( string $source ) : array
$source string
return array
    public static function parseRequestParams(string $source) : array
    {
        // '[' is urlencoded in the input, but we must urldecode it in order
        // to find it when replacing names with the regexp below.
        $source = str_replace(urlencode('['), '[', $source);
        $source = preg_replace_callback('/(^|(?<=&))[^=[&]+/', function ($key) {
            return bin2hex(urldecode($key[0]));
        }, $source);
        // parse_str urldecodes both keys and values in resulting array.
        parse_str($source, $params);
        return array_combine(array_map('hex2bin', array_keys($params)), $params);
    }

Usage Example

Example #1
0
 /**
  * @dataProvider parseRequestParamsProvider
  */
 public function testParseRequestParams($source, $expected)
 {
     $actual = RequestParser::parseRequestParams($source);
     $this->assertEquals($expected, $actual);
 }