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

Use array_key_first() when judicious #2029

Open
wants to merge 1 commit into
base: 8.x
Choose a base branch
from
Open
Show file tree
Hide file tree
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
43 changes: 15 additions & 28 deletions src/Bulk.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,31 +202,21 @@ public function addData($data, ?string $opType = null)
*/
public function addRawData(array $data): self
{
$action = null;

foreach ($data as $row) {
if (\is_array($row)) {
$opType = \key($row);
$metadata = \reset($row);
if (Action::isValidOpType($opType)) {
// add previous action
if (isset($action)) {
$this->addAction($action);
}
$action = new Action($opType, $metadata);
} elseif (isset($action)) {
$action->setSource($row);
$this->addAction($action);
$action = null;
} else {
throw new InvalidException('Invalid bulk data, source must follow action metadata');
}
} else {
if (!\is_array($row) || null === ($opType = \array_key_first($row))) {
throw new InvalidException('Invalid bulk data, should be array of array, Document or Bulk/Action');
}
}

// add last action if available
if (isset($action)) {
$this->addAction($action);
if (Action::isValidOpType($opType)) {
Copy link
Owner

Choose a reason for hiding this comment

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

Nice rewrite. This old code was way too complicated!

$action = new Action($opType, $row[$opType]);
$this->addAction($action);
} elseif (null !== $action && !$action->hasSource()) {
$action->setSource($row);
} else {
throw new InvalidException('Invalid bulk data, source must follow action metadata');
}
}

return $this;
Expand Down Expand Up @@ -299,9 +289,7 @@ public function send(): ResponseSet
protected function _processResponse(Response $response): ResponseSet
{
$responseData = $response->getData();

$actions = $this->getActions();

$bulkResponses = [];

if (isset($responseData['items']) && \is_array($responseData['items'])) {
Expand All @@ -311,16 +299,15 @@ protected function _processResponse(Response $response): ResponseSet
}

$action = $actions[$key];

$opType = \key($item);
$bulkResponseData = \reset($item);
$opType = \array_key_first($item);
$bulkResponseData = $item[$opType];

if ($action instanceof AbstractDocumentAction) {
$data = $action->getData();
if ($data instanceof Document && $data->isAutoPopulate()
if (($data instanceof Document && $data->isAutoPopulate())
|| $this->_client->getConfigValue(['document', 'autoPopulate'], false)
) {
if (!$data->hasId() && isset($bulkResponseData['_id'])) {
if (isset($bulkResponseData['_id']) && !$data->hasId()) {
$data->setId($bulkResponseData['_id']);
}
$data->setVersionParams($bulkResponseData);
Expand Down
8 changes: 5 additions & 3 deletions src/Node/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,11 @@ public function refresh(array $params = []): Response
$this->_response = $this->getNode()->getClient()->requestEndpoint($endpoint);
$data = $this->getResponse()->getData();

$this->_data = \reset($data['nodes']);
$this->_id = \key($data['nodes']);
$this->getNode()->setId($this->getId());
if (null !== $nodeId = \array_key_first($data['nodes'])) {
Copy link
Owner

Choose a reason for hiding this comment

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

Trying to get my head around on what happened before when we didn't check furl null.

If there are no nodes, it is expected that we keep the old values or should these be overwritten with empty values?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

You're right, I didn't think this case, when nodes can be empty

$this->_data = $data['nodes'][$nodeId];
$this->_id = $nodeId;
$this->getNode()->setId($nodeId);
}

return $this->_response;
}
Expand Down
7 changes: 3 additions & 4 deletions src/Query/Fuzzy.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct(?string $fieldName = null, ?string $value = null)
*/
public function setField(string $fieldName, string $value): self
{
if (\count($this->getParams()) > 0 && \key($this->getParams()) !== $fieldName) {
if (\count($this->getParams()) > 0 && \array_key_first($this->getParams()) !== $fieldName) {
throw new InvalidException('Fuzzy query can only support a single field.');
}

Expand All @@ -51,11 +51,10 @@ public function setField(string $fieldName, string $value): self
public function setFieldOption(string $option, $value): self
{
//Retrieve the single existing field for alteration.
$params = $this->getParams();
if (\count($params) < 1) {
if (null === $key = \array_key_first($params = $this->getParams())) {
throw new InvalidException('No field has been set');
}
$key = \key($params);

$params[$key][$option] = $value;

return $this->setParam($key, $params[$key]);
Expand Down