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

Fixes #1906 - setOption('filter_path', 'responses.hits.hits._source'); #1907

Open
wants to merge 1 commit into
base: 8.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions src/Multi/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Elastica\Multi;

use Elastica\Client;
use Elastica\Exception\InvalidException;
use Elastica\JSON;
use Elastica\Request;
use Elastica\Search as BaseSearch;
Expand Down Expand Up @@ -54,6 +55,113 @@ public function __construct(Client $client, ?MultiBuilderInterface $builder = nu
$this->_builder = $builder ?? new MultiBuilder();
$this->_client = $client;
}

/**
* @param string $key
* @param mixed $value
*
* @return $this
*/
public function setOption($key, $value)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function setOption($key, $value)
public function setOption(string $key, $value): self

Argument $key can probably be type-hinted

{
$this->_validateOption($key);

$this->_options[$key] = $value;

return $this;
}

/**
* @param array $options
*
* @return $this
*/
public function setOptions(array $options)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function setOptions(array $options)
public function setOptions(array $options): self

{
$this->clearOptions();

foreach ($options as $key => $value) {
$this->setOption($key, $value);
}

return $this;
}

/**
* @return $this
*/
public function clearOptions()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function clearOptions()
public function clearOptions(): self

{
$this->_options = [];

return $this;
}

/**
* @param string $key
* @param mixed $value
*
* @return $this
*/
public function addOption($key, $value)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function addOption($key, $value)
public function addOption(string $key, $value): self

{
$this->_validateOption($key);

$this->_options[$key][] = $value;

return $this;
}

/**
* @param string $key
*
* @return bool
*/
public function hasOption($key)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function hasOption($key)
public function hasOption(string $key): bool

{
return isset($this->_options[$key]);
}

/**
* @param string $key
*
* @throws \Elastica\Exception\InvalidException
*
* @return mixed
*/
public function getOption($key)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function getOption($key)
public function getOption(string $key)

{
if (!$this->hasOption($key)) {
throw new InvalidException('Option '.$key.' does not exist');
}

return $this->_options[$key];
}

/**
* @return array
*/
public function getOptions()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function getOptions()
public function getOptions(): array

{
return $this->_options;
}

/**
* @param string $key
*
* @throws \Elastica\Exception\InvalidException
*
* @return bool
*/
protected function _validateOption($key)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
protected function _validateOption($key)
protected function _validateOption(string $key): bool

{
switch ($key) {
case BaseSearch::OPTION_FILTER_PATH:
return true;
}

throw new InvalidException('Invalid option '.$key);
}

public function getClient(): Client
{
Expand Down