/** * Create a request from the supplied superglobal values. * * If any argument is not supplied, the corresponding superglobal value will * be used. * * The ServerRequest created is then passed to the fromServer() method in * order to marshal the request URI and headers. * * @see fromServer() * * @param array $server The $_SERVER superglobal variable. * @param array $query The $_GET superglobal variable. * @param array $parsedBody The $_POST superglobal variable. * @param array $cookies The $_COOKIE superglobal variable. * @param array $files The $_FILES superglobal variable. * * @return ServerRequestInterface * * @throws \InvalidArgumentException for invalid file values */ public static function createFromGlobals(array $server = array(), array $query = array(), array $parsedBody = null, array $cookies = array(), array $files = array()) { $server = static::prepareServers($server ?: $_SERVER); $headers = static::prepareHeaders($server); $body = new PhpInputStream(); $method = ServerHelper::getValue($server, 'REQUEST_METHOD', 'GET'); $decodedBody = $_POST; $decodedFiles = $_FILES; if (in_array(strtoupper($method), array('PUT', 'PATCH', 'DELETE', 'LINK', 'UNLINK'))) { $type = HeaderHelper::getValue($headers, 'Content-Type'); if (strpos($type, 'application/x-www-form-urlencoded') !== false) { parse_str($body->__toString(), $decodedBody); } elseif (strpos($type, 'multipart/form-data') !== false) { list($decodedBody, $decodedFiles) = array_values(ServerHelper::parseFormData($body->__toString())); } } $files = static::prepareFiles($files ?: $decodedFiles); return new ServerRequest($server, $files, static::prepareUri($server, $headers), $method, $body, $headers, $cookies ?: $_COOKIE, $query ?: $_GET, $parsedBody ?: $decodedBody, static::getProtocolVersion($server)); }
/** * testParseFormData * * @return void * * @covers \Windwalker\Http\Helper\ServerHelper::parseFormData */ public function testParseFormData() { $type = 'multipart/form-data; boundary=----WebKitFormBoundary8zi5vcW6H9OgqKSj'; $input = <<<DATA ------WebKitFormBoundary8zi5vcW6H9OgqKSj Content-Disposition: form-data; name="flower" SAKURA ------WebKitFormBoundary8zi5vcW6H9OgqKSj Content-Disposition: form-data; name="tree" Marabutan ------WebKitFormBoundary8zi5vcW6H9OgqKSj Content-Disposition: form-data; name="fruit" Apple ------WebKitFormBoundary8zi5vcW6H9OgqKSj-- DATA; $input = str_replace("\r\n", "\n", $input); $input = str_replace("\n", "\r\n", $input); $this->assertEquals(array('data' => array('flower' => 'SAKURA', 'tree' => 'Marabutan', 'fruit' => 'Apple'), 'files' => array()), ServerHelper::parseFormData($input)); }