Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow string for Request data #2078

Merged
merged 2 commits into from
Jun 20, 2022
Merged

Allow string for Request data #2078

merged 2 commits into from
Jun 20, 2022

Conversation

franmomu
Copy link
Contributor

Looking at the Client::request:

Elastica/src/Client.php

Lines 500 to 509 in 682b78d

* @param array|string $data OPTIONAL Arguments as array or pre-encoded string
* @param array $query OPTIONAL Query params
* @param string $contentType Content-Type sent with this request
*
* @throws Exception\ClientException|Exception\ConnectionException
*/
public function request(string $path, string $method = Request::GET, $data = [], array $query = [], string $contentType = Request::DEFAULT_CONTENT_TYPE): Response
{
$connection = $this->getConnection();
$request = $this->_lastRequest = new Request($path, $method, $data, $query, $connection, $contentType);

It uses array|string for the data that is passed to Request.

And also in Http transport:

$data = $request->getData();
$httpMethod = $request->getMethod();
$headers[] = 'Content-Type: '.$request->getContentType();
if (!empty($data)) {
if ($this->hasParam('postWithRequestBody') && true == $this->getParam('postWithRequestBody')) {
$httpMethod = Request::POST;
}
if (\is_array($data)) {
$content = JSON::stringify($data, \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES);
} else {
$content = $data;
// Escaping of / not necessary. Causes problems in base64 encoding of files
$content = \str_replace('\/', '/', $content);
}

It treats data as a string|array.

PHPStan found this at level 4:

 ------ -----------------------------------------------------------------------
  Line   src/Transport/Http.php
 ------ -----------------------------------------------------------------------
  132    Else branch is unreachable because previous condition is always true.
 ------ -----------------------------------------------------------------------

@ruflin ruflin merged commit ea9fbd6 into ruflin:master Jun 20, 2022
@franmomu franmomu deleted the data_as_string branch June 20, 2022 11:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants