[TypeConverterCollection] Added get, set, has and addCollection methods
This commit is contained in:
parent
fffc5f71da
commit
676d5cb108
|
@ -17,16 +17,50 @@ namespace BeSimple\SoapCommon\Converter;
|
||||||
*/
|
*/
|
||||||
class TypeConverterCollection
|
class TypeConverterCollection
|
||||||
{
|
{
|
||||||
private $typeConverters = array();
|
private $converters = array();
|
||||||
|
|
||||||
public function add(TypeConverterInterface $converter)
|
|
||||||
{
|
|
||||||
$this->typeConverters[] = $converter;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function all()
|
public function all()
|
||||||
{
|
{
|
||||||
return $this->typeConverters;
|
return array_values($this->converters);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get($namespace, $name)
|
||||||
|
{
|
||||||
|
if (!$this->has($namespace, $name)) {
|
||||||
|
throw new \InvalidArgumentException(sprintf('The converter "%s %s" does not exists', $namespace, $name));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->converters[$namespace.':'.$name];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function add(TypeConverterInterface $converter)
|
||||||
|
{
|
||||||
|
if ($this->has($converter->getTypeNamespace(), $converter->getTypeName())) {
|
||||||
|
throw new \InvalidArgumentException(sprintf('The converter "%s %s" already exists', $converter->getTypeNamespace(), $converter->getTypeName()));
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->converters[$converter->getTypeNamespace().':'.$converter->getTypeName()] = $converter;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function set(array $converters)
|
||||||
|
{
|
||||||
|
$this->converters = array();
|
||||||
|
|
||||||
|
foreach ($converters as $converter) {
|
||||||
|
$this->add($converter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function has($namespace, $name)
|
||||||
|
{
|
||||||
|
return isset($this->converters[$namespace.':'.$name]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addCollection(TypeConverterCollection $converterCollection)
|
||||||
|
{
|
||||||
|
foreach ($converterCollection->all() as $converter) {
|
||||||
|
$this->add($converter);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -36,15 +70,15 @@ class TypeConverterCollection
|
||||||
{
|
{
|
||||||
$typemap = array();
|
$typemap = array();
|
||||||
|
|
||||||
foreach ($this->all() as $typeConverter) {
|
foreach ($this->converters as $converter) {
|
||||||
$typemap[] = array(
|
$typemap[] = array(
|
||||||
'type_name' => $typeConverter->getTypeName(),
|
'type_name' => $converter->getTypeName(),
|
||||||
'type_ns' => $typeConverter->getTypeNamespace(),
|
'type_ns' => $converter->getTypeNamespace(),
|
||||||
'from_xml' => function($input) use ($typeConverter) {
|
'from_xml' => function($input) use ($converter) {
|
||||||
return $typeConverter->convertXmlToPhp($input);
|
return $converter->convertXmlToPhp($input);
|
||||||
},
|
},
|
||||||
'to_xml' => function($input) use ($typeConverter) {
|
'to_xml' => function($input) use ($converter) {
|
||||||
return $typeConverter->convertPhpToXml($input);
|
return $converter->convertPhpToXml($input);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -62,4 +62,32 @@ class TypeConverterCollectionTest extends \PHPUnit_Framework_TestCase
|
||||||
$this->assertInstanceOf('Closure', $typemap[1]['from_xml']);
|
$this->assertInstanceOf('Closure', $typemap[1]['from_xml']);
|
||||||
$this->assertInstanceOf('Closure', $typemap[1]['to_xml']);
|
$this->assertInstanceOf('Closure', $typemap[1]['to_xml']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSet()
|
||||||
|
{
|
||||||
|
$converters = new TypeConverterCollection();
|
||||||
|
|
||||||
|
$dateTimeTypeConverter = new DateTimeTypeConverter();
|
||||||
|
$converters->add($dateTimeTypeConverter);
|
||||||
|
|
||||||
|
$converter = array(new DateTypeConverter);
|
||||||
|
$converters->set($converter);
|
||||||
|
|
||||||
|
$this->assertSame($converter, $converters->all());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAddCollection()
|
||||||
|
{
|
||||||
|
$converters1 = new TypeConverterCollection();
|
||||||
|
$converters2 = new TypeConverterCollection();
|
||||||
|
|
||||||
|
$dateTimeTypeConverter = new DateTimeTypeConverter();
|
||||||
|
$converters2->add($dateTimeTypeConverter);
|
||||||
|
$converters1->addCollection($converters2);
|
||||||
|
|
||||||
|
$this->assertSame(array($dateTimeTypeConverter), $converters1->all());
|
||||||
|
|
||||||
|
$this->setExpectedException('InvalidArgumentException');
|
||||||
|
$converters1->addCollection($converters2);
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue