Large refactoring removing states, abstract magic, vague fluent interfaces

This commit is contained in:
Petr Bechyně
2016-10-27 16:24:44 +02:00
parent c4a9b58b08
commit 0a157748a8
32 changed files with 953 additions and 933 deletions

View File

@ -1,246 +0,0 @@
<?php
/*
* This file is part of the BeSimpleSoapBundle.
*
* (c) Christian Kerl <christian-kerl@web.de>
* (c) Francis Besset <francis.besset@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace BeSimple\SoapCommon;
use BeSimple\SoapCommon\Converter\TypeConverterCollection;
use BeSimple\SoapCommon\Converter\TypeConverterInterface;
/**
* @author Christian Kerl <christian-kerl@web.de>
* @author Francis Besset <francis.besset@gmail.com>
*/
abstract class AbstractSoapBuilder
{
protected $wsdl;
protected $soapOptions = array();
/**
* @return AbstractSoapBuilder
*/
static public function createWithDefaults()
{
$builder = new static();
return $builder
->withSoapVersion12()
->withEncoding('UTF-8')
->withSingleElementArrays()
;
}
public function __construct()
{
$this->soapOptions['features'] = 0;
$this->soapOptions['classmap'] = new Classmap();
$this->soapOptions['typemap'] = new TypeConverterCollection();
}
public function getWsdl()
{
return $this->wsdl;
}
public function getSoapOptions()
{
$options = $this->soapOptions;
$options['classmap'] = $this->soapOptions['classmap']->all();
$options['typemap'] = $this->soapOptions['typemap']->getTypemap();
return $options;
}
/**
* @return AbstractSoapBuilder
*/
public function withWsdl($wsdl)
{
$this->wsdl = $wsdl;
return $this;
}
/**
* @return AbstractSoapBuilder
*/
public function withSoapVersion11()
{
$this->soapOptions['soap_version'] = \SOAP_1_1;
return $this;
}
/**
* @return AbstractSoapBuilder
*/
public function withSoapVersion12()
{
$this->soapOptions['soap_version'] = \SOAP_1_2;
return $this;
}
public function withEncoding($encoding)
{
$this->soapOptions['encoding'] = $encoding;
return $this;
}
public function withWsdlCache($cache)
{
if (!in_array($cache, Cache::getTypes(), true)) {
throw new \InvalidArgumentException();
}
$this->soapOptions['cache_wsdl'] = $cache;
return $this;
}
/**
* @return AbstractSoapBuilder
*/
public function withWsdlCacheNone()
{
$this->soapOptions['cache_wsdl'] = Cache::TYPE_NONE;
return $this;
}
/**
* @return AbstractSoapBuilder
*/
public function withWsdlCacheDisk()
{
$this->soapOptions['cache_wsdl'] = Cache::TYPE_DISK;
return $this;
}
/**
* @return AbstractSoapBuilder
*/
public function withWsdlCacheMemory()
{
$this->soapOptions['cache_wsdl'] = Cache::TYPE_MEMORY;
return $this;
}
/**
* @return AbstractSoapBuilder
*/
public function withWsdlCacheDiskAndMemory()
{
$this->soapOptions['cache_wsdl'] = Cache::TYPE_DISK_MEMORY;
return $this;
}
/**
* Enables the SOAP_SINGLE_ELEMENT_ARRAYS feature.
* If enabled arrays containing only one element will be passed as arrays otherwise the single element is extracted and directly passed.
*
* @return AbstractSoapBuilder
*/
public function withSingleElementArrays()
{
$this->soapOptions['features'] |= \SOAP_SINGLE_ELEMENT_ARRAYS;
return $this;
}
/**
* Enables the SOAP_WAIT_ONE_WAY_CALLS feature.
*
* @return AbstractSoapBuilder
*/
public function withWaitOneWayCalls()
{
$this->soapOptions['features'] |= \SOAP_WAIT_ONE_WAY_CALLS;
return $this;
}
/**
* Enables the SOAP_USE_XSI_ARRAY_TYPE feature.
*
* @return AbstractSoapBuilder
*/
public function withUseXsiArrayType()
{
$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->soapOptions['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->soapOptions['classmap']->addClassmap($classmap);
} else {
$this->soapOptions['classmap']->set($classmap->all());
}
return $this;
}
protected function validateWsdl()
{
if (null === $this->wsdl) {
throw new \InvalidArgumentException('The WSDL has to be configured!');
}
}
}

View File

@ -25,18 +25,23 @@ class Cache
const TYPE_MEMORY = WSDL_CACHE_MEMORY;
const TYPE_DISK_MEMORY = WSDL_CACHE_BOTH;
static protected $types = array(
static protected $types = [
self::TYPE_NONE,
self::TYPE_DISK,
self::TYPE_MEMORY,
self::TYPE_DISK_MEMORY,
);
];
static public function getTypes()
{
return self::$types;
}
static public function hasType($cacheType)
{
return in_array($cacheType, self::$types);
}
static public function isEnabled()
{
return self::iniGet('soap.wsdl_cache_enabled');

View File

@ -15,27 +15,29 @@ namespace BeSimple\SoapCommon;
/**
* @author Francis Besset <francis.besset@gmail.com>
*/
class Classmap
class ClassMap
{
/**
* @var array
*/
protected $classmap = array();
protected $classMap;
public function __construct(array $classMap = [])
{
$this->classmap = [];
foreach ($classMap as $type => $className) {
$this->add($type, $className);
}
}
/**
* @return array
*/
public function all()
public function getAll()
{
return $this->classmap;
return $this->classMap;
}
/**
* @param string $type
*
* @return string
*
* @throws \InvalidArgumentException
*/
public function get($type)
@ -44,39 +46,25 @@ class Classmap
throw new \InvalidArgumentException(sprintf('The type "%s" does not exists', $type));
}
return $this->classmap[$type];
return $this->classMap[$type];
}
/**
* @param string $type
* @param string $classname
*
* @param string $className
* @throws \InvalidArgumentException
*/
public function add($type, $classname)
public function add($type, $className)
{
if ($this->has($type)) {
throw new \InvalidArgumentException(sprintf('The type "%s" already exists', $type));
}
$this->classmap[$type] = $classname;
}
/**
* @param array $classmap
*/
public function set(array $classmap)
{
$this->classmap = array();
foreach ($classmap as $type => $classname) {
$this->add($type, $classname);
}
$this->classMap[$type] = $className;
}
/**
* @param string $type
*
* @return boolean
*/
public function has($type)
@ -84,10 +72,10 @@ class Classmap
return isset($this->classmap[$type]);
}
public function addClassmap(Classmap $classmap)
public function addClassMap(ClassMap $classMap)
{
foreach ($classmap->all() as $type => $classname) {
$this->add($type, $classname);
foreach ($classMap->getAll() as $type => $className) {
$this->add($type, $className);
}
}
}
}

View File

