diff --git a/src/Bulk.php b/src/Bulk.php index 0688cc6da2..4b18e56992 100644 --- a/src/Bulk.php +++ b/src/Bulk.php @@ -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)) { + $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; @@ -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'])) { @@ -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); diff --git a/src/Node/Info.php b/src/Node/Info.php index cad90d4ccf..7ea6f6d59f 100644 --- a/src/Node/Info.php +++ b/src/Node/Info.php @@ -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'])) { + $this->_data = $data['nodes'][$nodeId]; + $this->_id = $nodeId; + $this->getNode()->setId($nodeId); + } return $this->_response; } diff --git a/src/Query/Fuzzy.php b/src/Query/Fuzzy.php index 520b1d081c..a7b6be2770 100644 --- a/src/Query/Fuzzy.php +++ b/src/Query/Fuzzy.php @@ -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.'); } @@ -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]);