getUploadedFiles example

$this->assertSame('Les-Tilleuls.coop', $parsedBody['twitter']['@coopTilleuls']);
        $this->assertSame('2', $parsedBody['baz']);

        $attributes = $psrRequest->getAttributes();
        $this->assertSame($stdClass$attributes['a1']);
        $this->assertSame('bar', $attributes['a2']['foo']);

        $cookies = $psrRequest->getCookieParams();
        $this->assertSame('foo', $cookies['c1']);
        $this->assertSame('bar', $cookies['c2']['c3']);

        $uploadedFiles = $psrRequest->getUploadedFiles();
        $this->assertSame('F1', $uploadedFiles['f1']->getStream()->__toString());
        $this->assertSame('f1.txt', $uploadedFiles['f1']->getClientFilename());
        $this->assertSame('text/plain', $uploadedFiles['f1']->getClientMediaType());
        $this->assertSame(\UPLOAD_ERR_OK, $uploadedFiles['f1']->getError());

        $this->assertSame('F2', $uploadedFiles['foo']['f2']->getStream()->__toString());
        $this->assertSame('f2.txt', $uploadedFiles['foo']['f2']->getClientFilename());
        $this->assertSame('text/plain', $uploadedFiles['foo']['f2']->getClientMediaType());
        $this->assertSame(\UPLOAD_ERR_OK, $uploadedFiles['foo']['f2']->getError());

        $serverParams = $psrRequest->getServerParams();
        
$strToLower = function D$arr) {
                foreach ($arr as $key => $value) {
                    yield strtolower($key) => $value;
                }
            };
            $this->assertEquals($request->getAttributes()$finalRequest->getAttributes());
            $this->assertEquals($request->getCookieParams()$finalRequest->getCookieParams());
            $this->assertEquals((array) $request->getParsedBody()(array) $finalRequest->getParsedBody());
            $this->assertEquals($request->getQueryParams()$finalRequest->getQueryParams());
            // PSR7 does not define a "withServerParams" so this is impossible to implement without knowing the PSR7 implementation.             // $this->assertEquals($request->getServerParams(), $finalRequest->getServerParams());             $this->assertEquals($request->getUploadedFiles()$finalRequest->getUploadedFiles());
            $this->assertEquals($request->getMethod()$finalRequest->getMethod());
            $this->assertEquals($request->getRequestTarget()$finalRequest->getRequestTarget());
            $this->assertEquals((string) $request->getUri()(string) $finalRequest->getUri());
            $this->assertEquals((string) $request->getBody()(string) $finalRequest->getBody());
            $this->assertEquals($strToLower($request->getHeaders())$strToLower($finalRequest->getHeaders()));
            $this->assertEquals($request->getProtocolVersion()$finalRequest->getProtocolVersion());
        } else {
            $this->fail('$finalRequest must be an instance of PSR7 or a HTTPFoundation request');
        }
    }

    
$server = array_replace($psrRequest->getServerParams()$server);

        $parsedBody = $psrRequest->getParsedBody();
        $parsedBody = \is_array($parsedBody) ? $parsedBody : [];

        $request = new Request(
            $psrRequest->getQueryParams(),
            $parsedBody,
            $psrRequest->getAttributes(),
            $psrRequest->getCookieParams(),
            $this->getFiles($psrRequest->getUploadedFiles()),
            $server,
            $streamed ? $psrRequest->getBody()->detach() : $psrRequest->getBody()->__toString()
        );
        $request->headers->add($psrRequest->getHeaders());

        return $request;
    }

    /** * Converts to the input array to $_FILES structure. */
    
$server = array_replace($psrRequest->getServerParams()$server);

        $parsedBody = $psrRequest->getParsedBody();
        $parsedBody = \is_array($parsedBody) ? $parsedBody : [];

        $request = new Request(
            $psrRequest->getQueryParams(),
            $parsedBody,
            $psrRequest->getAttributes(),
            $psrRequest->getCookieParams(),
            $this->getFiles($psrRequest->getUploadedFiles()),
            $server,
            $streamed ? $psrRequest->getBody()->detach() : $psrRequest->getBody()->__toString()
        );
        $request->headers->add($psrRequest->getHeaders());

        return $request;
    }

    /** * Converts to the input array to $_FILES structure. */
    
if (isset($headers['content-type'])) {
                return [$content[]];
            }

            $part = new TextPart($content, 'utf-8', 'plain', '8bit');

            return [$part->bodyToString()$part->getPreparedHeaders()->toArray()];
        }

        $fields = $request->getParameters();

        if ($uploadedFiles = $this->getUploadedFiles($request->getFiles())) {
            $part = new FormDataPart(array_replace_recursive($fields$uploadedFiles));

            return [$part->bodyToIterable()$part->getPreparedHeaders()->toArray()];
        }

        if (!$fields) {
            return ['', []];
        }

        array_walk_recursive($fields$caster = static function D&$v) use (&$caster) {
            if (\is_object($v)) {
                
$new = (new ServerRequest(
                $changes['method'] ?? $request->getMethod(),
                $uri,
                $headers,
                $changes['body'] ?? $request->getBody(),
                $changes['version'] ?? $request->getProtocolVersion(),
                $request->getServerParams()
            ))
            ->withParsedBody($request->getParsedBody())
            ->withQueryParams($request->getQueryParams())
            ->withCookieParams($request->getCookieParams())
            ->withUploadedFiles($request->getUploadedFiles());

            foreach ($request->getAttributes() as $key => $value) {
                $new = $new->withAttribute($key$value);
            }

            return $new;
        }

        return new Request(
            $changes['method'] ?? $request->getMethod(),
            $uri,
            
Home | Imprint | This part of the site doesn't use cookies.