From c1c5d313504d0dae6a757344656780776cb62633 Mon Sep 17 00:00:00 2001 From: Francis Besset Date: Sat, 10 Sep 2011 19:33:27 +0200 Subject: [PATCH] Added TypeConverterCollection in SoapClient --- src/BeSimple/SoapClient/SoapClient.php | 37 ++++++++++++++++-- src/BeSimple/SoapClient/SoapRequest.php | 2 +- .../Tests/SoapClient/SoapClientTest.php | 39 ++++++++++++++++--- tests/bootstrap.php | 4 +- 4 files changed, 70 insertions(+), 12 deletions(-) diff --git a/src/BeSimple/SoapClient/SoapClient.php b/src/BeSimple/SoapClient/SoapClient.php index 85eac14..48a471f 100644 --- a/src/BeSimple/SoapClient/SoapClient.php +++ b/src/BeSimple/SoapClient/SoapClient.php @@ -1,7 +1,7 @@ * (c) Francis Besset @@ -13,6 +13,7 @@ namespace BeSimple\SoapClient; use BeSimple\SoapCommon\Cache; +use BeSimple\SoapCommon\Converter\TypeConverterCollection; /** * @author Francis Besset @@ -20,15 +21,17 @@ use BeSimple\SoapCommon\Cache; class SoapClient { protected $wsdl; + protected $converters; protected $soapClient; /** * @param string $wsdl * @param array $options */ - public function __construct($wsdl, array $options = array()) + public function __construct($wsdl, TypeConverterCollection $converters = null, array $options = array()) { - $this->wsdl = $wsdl; + $this->wsdl = $wsdl; + $this->converters = $converters; $this->setOptions($options); } @@ -160,7 +163,35 @@ class SoapClient $options['cache_wsdl'] = $this->options['cache_type']; $options['trace'] = $this->options['debug']; + $options['typemap'] = $this->getTypemap(); return $options; } + + /** + * @return array + */ + protected function getTypemap() + { + $typemap = array(); + + if (!$this->converters) { + return $typemap; + } + + foreach ($this->converters->all() as $typeConverter) { + $typemap[] = array( + 'type_name' => $typeConverter->getTypeName(), + 'type_ns' => $typeConverter->getTypeNamespace(), + 'from_xml' => function($input) use ($typeConverter) { + return $typeConverter->convertXmlToPhp($input); + }, + 'to_xml' => function($input) use ($typeConverter) { + return $typeConverter->convertPhpToXml($input); + }, + ); + } + + return $typemap; + } } \ No newline at end of file diff --git a/src/BeSimple/SoapClient/SoapRequest.php b/src/BeSimple/SoapClient/SoapRequest.php index 8973ab9..178a26a 100644 --- a/src/BeSimple/SoapClient/SoapRequest.php +++ b/src/BeSimple/SoapClient/SoapRequest.php @@ -1,7 +1,7 @@ * (c) Francis Besset diff --git a/tests/BeSimple/Tests/SoapClient/SoapClientTest.php b/tests/BeSimple/Tests/SoapClient/SoapClientTest.php index 985351f..34e9561 100644 --- a/tests/BeSimple/Tests/SoapClient/SoapClientTest.php +++ b/tests/BeSimple/Tests/SoapClient/SoapClientTest.php @@ -13,6 +13,9 @@ namespace BeSimple\Tests\SoapClient; use BeSimple\SoapCommon\Cache; +use BeSimple\SoapCommon\Converter\DateTimeTypeConverter; +use BeSimple\SoapCommon\Converter\DateTypeConverter; +use BeSimple\SoapCommon\Converter\TypeConverterCollection; use BeSimple\SoapClient\SoapClient; class SoapClientTest extends \PHPUnit_Framework_TestCase @@ -64,7 +67,7 @@ class SoapClientTest extends \PHPUnit_Framework_TestCase public function testCreateSoapHeader() { - $soapClient = new SoapClient('foo.wsdl', array('namespace' => 'http://foobar/soap/User/1.0/')); + $soapClient = new SoapClient('foo.wsdl', null, array('namespace' => 'http://foobar/soap/User/1.0/')); $soapHeader = $soapClient->createSoapHeader('foo', 'bar'); $this->assertInstanceOf('SoapHeader', $soapHeader); @@ -84,16 +87,40 @@ class SoapClientTest extends \PHPUnit_Framework_TestCase public function testGetSoapOptions() { Cache::setType(Cache::TYPE_MEMORY); - $soapClient = new SoapClient('foo.wsdl', array('debug' => true)); - $this->assertEquals(array('cache_wsdl' => Cache::getType(), 'trace' => true), $soapClient->getSoapOptions()); + $soapClient = new SoapClient('foo.wsdl', null, array('debug' => true)); + $this->assertEquals(array('cache_wsdl' => Cache::getType(), 'trace' => true, 'typemap' => array()), $soapClient->getSoapOptions()); - $soapClient = new SoapClient('foo.wsdl', array('debug' => false, 'cache_type' => Cache::TYPE_NONE)); - $this->assertEquals(array('cache_wsdl' => Cache::TYPE_NONE, 'trace' => false), $soapClient->getSoapOptions()); + $soapClient = new SoapClient('foo.wsdl', null, array('debug' => false, 'cache_type' => Cache::TYPE_NONE)); + $this->assertEquals(array('cache_wsdl' => Cache::TYPE_NONE, 'trace' => false, 'typemap' => array()), $soapClient->getSoapOptions()); + } + + public function testGetSoapOptionsWithTypemap() + { + $converters = new TypeConverterCollection(); + + $dateTimeTypeConverter = new DateTimeTypeConverter(); + $converters->add($dateTimeTypeConverter); + + $dateTypeConverter = new DateTypeConverter(); + $converters->add($dateTypeConverter); + + $soapClient = new SoapClient('foo.wsdl', $converters); + $soapOptions = $soapClient->getSoapOptions(); + + $this->assertEquals('http://www.w3.org/2001/XMLSchema', $soapOptions['typemap'][0]['type_ns']); + $this->assertEquals('dateTime', $soapOptions['typemap'][0]['type_name']); + $this->assertInstanceOf('Closure', $soapOptions['typemap'][0]['from_xml']); + $this->assertInstanceOf('Closure', $soapOptions['typemap'][0]['to_xml']); + + $this->assertEquals('http://www.w3.org/2001/XMLSchema', $soapOptions['typemap'][1]['type_ns']); + $this->assertEquals('date', $soapOptions['typemap'][1]['type_name']); + $this->assertInstanceOf('Closure', $soapOptions['typemap'][1]['from_xml']); + $this->assertInstanceOf('Closure', $soapOptions['typemap'][1]['to_xml']); } public function testGetNativeSoapClient() { - $soapClient = new SoapClient(__DIR__.'/Fixtures/foobar.wsdl', array('debug' => true)); + $soapClient = new SoapClient(__DIR__.'/Fixtures/foobar.wsdl', null, array('debug' => true)); $this->assertInstanceOf('SoapClient', $soapClient->getNativeSoapClient()); } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index fdc4599..2e0638c 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -11,14 +11,14 @@ spl_autoload_register(function($class) { return true; } - } else if (0 === strpos($class, 'BeSimple\SoapClient\\')) { + } elseif (0 === strpos($class, 'BeSimple\SoapClient\\')) { $path = __DIR__.'/../src/'.($class = strtr($class, '\\', '/')).'.php'; if (file_exists($path) && is_readable($path)) { require_once $path; return true; } - } else if (0 === strpos($class, 'BeSimple\SoapCommon\\')) { + } elseif (0 === strpos($class, 'BeSimple\SoapCommon\\')) { $path = __DIR__.'/../vendor/besimple-soapcommon/src/'.($class = strtr($class, '\\', '/')).'.php'; if (file_exists($path) && is_readable($path)) { require_once $path;