@@ -167,10 +167,7 @@ public function request($request, int $mode = self::RESPONSE_SHORT): XmlResponse
167167 }
168168
169169 if ('sdk ' == $ this ->protocol ) {
170- $ version = ('' == $ this ->version ) ? null : $ this ->version ;
171- $ requestXml = new SimpleXMLElement ((string ) $ request );
172- /** @psalm-suppress UndefinedClass */
173- $ xml = \pm_ApiRpc::getService ($ version )->call ($ requestXml ->children ()[0 ]->asXml (), $ this ->login );
170+ $ xml = $ this ->performSdkCall ((string ) $ request );
174171 } else {
175172 $ xml = $ this ->performHttpRequest ((string ) $ request );
176173 }
@@ -179,8 +176,25 @@ public function request($request, int $mode = self::RESPONSE_SHORT): XmlResponse
179176 ? call_user_func ($ this ->verifyResponseCallback , $ xml )
180177 : $ this ->verifyResponse ($ xml );
181178
182- $ result = (self ::RESPONSE_FULL === $ mode ) ? $ xml : $ xml ->xpath ('//result ' )[0 ];
183- return new XmlResponse ((string ) $ result ->asXML ());
179+ $ result = (self ::RESPONSE_FULL === $ mode )
180+ ? $ xml
181+ : ($ xml ->xpath ('//result ' ) ?: [null ])[0 ];
182+
183+ return new XmlResponse ($ result ? (string ) $ result ->asXML () : '' );
184+ }
185+
186+ private function performSdkCall (string $ request ): XmlResponse
187+ {
188+ $ version = ('' == $ this ->version ) ? null : $ this ->version ;
189+
190+ $ requestXml = new SimpleXMLElement ($ request );
191+ $ innerNodes = $ requestXml ->children ();
192+ $ innerXml = $ innerNodes && count ($ innerNodes ) > 0 && $ innerNodes [0 ] ? $ innerNodes [0 ]->asXml () : '' ;
193+
194+ /** @psalm-suppress UndefinedClass */
195+ $ result = \pm_ApiRpc::getService ($ version )->call ($ innerXml , $ this ->login );
196+
197+ return new XmlResponse ($ result ? (string ) $ result ->asXML () : '' );
184198 }
185199
186200 /**
@@ -265,7 +279,12 @@ private function splitResponseToArray(XmlResponse $responseXml, $mode = self::RE
265279 {
266280 $ responses = [];
267281
268- foreach ($ responseXml ->children () as $ childNode ) {
282+ $ nodes = $ responseXml ->children ();
283+ if (!$ nodes ) {
284+ return [];
285+ }
286+
287+ foreach ($ nodes as $ childNode ) {
269288 $ dom = $ this ->getDomDocument ($ this ->getPacket ());
270289 if (!$ dom ) {
271290 continue ;
@@ -278,7 +297,13 @@ private function splitResponseToArray(XmlResponse $responseXml, $mode = self::RE
278297 }
279298
280299 $ response = simplexml_load_string ($ dom ->saveXML ());
281- $ responses [] = (self ::RESPONSE_FULL == $ mode ) ? $ response : $ response ->xpath ('//result ' )[0 ];
300+ if (!$ response ) {
301+ return [];
302+ }
303+
304+ $ responses [] = (self ::RESPONSE_FULL == $ mode )
305+ ? $ response
306+ : ($ response ->xpath ('//result ' ) ?: [null ])[0 ];
282307 }
283308
284309 return $ responses ;
@@ -330,8 +355,8 @@ private function verifyResponse($xml): void
330355 }
331356
332357 if ($ xml ->xpath ('//status[text()="error"] ' ) && $ xml ->xpath ('//errcode ' ) && $ xml ->xpath ('//errtext ' )) {
333- $ errorCode = (int ) $ xml ->xpath ('//errcode ' )[0 ];
334- $ errorMessage = (string ) $ xml ->xpath ('//errtext ' )[0 ];
358+ $ errorCode = (int ) ( $ xml ->xpath ('//errcode ' ) ?: [ null ] )[0 ];
359+ $ errorMessage = (string ) ( $ xml ->xpath ('//errtext ' ) ?: [ null ] )[0 ];
335360
336361 throw new Exception ($ errorMessage , $ errorCode );
337362 }
0 commit comments