@ -19,7 +19,7 @@ class TypeConverterCollection
{
private $converters = array();
public function all()
public function getAll()
{
return array_values($this->converters);
}
@ -58,7 +58,7 @@ class TypeConverterCollection
public function addCollection(TypeConverterCollection $converterCollection)
{
foreach ($converterCollection->all() as $converter) {
foreach ($converterCollection->getAll() as $converter) {
$this->add($converter);
}
}

View File

@ -0,0 +1,70 @@
<?php
namespace BeSimple\SoapCommon\SoapOptions\SoapFeatures;
use Exception;
class SoapFeatures
{
const SINGLE_ELEMENT_ARRAYS = \SOAP_SINGLE_ELEMENT_ARRAYS;
const WAIT_ONE_WAY_CALLS = \SOAP_WAIT_ONE_WAY_CALLS;
const USE_XSI_ARRAY_TYPE = \SOAP_USE_XSI_ARRAY_TYPE;
private $featuresSum;
private $singleElementArrays = false;
private $oneWayCallsOn = false;
private $useXsiArrayType = false;
/**
* @param array $features array of SoapFeatures::FEATURE_NAME
* @throws Exception
*/
public function __construct(array $features)
{
$this->resolveFeatures($features);
}
public function isSingleElementArrays()
{
return $this->singleElementArrays;
}
public function isOneWayCallsOn()
{
return $this->oneWayCallsOn;
}
public function isUseXsiArrayType()
{
return $this->useXsiArrayType;
}
public function getFeaturesSum()
{
return $this->featuresSum;
}
private function resolveFeatures(array $features)
{
$featuresSum = 0;
foreach ($features as $feature) {
switch ($feature) {
case self::SINGLE_ELEMENT_ARRAYS:
$this->singleElementArrays = true;
$featuresSum += $feature;
break;
case self::WAIT_ONE_WAY_CALLS:
$this->oneWayCallsOn = true;
$featuresSum += $feature;
break;
case self::USE_XSI_ARRAY_TYPE:
$this->useXsiArrayType = true;
$featuresSum += $feature;
break;
default:
throw new Exception('Unknown SOAP feature: ' . $feature);
}
}
$this->featuresSum = $featuresSum;
}
}

View File

@ -0,0 +1,124 @@
<?php
namespace BeSimple\SoapCommon\SoapOptions;
use BeSimple\SoapCommon\Cache;
use BeSimple\SoapCommon\ClassMap;
use BeSimple\SoapCommon\Converter\TypeConverterCollection;
use BeSimple\SoapCommon\Helper;
use BeSimple\SoapCommon\SoapOptions\SoapFeatures\SoapFeatures;
class SoapOptions
{
const SOAP_VERSION_1_1 = \SOAP_1_1;
const SOAP_VERSION_1_2 = \SOAP_1_2;
const SOAP_ENCODING_UTF8 = 'UTF-8';
const SOAP_SINGLE_ELEMENT_ARRAYS_OFF = 0;
const SOAP_CACHE_TYPE_NONE = Cache::TYPE_NONE;
const SOAP_CACHE_TYPE_DISK = Cache::TYPE_DISK;
const SOAP_CACHE_TYPE_MEMORY = Cache::TYPE_MEMORY;
const SOAP_CACHE_TYPE_DISK_MEMORY = Cache::TYPE_DISK_MEMORY;
const SOAP_ATTACHMENTS_OFF = null;
const SOAP_ATTACHMENTS_TYPE_BASE64 = Helper::ATTACHMENTS_TYPE_BASE64;
const SOAP_ATTACHMENTS_TYPE_MTOM = Helper::ATTACHMENTS_TYPE_MTOM;
const SOAP_ATTACHMENTS_TYPE_SWA = Helper::ATTACHMENTS_TYPE_SWA;
protected $soapVersion;
protected $encoding;
protected $soapFeatures;
protected $wsdlFile;
protected $wsdlCacheType;
protected $classMap;
protected $typeConverterCollection;
protected $attachmentType;
/**
* @param int $soapVersion = SoapOptions::SOAP_VERSION_1_1|SoapOptions::SOAP_VERSION_1_2
* @param string $encoding = SoapOptions::SOAP_ENCODING_UTF8
* @param SoapFeatures $features
* @param string $wsdlFile
* @param string $wsdlCacheType = SoapOptions::SOAP_CACHE_TYPE_NONE|SoapOptions::SOAP_CACHE_TYPE_MEMORY|SoapOptions::SOAP_CACHE_TYPE_DISK|SoapOptions::SOAP_CACHE_TYPE_DISK_MEMORY
* @param ClassMap $classMap
* @param TypeConverterCollection $typeConverterCollection
* @param string $attachmentType = SoapOptions::SOAP_ATTACHMENTS_OFF|SoapOptions::SOAP_ATTACHMENTS_TYPE_SWA|SoapOptions::ATTACHMENTS_TYPE_MTOM|SoapOptions::ATTACHMENTS_TYPE_BASE64
*/
public function __construct(
$soapVersion,
$encoding,
SoapFeatures $features,
$wsdlFile,
$wsdlCacheType,
ClassMap $classMap,
TypeConverterCollection $typeConverterCollection,
$attachmentType = null
) {
$this->soapVersion = $soapVersion;
$this->encoding = $encoding;
$this->soapFeatures = $features;
$this->wsdlFile = $wsdlFile;
$this->wsdlCacheType = $wsdlCacheType;
$this->classMap = $classMap;
$this->typeConverterCollection = $typeConverterCollection;
$this->attachmentType = $attachmentType;
}
public function getSoapVersion()
{
return $this->soapVersion;
}
public function getEncoding()
{
return $this->encoding;
}
public function getWsdlFile()
{
return $this->wsdlFile;
}
public function getWsdlCacheType()
{
return $this->wsdlCacheType;
}
public function hasAttachments()
{
return $this->attachmentType !== self::SOAP_ATTACHMENTS_OFF;
}
public function getAttachmentType()
{
return $this->attachmentType;
}
public function getSoapFeatures()
{
return $this->soapFeatures;
}
public function getClassMap()
{
return $this->classMap;
}
public function getTypeConverterCollection()
{
return $this->typeConverterCollection;
}
public function toArray()
{
$optionsAsArray = [
'soap_version' => $this->getSoapVersion(),
'encoding' => $this->getEncoding(),
'features' => $this->getSoapFeatures(),
'wsdl' => $this->getWsdlFile(),
'cache_wsdl' => $this->getWsdlCacheType(),
'classmap' => $this->getClassMap()->getAll(),
'typemap' => $this->getTypeConverterCollection()->getTypemap(),
];
return $optionsAsArray;
}
}

View File

@ -0,0 +1,44 @@
<?php
/*
* This file is part of the BeSimpleSoapBundle.
*
* (c) Christian Kerl <christian-kerl@web.de>
* (c) Francis Besset <francis.besset@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace BeSimple\SoapCommon;
use BeSimple\SoapCommon\Converter\TypeConverterCollection;
use BeSimple\SoapCommon\SoapOptions\SoapFeatures\SoapFeatures;
use BeSimple\SoapCommon\SoapOptions\SoapOptions;
use InvalidArgumentException;
/**
* @author Petr Bechyně <petr.bechyne@vodafone.com>
*/
class SoapOptionsBuilder
{
static public function createWithDefaults($wsdlFile, $wsdlCacheType = Cache::TYPE_NONE)
{
if (!Cache::hasType($wsdlCacheType)) {
throw new InvalidArgumentException;
}
$soapOptions = new SoapOptions(
SoapOptions::SOAP_VERSION_1_2,
SoapOptions::SOAP_ENCODING_UTF8,
new SoapFeatures([
SoapFeatures::SINGLE_ELEMENT_ARRAYS
]),
$wsdlFile,
$wsdlCacheType,
new ClassMap(),
new TypeConverterCollection()
);
return $soapOptions;
}
}

View File

@ -13,7 +13,6 @@
namespace BeSimple\SoapCommon;
use BeSimple\SoapCommon\SoapMessage;
/**
* SOAP request message.
@ -22,5 +21,27 @@ use BeSimple\SoapCommon\SoapMessage;
*/
class SoapRequest extends SoapMessage
{
/**
* Factory function for SoapRequest.
*
* @param string $content Content
* @param string $location Location
* @param string $action SOAP action
* @param string $version SOAP version
*
* @return SoapRequest
*/
public static function create($content, $location, $action, $version)
{
$request = new SoapRequest();
// $content is if unmodified from SoapClient not a php string type!
$request->setContent((string) $content);
$request->setLocation($location);
$request->setAction($action);
$request->setVersion($version);
$contentType = SoapMessage::getContentTypeForVersion($version);
$request->setContentType($contentType);
}
return $request;
}
}

View File

@ -177,7 +177,7 @@ class AbstractSoapBuilderTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(1, count($options['classmap']));
$classmap = new Classmap();
$classmap = new ClassMap();
$classmap->add('bar', __CLASS__);
$builder->withClassmap($classmap);
$options = $builder->getSoapOptions();

View File

@ -0,0 +1,65 @@
<?php
/*
* This file is part of the BeSimpleSoapCommon.
*
* (c) Christian Kerl <christian-kerl@web.de>
* (c) Francis Besset <francis.besset@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace BeSimple\SoapCommon\Tests;
use BeSimple\SoapCommon\ClassMap;
/**
* UnitTest for \BeSimple\SoapCommon\ClassMap.
*
* @author Francis Besset <francis.besset@gmail.com>
*/
class ClassMapTest extends \PHPUnit_Framework_TestCase
{
public function testAll()
{
$classmap = new ClassMap();
$this->assertSame(array(), $classmap->getAll());
}
public function testAdd()
{
$classmap = new ClassMap();
$classmap->add('foobar', 'BeSimple\SoapCommon\ClassMap');
$this->setExpectedException('InvalidArgumentException');
$classmap->add('foobar', 'BeSimple\SoapCommon\ClassMap');
}
public function testGet()
{
$classmap = new ClassMap();
$classmap->add('foobar', 'BeSimple\SoapCommon\ClassMap');
$this->assertSame('BeSimple\SoapCommon\ClassMap', $classmap->get('foobar'));
$this->setExpectedException('InvalidArgumentException');
$classmap->get('bar');
}
public function testAddClassMap()
{
$classmap1 = new ClassMap();
$classmap2 = new ClassMap();
$classmap2->add('foobar', 'BeSimple\SoapCommon\ClassMap');
$classmap1->addClassMap($classmap2);
$this->assertEquals(array('foobar' => 'BeSimple\SoapCommon\ClassMap'), $classmap1->getAll());
$this->setExpectedException('InvalidArgumentException');
$classmap1->addClassMap($classmap2);
}
}

View File

@ -1,81 +0,0 @@
<?php
/*
* This file is part of the BeSimpleSoapCommon.
*
* (c) Christian Kerl <christian-kerl@web.de>
* (c) Francis Besset <francis.besset@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace BeSimple\SoapCommon\Tests;
use BeSimple\SoapCommon\Classmap;
/**
* UnitTest for \BeSimple\SoapCommon\Classmap.
*
* @author Francis Besset <francis.besset@gmail.com>
*/
class ClassmapTest extends \PHPUnit_Framework_TestCase
{
public function testAll()
{
$classmap = new Classmap();
$this->assertSame(array(), $classmap->all());
}
public function testAdd()
{
$classmap = new Classmap();
$classmap->add('foobar', 'BeSimple\SoapCommon\Classmap');
$this->setExpectedException('InvalidArgumentException');
$classmap->add('foobar', 'BeSimple\SoapCommon\Classmap');
}
public function testGet()
{
$classmap = new Classmap();
$classmap->add('foobar', 'BeSimple\SoapCommon\Classmap');
$this->assertSame('BeSimple\SoapCommon\Classmap', $classmap->get('foobar'));
$this->setExpectedException('InvalidArgumentException');
$classmap->get('bar');
}
public function testSet()
{
$classmap = new Classmap();
$classmap->add('foobar', 'BeSimple\SoapCommon\Tests\ClassmapTest');
$classmap->add('foo', 'BeSimple\SoapCommon\Tests\Classmap');
$map = array(
'foobar' => 'BeSimple\SoapCommon\Classmap',
'barfoo' => 'BeSimple\SoapCommon\Tests\ClassmapTest',
);
$classmap->set($map);
$this->assertSame($map, $classmap->all());
}
public function testAddClassmap()
{
$classmap1 = new Classmap();
$classmap2 = new Classmap();
$classmap2->add('foobar', 'BeSimple\SoapCommon\Classmap');
$classmap1->addClassmap($classmap2);
$this->assertEquals(array('foobar' => 'BeSimple\SoapCommon\Classmap'), $classmap1->all());
$this->setExpectedException('InvalidArgumentException');
$classmap1->addClassmap($classmap2);
}
}

View File

@ -30,12 +30,12 @@ class TypeConverterCollectionTest extends \PHPUnit_Framework_TestCase
$dateTimeTypeConverter = new DateTimeTypeConverter();
$converters->add($dateTimeTypeConverter);
$this->assertSame(array($dateTimeTypeConverter), $converters->all());
$this->assertSame(array($dateTimeTypeConverter), $converters->getAll());
$dateTypeConverter = new DateTypeConverter();
$converters->add($dateTypeConverter);
$this->assertSame(array($dateTimeTypeConverter, $dateTypeConverter), $converters->all());
$this->assertSame(array($dateTimeTypeConverter, $dateTypeConverter), $converters->getAll());
}
public function testGetTypemap()
@ -73,7 +73,7 @@ class TypeConverterCollectionTest extends \PHPUnit_Framework_TestCase
$converter = array(new DateTypeConverter);
$converters->set($converter);
$this->assertSame($converter, $converters->all());
$this->assertSame($converter, $converters->getAll());
}
public function testAddCollection()
@ -85,7 +85,7 @@ class TypeConverterCollectionTest extends \PHPUnit_Framework_TestCase
$converters2->add($dateTimeTypeConverter);
$converters1->addCollection($converters2);
$this->assertSame(array($dateTimeTypeConverter), $converters1->all());
$this->assertSame(array($dateTimeTypeConverter), $converters1->getAll());
$this->setExpectedException('InvalidArgumentException');
$converters1->addCollection($converters2);