@@ -72,10 +72,9 @@ public function __construct(
7272 * @param string $uri
7373 * @param mixed|null $body
7474 * @return stdClass
75- * @throws HttpErrorResponseException
76- * @throws HttpTransportException
77- * @throws SerializeException
78- * @throws OpenSearchException
75+ * @throws HttpErrorResponseException If the response status code is not 2xx
76+ * @throws HttpTransportException If an error happens while the http client processes the request
77+ * @throws SerializeException If an error happens during (de-)serialization
7978 */
8079 protected function request (string $ method , string $ uri , mixed $ body = null ): stdClass
8180 {
@@ -96,6 +95,7 @@ protected function request(string $method, string $uri, mixed $body = null): std
9695 throw $ e ;
9796 }
9897 }
98+ /** @var OpenSearchException $lastError */
9999 throw $ lastError ;
100100 }
101101
@@ -113,7 +113,9 @@ protected function buildUrl(string ...$path): string
113113 *
114114 * @param ModelInterface $model
115115 * @return bool
116- * @throws OpenSearchException
116+ * @throws HttpErrorResponseException If the response status code is not 2xx
117+ * @throws HttpTransportException If an error happens while the http client processes the request
118+ * @throws SerializeException If an error happens during (de-)serialization
117119 */
118120 public function save (ModelInterface $ model ): bool
119121 {
@@ -132,10 +134,9 @@ public function save(ModelInterface $model): bool
132134 * @param mixed $id
133135 * @param ModelInterface|null $model
134136 * @return ModelInterface|null
135- * @throws HttpErrorResponseException
136- * @throws HttpTransportException
137- * @throws OpenSearchException
138- * @throws SerializeException
137+ * @throws HttpErrorResponseException If the response status code is not 2xx
138+ * @throws HttpTransportException If an error happens while the http client processes the request
139+ * @throws SerializeException If an error happens during (de-)serialization
139140 */
140141 public function get (string $ modelClass , mixed $ id , ?ModelInterface $ model = null ): ?ModelInterface
141142 {
@@ -151,17 +152,7 @@ public function get(string $modelClass, mixed $id, ?ModelInterface $model = null
151152 throw $ e ;
152153 }
153154
154- if (!isset ($ response ->_id ) || !is_string ($ response ->_id )) {
155- throw new SerializeException ("Received invalid document _id from OpenSearch " );
156- }
157-
158- if (isset ($ response ->_source ) && is_object ($ response ->_source )) {
159- $ data = get_object_vars ($ response ->_source );
160- } else {
161- $ data = [];
162- }
163-
164- $ data [$ modelClass ::getIdField ()] = $ response ->_id ;
155+ $ data = $ this ->getModelData ($ response , $ modelClass );
165156
166157 if ($ model ) {
167158 return $ model ->applyData ($ data );
@@ -174,7 +165,9 @@ public function get(string $modelClass, mixed $id, ?ModelInterface $model = null
174165 *
175166 * @param ModelInterface $model
176167 * @return bool
177- * @throws OpenSearchException
168+ * @throws HttpErrorResponseException If the response status code is not 2xx
169+ * @throws HttpTransportException If an error happens while the http client processes the request
170+ * @throws SerializeException If an error happens during (de-)serialization
178171 */
179172 public function delete (ModelInterface $ model ): bool
180173 {
@@ -208,10 +201,9 @@ protected function getHitCountRelation(string $name): ?CountRelation
208201 /**
209202 * @param Search $search
210203 * @return SearchResult
211- * @throws HttpErrorResponseException
212- * @throws HttpTransportException
213- * @throws OpenSearchException
214- * @throws SerializeException
204+ * @throws HttpErrorResponseException If the response status code is not 2xx
205+ * @throws HttpTransportException If an error happens while the http client processes the request
206+ * @throws SerializeException If an error happens during (de-)serialization
215207 */
216208 public function search (Search $ search ): SearchResult
217209 {
@@ -242,23 +234,32 @@ public function search(Search $search): SearchResult
242234 }
243235
244236 foreach ($ response ->hits ->hits as $ resultDocument ) {
245- if (!isset ($ resultDocument ->_id ) || !is_string ($ resultDocument ->_id )) {
246- throw new SerializeException ("Received invalid document _id from OpenSearch " );
247- }
248-
249- if (isset ($ resultDocument ->_source ) && is_object ($ resultDocument ->_source )) {
250- $ data = get_object_vars ($ resultDocument ->_source );
251- } else {
252- $ data = [];
253- }
254-
255- $ data [$ modelClassName ::getIdField ()] = $ resultDocument ->_id ;
256-
257237 /** @var ModelInterface $model */
258238 $ model = new $ modelClassName ();
259- $ model ->applyData ($ data );
239+ $ model ->applyData ($ this -> getModelData ( $ resultDocument , $ modelClassName ) );
260240 $ result ->add ($ model );
261241 }
262242 return $ result ;
263243 }
244+
245+ /**
246+ * @param stdClass $response
247+ * @param class-string<ModelInterface> $modelClass $modelClass
248+ * @return array
249+ */
250+ public function getModelData (stdClass $ response , string $ modelClass ): array
251+ {
252+ if (!isset ($ response ->_id ) || !is_string ($ response ->_id )) {
253+ throw new SerializeException ("Received invalid document _id from OpenSearch " );
254+ }
255+
256+ if (isset ($ response ->_source ) && is_object ($ response ->_source )) {
257+ $ data = get_object_vars ($ response ->_source );
258+ } else {
259+ $ data = [];
260+ }
261+
262+ $ data [$ modelClass ::getIdField ()] = $ response ->_id ;
263+ return $ data ;
264+ }
264265}
0 commit comments