/** * Makes PSR-7 compliant Request object from provided parameters * * @param UriInterface|string $uri * @param null|string|array $data * @param array $queryParameters * @param array $headers * @param null|string $method * @param null|string $contentType * @param null|string $contentCharset * @return RequestInterface * @throws InvalidArgumentException */ public static function make($uri, $data = null, array $queryParameters = [], array $headers = [], $method = null, $contentType = null, $contentCharset = null) { if (is_null($method)) { $method = is_null($data) ? static::METHOD_GET : static::METHOD_POST; } $uri = UriFactory::make($uri, $queryParameters); $stream = new Stream('php://memory', 'r+'); $request = new Request($uri, $method, $stream, $headers); $contentType = $contentType ?: $request->getHeaderLine('Content-Type'); $contentType = $contentType ?: static::DEFAULT_CONTENT_TYPE; $contentTypeParts = explode(';', $contentType); $mimeType = trim(array_shift($contentTypeParts)); if (is_null($data)) { $body = null; } elseif (is_string($data)) { $body = $data; } elseif (is_array($data)) { $body = static::format($data, $mimeType); } else { throw new InvalidArgumentException('Data should be either null, string or array'); } if (!is_null($body)) { $contentCharset = $contentCharset ?: static::extractCharset($contentTypeParts); $contentCharset = $contentCharset ?: static::DEFAULT_CONTENT_CHARSET; $contentTypeReassembled = $mimeType . '; charset=' . $contentCharset; $request = $request->withHeader('Content-Type', $contentTypeReassembled); $request->getBody()->write($body); } return $request; }
public function testChangeQueryParameter() { $uri = new Uri('http://www.example.com?first[a][b][c]=one&second=two'); $queryParameters = ['first' => 'different']; $newUri = UriFactory::addQueryParameters($uri, $queryParameters); $expected = 'first=different&second=two'; $this->assertSame($expected, $newUri->getQuery()); }