diff --git a/.gitignore b/.gitignore index 839ccd1..8e30121 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,6 @@ /phpunit.xml log /nbproject/private/ -/nbproject/ \ No newline at end of file +/nbproject/ +/.idea +/composer.lock diff --git a/README.md b/README.md index 3bc65f3..5ebd7c0 100644 --- a/README.md +++ b/README.md @@ -134,7 +134,7 @@ $resultClassmap = array( 'Temperatures' => '\Camcima\Soap\Test\Fixtures\Temperatures', 'ProbabilityOfPrecipiation' => '\Camcima\Soap\Test\Fixtures\ProbabilityOfPrecipiation' ); -$getCityForecastByZIPResult = $soapClient->mapSoapResult($soapResult, 'GetCityForecastByZIPResult', $resultClassmap); +$getCityForecastByZIPResult = $soapClient->mapSoapResult($soapResult, 'GetCityForecastByZIPResult', $resultClassmap, true); ``` The native SOAP client returns all objects as Standard Classes (`StdClass`) and my mapping function "casts" them to the mapped local classes. This functions is based on the property name that holds the standard class object. This works pretty well in most scenarios, but when there is an array of objects, it needs a special config: @@ -157,6 +157,17 @@ If all your result classes reside in the same namespace, there is no need to map $resultClassNamespace = '\MyProject\SOAP\Result\\'; ``` +### Skip Root Object ### + +Sometimes, the webservice will return an object with only one property, so it doesn't make a lot of sense to create a wrapper object just for that. In that case, you can use the `skipRootObject` parameter in the `mapSoapResult` method to skip the root object and return the inner property instead. + +```php +mapObject($soapResult->$rootClassName, $rootClassName, $resultClassMap, $resultClassNamespace); + + if ($skipRootObject) { + $objVarsNames = array_keys(get_object_vars($soapResult)); + $rootClassName = reset($objVarsNames); + $soapResultObj = $this->mapObject($soapResult->$rootClassName, $rootClassName, $resultClassMap, $resultClassNamespace); + } else { + $soapResultObj = $this->mapObject($soapResult, $rootClassName, $resultClassMap, $resultClassNamespace); + } return $soapResultObj; } diff --git a/src/Camcima/Soap/Test/ClientTest.php b/src/Camcima/Soap/Test/ClientTest.php index f0e193d..ae09d7e 100644 --- a/src/Camcima/Soap/Test/ClientTest.php +++ b/src/Camcima/Soap/Test/ClientTest.php @@ -240,7 +240,7 @@ public function testDoRequest() 'Temperatures' => '\Camcima\Soap\Test\Fixtures\Temperatures', 'ProbabilityOfPrecipiation' => '\Camcima\Soap\Test\Fixtures\ProbabilityOfPrecipiation' ); - $getCityForecastByZIPResult = $soapClient->mapSoapResult($soapResult, 'GetCityForecastByZIPResult', $resultClassmap); + $getCityForecastByZIPResult = $soapClient->mapSoapResult($soapResult, 'GetCityForecastByZIPResult', $resultClassmap, '', true); /* @var $getCityForecastByZIPResult \Camcima\Soap\Test\Fixtures\GetCityForecastByZIPResult */ $this->assertInstanceOf('\Camcima\Soap\Test\Fixtures\GetCityForecastByZIPResult', $getCityForecastByZIPResult); $this->assertTrue($getCityForecastByZIPResult->Success); @@ -293,7 +293,7 @@ public function testDoRequestWithNamespace() $resultClassmap = array( 'array|Forecast' => '\Camcima\Soap\Test\Fixtures\ForecastEntry', ); - $getCityForecastByZIPResult = $soapClient->mapSoapResult($soapResult, 'GetCityForecastByZIPResult', $resultClassmap, $resultClassNamespace); + $getCityForecastByZIPResult = $soapClient->mapSoapResult($soapResult, 'GetCityForecastByZIPResult', $resultClassmap, $resultClassNamespace, true); /* @var $getCityForecastByZIPResult \Camcima\Soap\Test\Fixtures\GetCityForecastByZIPResult */ $this->assertInstanceOf('\Camcima\Soap\Test\Fixtures\GetCityForecastByZIPResult', $getCityForecastByZIPResult); $this->assertTrue($getCityForecastByZIPResult->Success);