@@ -5,6 +5,5 @@ |
||
| 5 | 5 | /** |
| 6 | 6 | * No entry was found in the container. |
| 7 | 7 | */ |
| 8 | -interface NotFoundExceptionInterface extends ContainerExceptionInterface |
|
| 9 | -{ |
|
| 8 | +interface NotFoundExceptionInterface extends ContainerExceptionInterface { |
|
| 10 | 9 | } |
@@ -11,7 +11,7 @@ |
||
| 11 | 11 | * Elasticsearch B.V licenses this file to you under the MIT License. |
| 12 | 12 | * See the LICENSE file in the project root for more information. |
| 13 | 13 | */ |
| 14 | -declare (strict_types=1); |
|
| 14 | +declare(strict_types=1); |
|
| 15 | 15 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\Elastic\Elasticsearch; |
| 16 | 16 | |
| 17 | 17 | use OCA\FullTextSearch_Elasticsearch\Vendor\Elastic\Elasticsearch\Response\Elasticsearch; |
@@ -26,124 +26,124 @@ |
||
| 26 | 26 | use Psr\Log\LoggerInterface; |
| 27 | 27 | final class Client implements ClientInterface |
| 28 | 28 | { |
| 29 | - const CLIENT_NAME = 'es'; |
|
| 30 | - const VERSION = '8.17.0'; |
|
| 31 | - const API_COMPATIBILITY_HEADER = '%s/vnd.elasticsearch+%s; compatible-with=8'; |
|
| 32 | - const SEARCH_ENDPOINTS = ['search', 'async_search.submit', 'msearch', 'eql.search', 'terms_enum', 'search_template', 'msearch_template', 'render_search_template', 'esql.query', 'knnSearch']; |
|
| 33 | - use ClientEndpointsTrait; |
|
| 34 | - use EndpointTrait; |
|
| 35 | - use NamespaceTrait; |
|
| 36 | - protected Transport $transport; |
|
| 37 | - protected LoggerInterface $logger; |
|
| 38 | - /** |
|
| 39 | - * Specify is the request is asyncronous |
|
| 40 | - */ |
|
| 41 | - protected bool $async = \false; |
|
| 42 | - /** |
|
| 43 | - * Enable or disable the x-elastic-meta-header |
|
| 44 | - */ |
|
| 45 | - protected bool $elasticMetaHeader = \true; |
|
| 46 | - /** |
|
| 47 | - * Enable or disable the response Exception |
|
| 48 | - */ |
|
| 49 | - protected bool $responseException = \true; |
|
| 50 | - /** |
|
| 51 | - * The endpoint namespace storage |
|
| 52 | - */ |
|
| 53 | - protected array $namespace; |
|
| 54 | - public function __construct(Transport $transport, LoggerInterface $logger) |
|
| 55 | - { |
|
| 56 | - $this->transport = $transport; |
|
| 57 | - $this->logger = $logger; |
|
| 58 | - $this->defaultTransportSettings($this->transport); |
|
| 59 | - } |
|
| 60 | - /** |
|
| 61 | - * @inheritdoc |
|
| 62 | - */ |
|
| 63 | - public function getTransport() : Transport |
|
| 64 | - { |
|
| 65 | - return $this->transport; |
|
| 66 | - } |
|
| 67 | - /** |
|
| 68 | - * @inheritdoc |
|
| 69 | - */ |
|
| 70 | - public function getLogger() : LoggerInterface |
|
| 71 | - { |
|
| 72 | - return $this->logger; |
|
| 73 | - } |
|
| 74 | - /** |
|
| 75 | - * Set the default settings for Elasticsearch |
|
| 76 | - */ |
|
| 77 | - protected function defaultTransportSettings(Transport $transport) : void |
|
| 78 | - { |
|
| 79 | - $transport->setUserAgent('elasticsearch-php', self::VERSION); |
|
| 80 | - } |
|
| 81 | - /** |
|
| 82 | - * @inheritdoc |
|
| 83 | - */ |
|
| 84 | - public function setAsync(bool $async) : self |
|
| 85 | - { |
|
| 86 | - $this->async = $async; |
|
| 87 | - return $this; |
|
| 88 | - } |
|
| 89 | - /** |
|
| 90 | - * @inheritdoc |
|
| 91 | - */ |
|
| 92 | - public function getAsync() : bool |
|
| 93 | - { |
|
| 94 | - return $this->async; |
|
| 95 | - } |
|
| 96 | - /** |
|
| 97 | - * @inheritdoc |
|
| 98 | - */ |
|
| 99 | - public function setElasticMetaHeader(bool $active) : self |
|
| 100 | - { |
|
| 101 | - $this->elasticMetaHeader = $active; |
|
| 102 | - return $this; |
|
| 103 | - } |
|
| 104 | - /** |
|
| 105 | - * @inheritdoc |
|
| 106 | - */ |
|
| 107 | - public function getElasticMetaHeader() : bool |
|
| 108 | - { |
|
| 109 | - return $this->elasticMetaHeader; |
|
| 110 | - } |
|
| 111 | - /** |
|
| 112 | - * @inheritdoc |
|
| 113 | - */ |
|
| 114 | - public function setResponseException(bool $active) : self |
|
| 115 | - { |
|
| 116 | - $this->responseException = $active; |
|
| 117 | - return $this; |
|
| 118 | - } |
|
| 119 | - /** |
|
| 120 | - * @inheritdoc |
|
| 121 | - */ |
|
| 122 | - public function getResponseException() : bool |
|
| 123 | - { |
|
| 124 | - return $this->responseException; |
|
| 125 | - } |
|
| 126 | - /** |
|
| 127 | - * @inheritdoc |
|
| 128 | - */ |
|
| 129 | - public function sendRequest(RequestInterface $request) |
|
| 130 | - { |
|
| 131 | - // If async returns a Promise |
|
| 132 | - if ($this->getAsync()) { |
|
| 133 | - if ($this->getElasticMetaHeader()) { |
|
| 134 | - $this->transport->setElasticMetaHeader(Client::CLIENT_NAME, Client::VERSION, \true); |
|
| 135 | - } |
|
| 136 | - $this->transport->setAsyncOnSuccess($request->getMethod() === 'HEAD' ? new AsyncOnSuccessNoException() : ($this->getResponseException() ? new AsyncOnSuccess() : new AsyncOnSuccessNoException())); |
|
| 137 | - return $this->transport->sendAsyncRequest($request); |
|
| 138 | - } |
|
| 139 | - if ($this->getElasticMetaHeader()) { |
|
| 140 | - $this->transport->setElasticMetaHeader(Client::CLIENT_NAME, Client::VERSION, \false); |
|
| 141 | - } |
|
| 142 | - $start = \microtime(\true); |
|
| 143 | - $response = $this->transport->sendRequest($request); |
|
| 144 | - $this->logger->info(\sprintf("Response time in %.3f sec", \microtime(\true) - $start)); |
|
| 145 | - $result = new Elasticsearch(); |
|
| 146 | - $result->setResponse($response, $request->getMethod() === 'HEAD' ? \false : $this->getResponseException()); |
|
| 147 | - return $result; |
|
| 148 | - } |
|
| 29 | + const CLIENT_NAME = 'es'; |
|
| 30 | + const VERSION = '8.17.0'; |
|
| 31 | + const API_COMPATIBILITY_HEADER = '%s/vnd.elasticsearch+%s; compatible-with=8'; |
|
| 32 | + const SEARCH_ENDPOINTS = ['search', 'async_search.submit', 'msearch', 'eql.search', 'terms_enum', 'search_template', 'msearch_template', 'render_search_template', 'esql.query', 'knnSearch']; |
|
| 33 | + use ClientEndpointsTrait; |
|
| 34 | + use EndpointTrait; |
|
| 35 | + use NamespaceTrait; |
|
| 36 | + protected Transport $transport; |
|
| 37 | + protected LoggerInterface $logger; |
|
| 38 | + /** |
|
| 39 | + * Specify is the request is asyncronous |
|
| 40 | + */ |
|
| 41 | + protected bool $async = \false; |
|
| 42 | + /** |
|
| 43 | + * Enable or disable the x-elastic-meta-header |
|
| 44 | + */ |
|
| 45 | + protected bool $elasticMetaHeader = \true; |
|
| 46 | + /** |
|
| 47 | + * Enable or disable the response Exception |
|
| 48 | + */ |
|
| 49 | + protected bool $responseException = \true; |
|
| 50 | + /** |
|
| 51 | + * The endpoint namespace storage |
|
| 52 | + */ |
|
| 53 | + protected array $namespace; |
|
| 54 | + public function __construct(Transport $transport, LoggerInterface $logger) |
|
| 55 | + { |
|
| 56 | + $this->transport = $transport; |
|
| 57 | + $this->logger = $logger; |
|
| 58 | + $this->defaultTransportSettings($this->transport); |
|
| 59 | + } |
|
| 60 | + /** |
|
| 61 | + * @inheritdoc |
|
| 62 | + */ |
|
| 63 | + public function getTransport() : Transport |
|
| 64 | + { |
|
| 65 | + return $this->transport; |
|
| 66 | + } |
|
| 67 | + /** |
|
| 68 | + * @inheritdoc |
|
| 69 | + */ |
|
| 70 | + public function getLogger() : LoggerInterface |
|
| 71 | + { |
|
| 72 | + return $this->logger; |
|
| 73 | + } |
|
| 74 | + /** |
|
| 75 | + * Set the default settings for Elasticsearch |
|
| 76 | + */ |
|
| 77 | + protected function defaultTransportSettings(Transport $transport) : void |
|
| 78 | + { |
|
| 79 | + $transport->setUserAgent('elasticsearch-php', self::VERSION); |
|
| 80 | + } |
|
| 81 | + /** |
|
| 82 | + * @inheritdoc |
|
| 83 | + */ |
|
| 84 | + public function setAsync(bool $async) : self |
|
| 85 | + { |
|
| 86 | + $this->async = $async; |
|
| 87 | + return $this; |
|
| 88 | + } |
|
| 89 | + /** |
|
| 90 | + * @inheritdoc |
|
| 91 | + */ |
|
| 92 | + public function getAsync() : bool |
|
| 93 | + { |
|
| 94 | + return $this->async; |
|
| 95 | + } |
|
| 96 | + /** |
|
| 97 | + * @inheritdoc |
|
| 98 | + */ |
|
| 99 | + public function setElasticMetaHeader(bool $active) : self |
|
| 100 | + { |
|
| 101 | + $this->elasticMetaHeader = $active; |
|
| 102 | + return $this; |
|
| 103 | + } |
|
| 104 | + /** |
|
| 105 | + * @inheritdoc |
|
| 106 | + */ |
|
| 107 | + public function getElasticMetaHeader() : bool |
|
| 108 | + { |
|
| 109 | + return $this->elasticMetaHeader; |
|
| 110 | + } |
|
| 111 | + /** |
|
| 112 | + * @inheritdoc |
|
| 113 | + */ |
|
| 114 | + public function setResponseException(bool $active) : self |
|
| 115 | + { |
|
| 116 | + $this->responseException = $active; |
|
| 117 | + return $this; |
|
| 118 | + } |
|
| 119 | + /** |
|
| 120 | + * @inheritdoc |
|
| 121 | + */ |
|
| 122 | + public function getResponseException() : bool |
|
| 123 | + { |
|
| 124 | + return $this->responseException; |
|
| 125 | + } |
|
| 126 | + /** |
|
| 127 | + * @inheritdoc |
|
| 128 | + */ |
|
| 129 | + public function sendRequest(RequestInterface $request) |
|
| 130 | + { |
|
| 131 | + // If async returns a Promise |
|
| 132 | + if ($this->getAsync()) { |
|
| 133 | + if ($this->getElasticMetaHeader()) { |
|
| 134 | + $this->transport->setElasticMetaHeader(Client::CLIENT_NAME, Client::VERSION, \true); |
|
| 135 | + } |
|
| 136 | + $this->transport->setAsyncOnSuccess($request->getMethod() === 'HEAD' ? new AsyncOnSuccessNoException() : ($this->getResponseException() ? new AsyncOnSuccess() : new AsyncOnSuccessNoException())); |
|
| 137 | + return $this->transport->sendAsyncRequest($request); |
|
| 138 | + } |
|
| 139 | + if ($this->getElasticMetaHeader()) { |
|
| 140 | + $this->transport->setElasticMetaHeader(Client::CLIENT_NAME, Client::VERSION, \false); |
|
| 141 | + } |
|
| 142 | + $start = \microtime(\true); |
|
| 143 | + $response = $this->transport->sendRequest($request); |
|
| 144 | + $this->logger->info(\sprintf("Response time in %.3f sec", \microtime(\true) - $start)); |
|
| 145 | + $result = new Elasticsearch(); |
|
| 146 | + $result->setResponse($response, $request->getMethod() === 'HEAD' ? \false : $this->getResponseException()); |
|
| 147 | + return $result; |
|
| 148 | + } |
|
| 149 | 149 | } |
@@ -24,8 +24,7 @@ |
||
| 24 | 24 | use OCA\FullTextSearch_Elasticsearch\Vendor\Http\Promise\Promise; |
| 25 | 25 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\RequestInterface; |
| 26 | 26 | use Psr\Log\LoggerInterface; |
| 27 | -final class Client implements ClientInterface |
|
| 28 | -{ |
|
| 27 | +final class Client implements ClientInterface { |
|
| 29 | 28 | const CLIENT_NAME = 'es'; |
| 30 | 29 | const VERSION = '8.17.0'; |
| 31 | 30 | const API_COMPATIBILITY_HEADER = '%s/vnd.elasticsearch+%s; compatible-with=8'; |
@@ -17,8 +17,8 @@ |
||
| 17 | 17 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\ResponseInterface; |
| 18 | 18 | interface ElasticsearchInterface |
| 19 | 19 | { |
| 20 | - /** |
|
| 21 | - * Set the HTTP PSR-7 response |
|
| 22 | - */ |
|
| 23 | - public function setResponse(ResponseInterface $response, bool $throwException = \true) : void; |
|
| 20 | + /** |
|
| 21 | + * Set the HTTP PSR-7 response |
|
| 22 | + */ |
|
| 23 | + public function setResponse(ResponseInterface $response, bool $throwException = \true) : void; |
|
| 24 | 24 | } |
@@ -11,7 +11,7 @@ |
||
| 11 | 11 | * Elasticsearch B.V licenses this file to you under the MIT License. |
| 12 | 12 | * See the LICENSE file in the project root for more information. |
| 13 | 13 | */ |
| 14 | -declare (strict_types=1); |
|
| 14 | +declare(strict_types=1); |
|
| 15 | 15 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\Elastic\Elasticsearch\Response; |
| 16 | 16 | |
| 17 | 17 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\ResponseInterface; |
@@ -15,8 +15,7 @@ |
||
| 15 | 15 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\Elastic\Elasticsearch\Response; |
| 16 | 16 | |
| 17 | 17 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\ResponseInterface; |
| 18 | -interface ElasticsearchInterface |
|
| 19 | -{ |
|
| 18 | +interface ElasticsearchInterface { |
|
| 20 | 19 | /** |
| 21 | 20 | * Set the HTTP PSR-7 response |
| 22 | 21 | */ |
@@ -34,220 +34,220 @@ |
||
| 34 | 34 | */ |
| 35 | 35 | class Elasticsearch implements ElasticsearchInterface, ResponseInterface, ArrayAccess |
| 36 | 36 | { |
| 37 | - const HEADER_CHECK = 'X-Elastic-Product'; |
|
| 38 | - const PRODUCT_NAME = 'Elasticsearch'; |
|
| 39 | - use ProductCheckTrait; |
|
| 40 | - use MessageResponseTrait; |
|
| 41 | - protected array $asArray; |
|
| 42 | - protected object $asObject; |
|
| 43 | - protected string $asString; |
|
| 44 | - /** |
|
| 45 | - * The PSR-7 response |
|
| 46 | - */ |
|
| 47 | - protected ResponseInterface $response; |
|
| 48 | - /** |
|
| 49 | - * Enable or disable the response Exception |
|
| 50 | - */ |
|
| 51 | - protected bool $responseException; |
|
| 52 | - /** |
|
| 53 | - * @throws ClientResponseException if status code 4xx |
|
| 54 | - * @throws ServerResponseException if status code 5xx |
|
| 55 | - */ |
|
| 56 | - public function setResponse(ResponseInterface $response, bool $throwException = \true) : void |
|
| 57 | - { |
|
| 58 | - $this->productCheck($response); |
|
| 59 | - $this->response = $response; |
|
| 60 | - $status = $response->getStatusCode(); |
|
| 61 | - if ($throwException && $status > 399 && $status < 500) { |
|
| 62 | - $error = new ClientResponseException(\sprintf("%s %s: %s", $status, $response->getReasonPhrase(), (string) $response->getBody()), $status); |
|
| 63 | - throw $error->setResponse($response); |
|
| 64 | - } elseif ($throwException && $status > 499 && $status < 600) { |
|
| 65 | - $error = new ServerResponseException(\sprintf("%s %s: %s", $status, $response->getReasonPhrase(), (string) $response->getBody()), $status); |
|
| 66 | - throw $error->setResponse($response); |
|
| 67 | - } |
|
| 68 | - } |
|
| 69 | - /** |
|
| 70 | - * Return true if status code is 2xx |
|
| 71 | - */ |
|
| 72 | - public function asBool() : bool |
|
| 73 | - { |
|
| 74 | - return $this->response->getStatusCode() >= 200 && $this->response->getStatusCode() < 300; |
|
| 75 | - } |
|
| 76 | - /** |
|
| 77 | - * Converts the body content to array, if possible. |
|
| 78 | - * Otherwise, it throws an UnknownContentTypeException |
|
| 79 | - * if Content-Type is not specified or unknown. |
|
| 80 | - * |
|
| 81 | - * @throws UnknownContentTypeException |
|
| 82 | - */ |
|
| 83 | - public function asArray() : array |
|
| 84 | - { |
|
| 85 | - if (isset($this->asArray)) { |
|
| 86 | - return $this->asArray; |
|
| 87 | - } |
|
| 88 | - if (!$this->response->hasHeader('Content-Type')) { |
|
| 89 | - throw new UnknownContentTypeException('No Content-Type specified in the response'); |
|
| 90 | - } |
|
| 91 | - $contentType = $this->response->getHeaderLine('Content-Type'); |
|
| 92 | - if (\strpos($contentType, 'application/json') !== \false || \strpos($contentType, 'application/vnd.elasticsearch+json') !== \false) { |
|
| 93 | - $this->asArray = JsonSerializer::unserialize($this->asString()); |
|
| 94 | - return $this->asArray; |
|
| 95 | - } |
|
| 96 | - if (\strpos($contentType, 'application/x-ndjson') !== \false || \strpos($contentType, 'application/vnd.elasticsearch+x-ndjson') !== \false) { |
|
| 97 | - $this->asArray = NDJsonSerializer::unserialize($this->asString()); |
|
| 98 | - return $this->asArray; |
|
| 99 | - } |
|
| 100 | - if (\strpos($contentType, 'text/csv') !== \false) { |
|
| 101 | - $this->asArray = CsvSerializer::unserialize($this->asString()); |
|
| 102 | - return $this->asArray; |
|
| 103 | - } |
|
| 104 | - throw new UnknownContentTypeException(\sprintf("Cannot deserialize the reponse as array with Content-Type: %s", $contentType)); |
|
| 105 | - } |
|
| 106 | - /** |
|
| 107 | - * Converts the body content to object, if possible. |
|
| 108 | - * Otherwise, it throws an UnknownContentTypeException |
|
| 109 | - * if Content-Type is not specified or unknown. |
|
| 110 | - * |
|
| 111 | - * @throws UnknownContentTypeException |
|
| 112 | - */ |
|
| 113 | - public function asObject() : object |
|
| 114 | - { |
|
| 115 | - if (isset($this->asObject)) { |
|
| 116 | - return $this->asObject; |
|
| 117 | - } |
|
| 118 | - $contentType = $this->response->getHeaderLine('Content-Type'); |
|
| 119 | - if (\strpos($contentType, 'application/json') !== \false || \strpos($contentType, 'application/vnd.elasticsearch+json') !== \false) { |
|
| 120 | - $this->asObject = JsonSerializer::unserialize($this->asString(), ['type' => 'object']); |
|
| 121 | - return $this->asObject; |
|
| 122 | - } |
|
| 123 | - if (\strpos($contentType, 'application/x-ndjson') !== \false || \strpos($contentType, 'application/vnd.elasticsearch+x-ndjson') !== \false) { |
|
| 124 | - $this->asObject = NDJsonSerializer::unserialize($this->asString(), ['type' => 'object']); |
|
| 125 | - return $this->asObject; |
|
| 126 | - } |
|
| 127 | - if (\strpos($contentType, 'text/xml') !== \false || \strpos($contentType, 'application/xml') !== \false) { |
|
| 128 | - $this->asObject = XmlSerializer::unserialize($this->asString()); |
|
| 129 | - return $this->asObject; |
|
| 130 | - } |
|
| 131 | - throw new UnknownContentTypeException(\sprintf("Cannot deserialize the reponse as object with Content-Type: %s", $contentType)); |
|
| 132 | - } |
|
| 133 | - /** |
|
| 134 | - * Converts the body content to string |
|
| 135 | - */ |
|
| 136 | - public function asString() : string |
|
| 137 | - { |
|
| 138 | - if (empty($this->asString)) { |
|
| 139 | - $this->asString = (string) $this->response->getBody(); |
|
| 140 | - } |
|
| 141 | - return $this->asString; |
|
| 142 | - } |
|
| 143 | - /** |
|
| 144 | - * Converts the body content to string |
|
| 145 | - */ |
|
| 146 | - public function __toString() : string |
|
| 147 | - { |
|
| 148 | - return $this->asString(); |
|
| 149 | - } |
|
| 150 | - /** |
|
| 151 | - * Access the body content as object properties |
|
| 152 | - * |
|
| 153 | - * @see https://www.php.net/manual/en/language.oop5.overloading.php#object.get |
|
| 154 | - */ |
|
| 155 | - public function __get($name) |
|
| 156 | - { |
|
| 157 | - return $this->asObject()->{$name} ?? null; |
|
| 158 | - } |
|
| 159 | - /** |
|
| 160 | - * ArrayAccess interface |
|
| 161 | - * |
|
| 162 | - * @see https://www.php.net/manual/en/class.arrayaccess.php |
|
| 163 | - */ |
|
| 164 | - public function offsetExists($offset) : bool |
|
| 165 | - { |
|
| 166 | - return isset($this->asArray()[$offset]); |
|
| 167 | - } |
|
| 168 | - /** |
|
| 169 | - * ArrayAccess interface |
|
| 170 | - * |
|
| 171 | - * @see https://www.php.net/manual/en/class.arrayaccess.php |
|
| 172 | - * |
|
| 173 | - * @return mixed |
|
| 174 | - */ |
|
| 175 | - #[\ReturnTypeWillChange] |
|
| 176 | - public function offsetGet($offset) |
|
| 177 | - { |
|
| 178 | - return $this->asArray()[$offset]; |
|
| 179 | - } |
|
| 180 | - /** |
|
| 181 | - * ArrayAccess interface |
|
| 182 | - * |
|
| 183 | - * @see https://www.php.net/manual/en/class.arrayaccess.php |
|
| 184 | - */ |
|
| 185 | - public function offsetSet($offset, $value) : void |
|
| 186 | - { |
|
| 187 | - throw new ArrayAccessException('The array is reading only'); |
|
| 188 | - } |
|
| 189 | - /** |
|
| 190 | - * ArrayAccess interface |
|
| 191 | - * |
|
| 192 | - * @see https://www.php.net/manual/en/class.arrayaccess.php |
|
| 193 | - */ |
|
| 194 | - public function offsetUnset($offset) : void |
|
| 195 | - { |
|
| 196 | - throw new ArrayAccessException('The array is reading only'); |
|
| 197 | - } |
|
| 198 | - /** |
|
| 199 | - * Map the response body to an object of a specific class |
|
| 200 | - * by default the class is the PHP standard one (stdClass) |
|
| 201 | - * |
|
| 202 | - * This mapping works only for ES|QL results (with columns and values) |
|
| 203 | - * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/esql.html |
|
| 204 | - * |
|
| 205 | - * @return object[] |
|
| 206 | - */ |
|
| 207 | - public function mapTo(string $class = stdClass::class) : array |
|
| 208 | - { |
|
| 209 | - $response = $this->asArray(); |
|
| 210 | - if (!isset($response['columns']) || !isset($response['values'])) { |
|
| 211 | - throw new UnknownContentTypeException(\sprintf("The response is not a valid ES|QL result. I cannot mapTo(\"%s\")", $class)); |
|
| 212 | - } |
|
| 213 | - $iterator = []; |
|
| 214 | - $ncol = \count($response['columns']); |
|
| 215 | - foreach ($response['values'] as $value) { |
|
| 216 | - $obj = new $class(); |
|
| 217 | - for ($i = 0; $i < $ncol; $i++) { |
|
| 218 | - $field = Utility::formatVariableName($response['columns'][$i]['name']); |
|
| 219 | - if ($class !== stdClass::class && !\property_exists($obj, $field)) { |
|
| 220 | - continue; |
|
| 221 | - } |
|
| 222 | - switch ($response['columns'][$i]['type']) { |
|
| 223 | - case 'boolean': |
|
| 224 | - $obj->{$field} = (bool) $value[$i]; |
|
| 225 | - break; |
|
| 226 | - case 'date': |
|
| 227 | - $obj->{$field} = new DateTime($value[$i]); |
|
| 228 | - break; |
|
| 229 | - case 'alias': |
|
| 230 | - case 'text': |
|
| 231 | - case 'keyword': |
|
| 232 | - case 'ip': |
|
| 233 | - $obj->{$field} = (string) $value[$i]; |
|
| 234 | - break; |
|
| 235 | - case 'integer': |
|
| 236 | - $obj->{$field} = (int) $value[$i]; |
|
| 237 | - break; |
|
| 238 | - case 'long': |
|
| 239 | - case 'double': |
|
| 240 | - $obj->{$field} = (float) $value[$i]; |
|
| 241 | - break; |
|
| 242 | - case 'null': |
|
| 243 | - $obj->{$field} = null; |
|
| 244 | - break; |
|
| 245 | - default: |
|
| 246 | - $obj->{$field} = $value[$i]; |
|
| 247 | - } |
|
| 248 | - } |
|
| 249 | - $iterator[] = $obj; |
|
| 250 | - } |
|
| 251 | - return $iterator; |
|
| 252 | - } |
|
| 37 | + const HEADER_CHECK = 'X-Elastic-Product'; |
|
| 38 | + const PRODUCT_NAME = 'Elasticsearch'; |
|
| 39 | + use ProductCheckTrait; |
|
| 40 | + use MessageResponseTrait; |
|
| 41 | + protected array $asArray; |
|
| 42 | + protected object $asObject; |
|
| 43 | + protected string $asString; |
|
| 44 | + /** |
|
| 45 | + * The PSR-7 response |
|
| 46 | + */ |
|
| 47 | + protected ResponseInterface $response; |
|
| 48 | + /** |
|
| 49 | + * Enable or disable the response Exception |
|
| 50 | + */ |
|
| 51 | + protected bool $responseException; |
|
| 52 | + /** |
|
| 53 | + * @throws ClientResponseException if status code 4xx |
|
| 54 | + * @throws ServerResponseException if status code 5xx |
|
| 55 | + */ |
|
| 56 | + public function setResponse(ResponseInterface $response, bool $throwException = \true) : void |
|
| 57 | + { |
|
| 58 | + $this->productCheck($response); |
|
| 59 | + $this->response = $response; |
|
| 60 | + $status = $response->getStatusCode(); |
|
| 61 | + if ($throwException && $status > 399 && $status < 500) { |
|
| 62 | + $error = new ClientResponseException(\sprintf("%s %s: %s", $status, $response->getReasonPhrase(), (string) $response->getBody()), $status); |
|
| 63 | + throw $error->setResponse($response); |
|
| 64 | + } elseif ($throwException && $status > 499 && $status < 600) { |
|
| 65 | + $error = new ServerResponseException(\sprintf("%s %s: %s", $status, $response->getReasonPhrase(), (string) $response->getBody()), $status); |
|
| 66 | + throw $error->setResponse($response); |
|
| 67 | + } |
|
| 68 | + } |
|
| 69 | + /** |
|
| 70 | + * Return true if status code is 2xx |
|
| 71 | + */ |
|
| 72 | + public function asBool() : bool |
|
| 73 | + { |
|
| 74 | + return $this->response->getStatusCode() >= 200 && $this->response->getStatusCode() < 300; |
|
| 75 | + } |
|
| 76 | + /** |
|
| 77 | + * Converts the body content to array, if possible. |
|
| 78 | + * Otherwise, it throws an UnknownContentTypeException |
|
| 79 | + * if Content-Type is not specified or unknown. |
|
| 80 | + * |
|
| 81 | + * @throws UnknownContentTypeException |
|
| 82 | + */ |
|
| 83 | + public function asArray() : array |
|
| 84 | + { |
|
| 85 | + if (isset($this->asArray)) { |
|
| 86 | + return $this->asArray; |
|
| 87 | + } |
|
| 88 | + if (!$this->response->hasHeader('Content-Type')) { |
|
| 89 | + throw new UnknownContentTypeException('No Content-Type specified in the response'); |
|
| 90 | + } |
|
| 91 | + $contentType = $this->response->getHeaderLine('Content-Type'); |
|
| 92 | + if (\strpos($contentType, 'application/json') !== \false || \strpos($contentType, 'application/vnd.elasticsearch+json') !== \false) { |
|
| 93 | + $this->asArray = JsonSerializer::unserialize($this->asString()); |
|
| 94 | + return $this->asArray; |
|
| 95 | + } |
|
| 96 | + if (\strpos($contentType, 'application/x-ndjson') !== \false || \strpos($contentType, 'application/vnd.elasticsearch+x-ndjson') !== \false) { |
|
| 97 | + $this->asArray = NDJsonSerializer::unserialize($this->asString()); |
|
| 98 | + return $this->asArray; |
|
| 99 | + } |
|
| 100 | + if (\strpos($contentType, 'text/csv') !== \false) { |
|
| 101 | + $this->asArray = CsvSerializer::unserialize($this->asString()); |
|
| 102 | + return $this->asArray; |
|
| 103 | + } |
|
| 104 | + throw new UnknownContentTypeException(\sprintf("Cannot deserialize the reponse as array with Content-Type: %s", $contentType)); |
|
| 105 | + } |
|
| 106 | + /** |
|
| 107 | + * Converts the body content to object, if possible. |
|
| 108 | + * Otherwise, it throws an UnknownContentTypeException |
|
| 109 | + * if Content-Type is not specified or unknown. |
|
| 110 | + * |
|
| 111 | + * @throws UnknownContentTypeException |
|
| 112 | + */ |
|
| 113 | + public function asObject() : object |
|
| 114 | + { |
|
| 115 | + if (isset($this->asObject)) { |
|
| 116 | + return $this->asObject; |
|
| 117 | + } |
|
| 118 | + $contentType = $this->response->getHeaderLine('Content-Type'); |
|
| 119 | + if (\strpos($contentType, 'application/json') !== \false || \strpos($contentType, 'application/vnd.elasticsearch+json') !== \false) { |
|
| 120 | + $this->asObject = JsonSerializer::unserialize($this->asString(), ['type' => 'object']); |
|
| 121 | + return $this->asObject; |
|
| 122 | + } |
|
| 123 | + if (\strpos($contentType, 'application/x-ndjson') !== \false || \strpos($contentType, 'application/vnd.elasticsearch+x-ndjson') !== \false) { |
|
| 124 | + $this->asObject = NDJsonSerializer::unserialize($this->asString(), ['type' => 'object']); |
|
| 125 | + return $this->asObject; |
|
| 126 | + } |
|
| 127 | + if (\strpos($contentType, 'text/xml') !== \false || \strpos($contentType, 'application/xml') !== \false) { |
|
| 128 | + $this->asObject = XmlSerializer::unserialize($this->asString()); |
|
| 129 | + return $this->asObject; |
|
| 130 | + } |
|
| 131 | + throw new UnknownContentTypeException(\sprintf("Cannot deserialize the reponse as object with Content-Type: %s", $contentType)); |
|
| 132 | + } |
|
| 133 | + /** |
|
| 134 | + * Converts the body content to string |
|
| 135 | + */ |
|
| 136 | + public function asString() : string |
|
| 137 | + { |
|
| 138 | + if (empty($this->asString)) { |
|
| 139 | + $this->asString = (string) $this->response->getBody(); |
|
| 140 | + } |
|
| 141 | + return $this->asString; |
|
| 142 | + } |
|
| 143 | + /** |
|
| 144 | + * Converts the body content to string |
|
| 145 | + */ |
|
| 146 | + public function __toString() : string |
|
| 147 | + { |
|
| 148 | + return $this->asString(); |
|
| 149 | + } |
|
| 150 | + /** |
|
| 151 | + * Access the body content as object properties |
|
| 152 | + * |
|
| 153 | + * @see https://www.php.net/manual/en/language.oop5.overloading.php#object.get |
|
| 154 | + */ |
|
| 155 | + public function __get($name) |
|
| 156 | + { |
|
| 157 | + return $this->asObject()->{$name} ?? null; |
|
| 158 | + } |
|
| 159 | + /** |
|
| 160 | + * ArrayAccess interface |
|
| 161 | + * |
|
| 162 | + * @see https://www.php.net/manual/en/class.arrayaccess.php |
|
| 163 | + */ |
|
| 164 | + public function offsetExists($offset) : bool |
|
| 165 | + { |
|
| 166 | + return isset($this->asArray()[$offset]); |
|
| 167 | + } |
|
| 168 | + /** |
|
| 169 | + * ArrayAccess interface |
|
| 170 | + * |
|
| 171 | + * @see https://www.php.net/manual/en/class.arrayaccess.php |
|
| 172 | + * |
|
| 173 | + * @return mixed |
|
| 174 | + */ |
|
| 175 | + #[\ReturnTypeWillChange] |
|
| 176 | + public function offsetGet($offset) |
|
| 177 | + { |
|
| 178 | + return $this->asArray()[$offset]; |
|
| 179 | + } |
|
| 180 | + /** |
|
| 181 | + * ArrayAccess interface |
|
| 182 | + * |
|
| 183 | + * @see https://www.php.net/manual/en/class.arrayaccess.php |
|
| 184 | + */ |
|
| 185 | + public function offsetSet($offset, $value) : void |
|
| 186 | + { |
|
| 187 | + throw new ArrayAccessException('The array is reading only'); |
|
| 188 | + } |
|
| 189 | + /** |
|
| 190 | + * ArrayAccess interface |
|
| 191 | + * |
|
| 192 | + * @see https://www.php.net/manual/en/class.arrayaccess.php |
|
| 193 | + */ |
|
| 194 | + public function offsetUnset($offset) : void |
|
| 195 | + { |
|
| 196 | + throw new ArrayAccessException('The array is reading only'); |
|
| 197 | + } |
|
| 198 | + /** |
|
| 199 | + * Map the response body to an object of a specific class |
|
| 200 | + * by default the class is the PHP standard one (stdClass) |
|
| 201 | + * |
|
| 202 | + * This mapping works only for ES|QL results (with columns and values) |
|
| 203 | + * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/esql.html |
|
| 204 | + * |
|
| 205 | + * @return object[] |
|
| 206 | + */ |
|
| 207 | + public function mapTo(string $class = stdClass::class) : array |
|
| 208 | + { |
|
| 209 | + $response = $this->asArray(); |
|
| 210 | + if (!isset($response['columns']) || !isset($response['values'])) { |
|
| 211 | + throw new UnknownContentTypeException(\sprintf("The response is not a valid ES|QL result. I cannot mapTo(\"%s\")", $class)); |
|
| 212 | + } |
|
| 213 | + $iterator = []; |
|
| 214 | + $ncol = \count($response['columns']); |
|
| 215 | + foreach ($response['values'] as $value) { |
|
| 216 | + $obj = new $class(); |
|
| 217 | + for ($i = 0; $i < $ncol; $i++) { |
|
| 218 | + $field = Utility::formatVariableName($response['columns'][$i]['name']); |
|
| 219 | + if ($class !== stdClass::class && !\property_exists($obj, $field)) { |
|
| 220 | + continue; |
|
| 221 | + } |
|
| 222 | + switch ($response['columns'][$i]['type']) { |
|
| 223 | + case 'boolean': |
|
| 224 | + $obj->{$field} = (bool) $value[$i]; |
|
| 225 | + break; |
|
| 226 | + case 'date': |
|
| 227 | + $obj->{$field} = new DateTime($value[$i]); |
|
| 228 | + break; |
|
| 229 | + case 'alias': |
|
| 230 | + case 'text': |
|
| 231 | + case 'keyword': |
|
| 232 | + case 'ip': |
|
| 233 | + $obj->{$field} = (string) $value[$i]; |
|
| 234 | + break; |
|
| 235 | + case 'integer': |
|
| 236 | + $obj->{$field} = (int) $value[$i]; |
|
| 237 | + break; |
|
| 238 | + case 'long': |
|
| 239 | + case 'double': |
|
| 240 | + $obj->{$field} = (float) $value[$i]; |
|
| 241 | + break; |
|
| 242 | + case 'null': |
|
| 243 | + $obj->{$field} = null; |
|
| 244 | + break; |
|
| 245 | + default: |
|
| 246 | + $obj->{$field} = $value[$i]; |
|
| 247 | + } |
|
| 248 | + } |
|
| 249 | + $iterator[] = $obj; |
|
| 250 | + } |
|
| 251 | + return $iterator; |
|
| 252 | + } |
|
| 253 | 253 | } |
@@ -11,7 +11,7 @@ discard block |
||
| 11 | 11 | * Elasticsearch B.V licenses this file to you under the MIT License. |
| 12 | 12 | * See the LICENSE file in the project root for more information. |
| 13 | 13 | */ |
| 14 | -declare (strict_types=1); |
|
| 14 | +declare(strict_types=1); |
|
| 15 | 15 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\Elastic\Elasticsearch\Response; |
| 16 | 16 | |
| 17 | 17 | use ArrayAccess; |
@@ -59,10 +59,10 @@ discard block |
||
| 59 | 59 | $this->response = $response; |
| 60 | 60 | $status = $response->getStatusCode(); |
| 61 | 61 | if ($throwException && $status > 399 && $status < 500) { |
| 62 | - $error = new ClientResponseException(\sprintf("%s %s: %s", $status, $response->getReasonPhrase(), (string) $response->getBody()), $status); |
|
| 62 | + $error = new ClientResponseException(\sprintf("%s %s: %s", $status, $response->getReasonPhrase(), (string)$response->getBody()), $status); |
|
| 63 | 63 | throw $error->setResponse($response); |
| 64 | 64 | } elseif ($throwException && $status > 499 && $status < 600) { |
| 65 | - $error = new ServerResponseException(\sprintf("%s %s: %s", $status, $response->getReasonPhrase(), (string) $response->getBody()), $status); |
|
| 65 | + $error = new ServerResponseException(\sprintf("%s %s: %s", $status, $response->getReasonPhrase(), (string)$response->getBody()), $status); |
|
| 66 | 66 | throw $error->setResponse($response); |
| 67 | 67 | } |
| 68 | 68 | } |
@@ -136,7 +136,7 @@ discard block |
||
| 136 | 136 | public function asString() : string |
| 137 | 137 | { |
| 138 | 138 | if (empty($this->asString)) { |
| 139 | - $this->asString = (string) $this->response->getBody(); |
|
| 139 | + $this->asString = (string)$this->response->getBody(); |
|
| 140 | 140 | } |
| 141 | 141 | return $this->asString; |
| 142 | 142 | } |
@@ -221,7 +221,7 @@ discard block |
||
| 221 | 221 | } |
| 222 | 222 | switch ($response['columns'][$i]['type']) { |
| 223 | 223 | case 'boolean': |
| 224 | - $obj->{$field} = (bool) $value[$i]; |
|
| 224 | + $obj->{$field} = (bool)$value[$i]; |
|
| 225 | 225 | break; |
| 226 | 226 | case 'date': |
| 227 | 227 | $obj->{$field} = new DateTime($value[$i]); |
@@ -230,14 +230,14 @@ discard block |
||
| 230 | 230 | case 'text': |
| 231 | 231 | case 'keyword': |
| 232 | 232 | case 'ip': |
| 233 | - $obj->{$field} = (string) $value[$i]; |
|
| 233 | + $obj->{$field} = (string)$value[$i]; |
|
| 234 | 234 | break; |
| 235 | 235 | case 'integer': |
| 236 | - $obj->{$field} = (int) $value[$i]; |
|
| 236 | + $obj->{$field} = (int)$value[$i]; |
|
| 237 | 237 | break; |
| 238 | 238 | case 'long': |
| 239 | 239 | case 'double': |
| 240 | - $obj->{$field} = (float) $value[$i]; |
|
| 240 | + $obj->{$field} = (float)$value[$i]; |
|
| 241 | 241 | break; |
| 242 | 242 | case 'null': |
| 243 | 243 | $obj->{$field} = null; |
@@ -11,7 +11,7 @@ |
||
| 11 | 11 | * Elasticsearch B.V licenses this file to you under the MIT License. |
| 12 | 12 | * See the LICENSE file in the project root for more information. |
| 13 | 13 | */ |
| 14 | -declare (strict_types=1); |
|
| 14 | +declare(strict_types=1); |
|
| 15 | 15 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\Elastic\Elasticsearch\Exception; |
| 16 | 16 | |
| 17 | 17 | use Throwable; |
@@ -15,6 +15,5 @@ |
||
| 15 | 15 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\Elastic\Elasticsearch\Exception; |
| 16 | 16 | |
| 17 | 17 | use Throwable; |
| 18 | -interface ElasticsearchException extends Throwable |
|
| 19 | -{ |
|
| 18 | +interface ElasticsearchException extends Throwable { |
|
| 20 | 19 | } |
@@ -21,5 +21,5 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | class ClientResponseException extends Exception implements ElasticsearchException |
| 23 | 23 | { |
| 24 | - use ResponseTrait; |
|
| 24 | + use ResponseTrait; |
|
| 25 | 25 | } |
@@ -11,7 +11,7 @@ |
||
| 11 | 11 | * Elasticsearch B.V licenses this file to you under the MIT License. |
| 12 | 12 | * See the LICENSE file in the project root for more information. |
| 13 | 13 | */ |
| 14 | -declare (strict_types=1); |
|
| 14 | +declare(strict_types=1); |
|
| 15 | 15 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\Elastic\Elasticsearch\Exception; |
| 16 | 16 | |
| 17 | 17 | use OCA\FullTextSearch_Elasticsearch\Vendor\Elastic\Elasticsearch\Traits\ResponseTrait; |
@@ -19,7 +19,6 @@ |
||
| 19 | 19 | /** |
| 20 | 20 | * HTTP client error with 4xx status code |
| 21 | 21 | */ |
| 22 | -class ClientResponseException extends Exception implements ElasticsearchException |
|
| 23 | -{ |
|
| 22 | +class ClientResponseException extends Exception implements ElasticsearchException { |
|
| 24 | 23 | use ResponseTrait; |
| 25 | 24 | } |
@@ -11,7 +11,7 @@ |
||
| 11 | 11 | * Elasticsearch B.V licenses this file to you under the MIT License. |
| 12 | 12 | * See the LICENSE file in the project root for more information. |
| 13 | 13 | */ |
| 14 | -declare (strict_types=1); |
|
| 14 | +declare(strict_types=1); |
|
| 15 | 15 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\Elastic\Elasticsearch\Exception; |
| 16 | 16 | |
| 17 | 17 | use Exception; |
@@ -15,6 +15,5 @@ |
||
| 15 | 15 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\Elastic\Elasticsearch\Exception; |
| 16 | 16 | |
| 17 | 17 | use Exception; |
| 18 | -class ConfigException extends Exception implements ElasticsearchException |
|
| 19 | -{ |
|
| 18 | +class ConfigException extends Exception implements ElasticsearchException { |
|
| 20 | 19 | } |
@@ -11,7 +11,7 @@ |
||
| 11 | 11 | * Elasticsearch B.V licenses this file to you under the MIT License. |
| 12 | 12 | * See the LICENSE file in the project root for more information. |
| 13 | 13 | */ |
| 14 | -declare (strict_types=1); |
|
| 14 | +declare(strict_types=1); |
|
| 15 | 15 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\Elastic\Elasticsearch\Exception; |
| 16 | 16 | |
| 17 | 17 | use InvalidArgumentException as InvalidArgument; |
@@ -15,6 +15,5 @@ |
||
| 15 | 15 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\Elastic\Elasticsearch\Exception; |
| 16 | 16 | |
| 17 | 17 | use InvalidArgumentException as InvalidArgument; |
| 18 | -class InvalidArgumentException extends InvalidArgument implements ElasticsearchException |
|
| 19 | -{ |
|
| 18 | +class InvalidArgumentException extends InvalidArgument implements ElasticsearchException { |
|
| 20 | 19 | } |
@@ -21,5 +21,5 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | class ServerResponseException extends Exception implements ElasticsearchException |
| 23 | 23 | { |
| 24 | - use ResponseTrait; |
|
| 24 | + use ResponseTrait; |
|
| 25 | 25 | } |
@@ -19,7 +19,6 @@ |
||
| 19 | 19 | /** |
| 20 | 20 | * HTTP server error with 5xx status code |
| 21 | 21 | */ |
| 22 | -class ServerResponseException extends Exception implements ElasticsearchException |
|
| 23 | -{ |
|
| 22 | +class ServerResponseException extends Exception implements ElasticsearchException { |
|
| 24 | 23 | use ResponseTrait; |
| 25 | 24 | } |
@@ -11,7 +11,7 @@ |
||
| 11 | 11 | * Elasticsearch B.V licenses this file to you under the MIT License. |
| 12 | 12 | * See the LICENSE file in the project root for more information. |
| 13 | 13 | */ |
| 14 | -declare (strict_types=1); |
|
| 14 | +declare(strict_types=1); |
|
| 15 | 15 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\Elastic\Elasticsearch\Exception; |
| 16 | 16 | |
| 17 | 17 | use OCA\FullTextSearch_Elasticsearch\Vendor\Elastic\Elasticsearch\Traits\ResponseTrait; |