diff --git a/src/BeSimple/SoapCommon/AbstractSoapBuilder.php b/src/BeSimple/SoapCommon/AbstractSoapBuilder.php index effa9a5..b3bf31d 100644 --- a/src/BeSimple/SoapCommon/AbstractSoapBuilder.php +++ b/src/BeSimple/SoapCommon/AbstractSoapBuilder.php @@ -12,7 +12,8 @@ namespace BeSimple\SoapCommon; -use BeSimple\SoapCommon\Cache; +use BeSimple\SoapCommon\Converter\TypeConverterCollection; +use BeSimple\SoapCommon\Converter\TypeConverterInterface; /** * @author Christian Kerl @@ -21,7 +22,7 @@ use BeSimple\SoapCommon\Cache; abstract class AbstractSoapBuilder { protected $wsdl; - protected $options; + protected $soapOptions; /** * @return AbstractSoapBuilder @@ -30,19 +31,19 @@ abstract class AbstractSoapBuilder { $builder = new static(); - $builder + return $builder ->withSoapVersion12() ->withEncoding('UTF-8') ->withSingleElementArrays() ; - - return $builder; } public function __construct() { - $this->options = array( + $this->soapOptions = array( 'features' => 0, + 'classmap' => new Classmap(), + 'typemap' => new TypeConverterCollection(), ); } @@ -51,9 +52,14 @@ abstract class AbstractSoapBuilder return $this->wsdl; } - public function getOptions() + public function getSoapOptions() { - return $this->options; + $options = $this->soapOptions; + + $options['classmap'] = $this->soapOptions['classmap']->all(); + $options['typemap'] = $this->soapOptions['typemap']->getTypemap(); + + return $options; } /** @@ -71,7 +77,7 @@ abstract class AbstractSoapBuilder */ public function withSoapVersion11() { - $this->options['soap_version'] = SOAP_1_1; + $this->soapOptions['soap_version'] = SOAP_1_1; return $this; } @@ -81,14 +87,14 @@ abstract class AbstractSoapBuilder */ public function withSoapVersion12() { - $this->options['soap_version'] = SOAP_1_2; + $this->soapOptions['soap_version'] = SOAP_1_2; return $this; } public function withEncoding($encoding) { - $this->options['encoding'] = $encoding; + $this->soapOptions['encoding'] = $encoding; return $this; } @@ -98,7 +104,7 @@ abstract class AbstractSoapBuilder */ public function withWsdlCacheNone() { - $this->options['cache_wsdl'] = Cache::TYPE_NONE; + $this->soapOptions['cache_wsdl'] = Cache::TYPE_NONE; return $this; } @@ -108,7 +114,7 @@ abstract class AbstractSoapBuilder */ public function withWsdlCacheDisk() { - $this->options['cache_wsdl'] = Cache::TYPE_DISK; + $this->soapOptions['cache_wsdl'] = Cache::TYPE_DISK; return $this; } @@ -118,7 +124,7 @@ abstract class AbstractSoapBuilder */ public function withWsdlCacheMemory() { - $this->options['cache_wsdl'] = Cache::TYPE_MEMORY; + $this->soapOptions['cache_wsdl'] = Cache::TYPE_MEMORY; return $this; } @@ -128,7 +134,7 @@ abstract class AbstractSoapBuilder */ public function withWsdlCacheDiskAndMemory() { - $this->options['cache_wsdl'] = Cache::TYPE_DISK_MEMORY; + $this->soapOptions['cache_wsdl'] = Cache::TYPE_DISK_MEMORY; return $this; } @@ -141,7 +147,7 @@ abstract class AbstractSoapBuilder */ public function withSingleElementArrays() { - $this->options['features'] |= SOAP_SINGLE_ELEMENT_ARRAYS; + $this->soapOptions['features'] |= SOAP_SINGLE_ELEMENT_ARRAYS; return $this; } @@ -153,7 +159,7 @@ abstract class AbstractSoapBuilder */ public function withWaitOneWayCalls() { - $this->options['features'] |= SOAP_WAIT_ONE_WAY_CALLS; + $this->soapOptions['features'] |= SOAP_WAIT_ONE_WAY_CALLS; return $this; } @@ -165,7 +171,59 @@ abstract class AbstractSoapBuilder */ public function withUseXsiArrayType() { - $this->options['features'] |= SOAP_USE_XSI_ARRAY_TYPE; + $this->soapOptions['features'] |= SOAP_USE_XSI_ARRAY_TYPE; + + return $this; + } + + public function withTypeConverter(TypeConverterInterface $converter) + { + $this->soapOptions['typemap']->add($converter); + + return $this; + } + + public function withTypeConverters(TypeConverterCollection $converters, $merge = true) + { + if ($merge) { + $this->soapOptions['typemap']->addCollection($converters); + } else { + $this->soapOptions['typemap']->set($converters->all()); + } + + return $this; + } + + /** + * Adds a class mapping to the classmap. + * + * @param string $xmlType + * @param string $phpType + * + * @return AbstractSoapBuilder + */ + public function withClassMapping($xmlType, $phpType) + { + $this->options['classmap']->add($xmlType, $phpType); + + return $this; + } + + /** + * Sets the classmap. + * + * @param array $classmap The classmap. + * @param boolean $merge If true the given classmap is merged into the existing one, otherwise the existing one is overwritten. + * + * @return AbstractSoapBuilder + */ + public function withClassmap(Classmap $classmap, $merge = true) + { + if ($merge) { + $this->options['classmap']->addClassmap($classmap); + } else { + $this->options['classmap']->set($classmap->all()); + } return $this; } diff --git a/tests/BeSimple/Tests/SoapCommon/AbstractSoapBuilderTest.php b/tests/BeSimple/Tests/SoapCommon/AbstractSoapBuilderTest.php index f0c3938..ff7dc43 100644 --- a/tests/BeSimple/Tests/SoapCommon/AbstractSoapBuilderTest.php +++ b/tests/BeSimple/Tests/SoapCommon/AbstractSoapBuilderTest.php @@ -19,13 +19,15 @@ class AbstractSoapBuilderTest extends \PHPUnit_Framework_TestCase { private $defaultOptions = array( 'features' => 0, + 'classmap' => array(), + 'typemap' => array(), ); public function testContruct() { $options = $this ->getSoapBuilder() - ->getOptions() + ->getSoapOptions() ; $this->assertEquals($this->mergeOptions(array()), $options); @@ -45,10 +47,10 @@ class AbstractSoapBuilderTest extends \PHPUnit_Framework_TestCase $builder = $this->getSoapBuilder(); $builder->withSoapVersion11(); - $this->assertEquals($this->mergeOptions(array('soap_version' => SOAP_1_1)), $builder->getOptions()); + $this->assertEquals($this->mergeOptions(array('soap_version' => SOAP_1_1)), $builder->getSoapOptions()); $builder->withSoapVersion12(); - $this->assertEquals($this->mergeOptions(array('soap_version' => SOAP_1_2)), $builder->getOptions()); + $this->assertEquals($this->mergeOptions(array('soap_version' => SOAP_1_2)), $builder->getSoapOptions()); } public function testWithEncoding() @@ -58,7 +60,7 @@ class AbstractSoapBuilderTest extends \PHPUnit_Framework_TestCase ->withEncoding('ISO 8859-15') ; - $this->assertEquals($this->mergeOptions(array('encoding' => 'ISO 8859-15')), $builder->getOptions()); + $this->assertEquals($this->mergeOptions(array('encoding' => 'ISO 8859-15')), $builder->getSoapOptions()); } public function testWithWsdlCache() @@ -66,16 +68,16 @@ class AbstractSoapBuilderTest extends \PHPUnit_Framework_TestCase $builder = $this->getSoapBuilder(); $builder->withWsdlCacheNone(); - $this->assertEquals($this->mergeOptions(array('cache_wsdl' => Cache::TYPE_NONE)), $builder->getOptions()); + $this->assertEquals($this->mergeOptions(array('cache_wsdl' => Cache::TYPE_NONE)), $builder->getSoapOptions()); $builder->withWsdlCacheDisk(); - $this->assertEquals($this->mergeOptions(array('cache_wsdl' => Cache::TYPE_DISK)), $builder->getOptions()); + $this->assertEquals($this->mergeOptions(array('cache_wsdl' => Cache::TYPE_DISK)), $builder->getSoapOptions()); $builder->withWsdlCacheMemory(); - $this->assertEquals($this->mergeOptions(array('cache_wsdl' => Cache::TYPE_MEMORY)), $builder->getOptions()); + $this->assertEquals($this->mergeOptions(array('cache_wsdl' => Cache::TYPE_MEMORY)), $builder->getSoapOptions()); $builder->withWsdlCacheDiskAndMemory(); - $this->assertEquals($this->mergeOptions(array('cache_wsdl' => Cache::TYPE_DISK_MEMORY)), $builder->getOptions()); + $this->assertEquals($this->mergeOptions(array('cache_wsdl' => Cache::TYPE_DISK_MEMORY)), $builder->getSoapOptions()); } public function testWithSingleElementArrays() @@ -83,7 +85,7 @@ class AbstractSoapBuilderTest extends \PHPUnit_Framework_TestCase $options = $this ->getSoapBuilder() ->withSingleElementArrays() - ->getOptions() + ->getSoapOptions() ; $this->assertEquals($this->mergeOptions(array('features' => SOAP_SINGLE_ELEMENT_ARRAYS)), $options); @@ -94,7 +96,7 @@ class AbstractSoapBuilderTest extends \PHPUnit_Framework_TestCase $options = $this ->getSoapBuilder() ->withWaitOneWayCalls() - ->getOptions() + ->getSoapOptions() ; $this->assertEquals($this->mergeOptions(array('features' => SOAP_WAIT_ONE_WAY_CALLS)), $options); @@ -105,7 +107,7 @@ class AbstractSoapBuilderTest extends \PHPUnit_Framework_TestCase $options = $this ->getSoapBuilder() ->withUseXsiArrayType() - ->getOptions() + ->getSoapOptions() ; $this->assertEquals($this->mergeOptions(array('features' => SOAP_USE_XSI_ARRAY_TYPE)), $options); @@ -118,15 +120,15 @@ class AbstractSoapBuilderTest extends \PHPUnit_Framework_TestCase $builder->withSingleElementArrays(); $features |= SOAP_SINGLE_ELEMENT_ARRAYS; - $this->assertEquals($this->mergeOptions(array('features' => $features)), $builder->getOptions()); + $this->assertEquals($this->mergeOptions(array('features' => $features)), $builder->getSoapOptions()); $builder->withWaitOneWayCalls(); $features |= SOAP_WAIT_ONE_WAY_CALLS; - $this->assertEquals($this->mergeOptions(array('features' => $features)), $builder->getOptions()); + $this->assertEquals($this->mergeOptions(array('features' => $features)), $builder->getSoapOptions()); $builder->withUseXsiArrayType(); $features |= SOAP_USE_XSI_ARRAY_TYPE; - $this->assertEquals($this->mergeOptions(array('features' => $features)), $builder->getOptions()); + $this->assertEquals($this->mergeOptions(array('features' => $features)), $builder->getSoapOptions()); } public function testCreateWithDefaults() @@ -135,7 +137,7 @@ class AbstractSoapBuilderTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('BeSimple\Tests\SoapCommon\Fixtures\SoapBuilder', $builder); - $this->assertEquals($this->mergeOptions(array('soap_version' => SOAP_1_2, 'encoding' => 'UTF-8', 'features' => SOAP_SINGLE_ELEMENT_ARRAYS)), $builder->getOptions()); + $this->assertEquals($this->mergeOptions(array('soap_version' => SOAP_1_2, 'encoding' => 'UTF-8', 'features' => SOAP_SINGLE_ELEMENT_ARRAYS)), $builder->getSoapOptions()); } private function getSoapBuilder()