Skip to content

Commit

Permalink
Included option to skipRootObject.
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlos Cima committed Mar 24, 2016
1 parent 996a57d commit 62b3dc2
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 9 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
/phpunit.xml
log
/nbproject/private/
/nbproject/
/nbproject/
/.idea
/composer.lock
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
<?php

public function mapSoapResult($soapResult, $rootClassName, array $resultClassMap = array(), $resultClassNamespace = '', $skipRootObject = false);
```


## Improvements ##

I plan to include new features according to my needs. If you need a special feature you have two options:
Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@
"psr-0": {
"Camcima": "src/"
}
},
"require-dev": {
"phpunit/phpunit": "^5.2"
}
}
21 changes: 16 additions & 5 deletions src/Camcima/Soap/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,17 +194,28 @@ public function __doRequest($request, $location, $action, $version, $one_way = 0
* @param $rootClassName
* @param array $resultClassMap
* @param string $resultClassNamespace
* @throws \Camcima\Exception\InvalidParameterException
* @param bool $skipRootObject
*
* @return object
*
* @throws InvalidClassMappingException
* @throws InvalidParameterException
* @throws MissingClassMappingException
* @throws \ReflectionException
*/
public function mapSoapResult($soapResult, $rootClassName, array $resultClassMap = array(), $resultClassNamespace = '')
public function mapSoapResult($soapResult, $rootClassName, array $resultClassMap = array(), $resultClassNamespace = '', $skipRootObject = false)
{
if (!is_object($soapResult)) {
throw new InvalidParameterException('Soap Result is not an object');
}
$objVarsNames = array_keys(get_object_vars($soapResult));
$rootClassName = reset($objVarsNames);
$soapResultObj = $this->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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Camcima/Soap/Test/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 62b3dc2

Please sign in to comment.