Added TypeConverterCollection::getTypemap() function

This commit is contained in:
Francis Besset 2011-09-13 20:58:46 +02:00
parent 9b0ec8816d
commit c2a2413330
2 changed files with 48 additions and 0 deletions

View File

@ -28,4 +28,27 @@ class TypeConverterCollection
{
return $this->typeConverters;
}
/**
* @return array
*/
public function getTypemap()
{
$typemap = array();
foreach ($this->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;
}
}

View File

@ -37,4 +37,29 @@ class TypeConverterCollectionTest extends \PHPUnit_Framework_TestCase
$this->assertSame(array($dateTimeTypeConverter, $dateTypeConverter), $converters->all());
}
public function testGetTypemap()
{
$converters = new TypeConverterCollection();
$this->assertEquals(array(), $converters->getTypemap());
$dateTimeTypeConverter = new DateTimeTypeConverter();
$converters->add($dateTimeTypeConverter);
$dateTypeConverter = new DateTypeConverter();
$converters->add($dateTypeConverter);
$typemap = $converters->getTypemap();
$this->assertEquals('http://www.w3.org/2001/XMLSchema', $typemap[0]['type_ns']);
$this->assertEquals('dateTime', $typemap[0]['type_name']);
$this->assertInstanceOf('Closure', $typemap[0]['from_xml']);
$this->assertInstanceOf('Closure', $typemap[0]['to_xml']);
$this->assertEquals('http://www.w3.org/2001/XMLSchema', $typemap[1]['type_ns']);
$this->assertEquals('date', $typemap[1]['type_name']);
$this->assertInstanceOf('Closure', $typemap[1]['from_xml']);
$this->assertInstanceOf('Closure', $typemap[1]['to_xml']);
}
}