From c18cede7ab2b3a8288b7a3759f99c641d011785c Mon Sep 17 00:00:00 2001 From: Francis Besset Date: Mon, 29 Aug 2011 21:34:49 +0200 Subject: [PATCH] Added xsd:date type and DateTypeConverter --- Converter/DateTimeTypeConverter.php | 2 +- Converter/DateTypeConverter.php | 44 +++++++++++++++++++++++ Resources/config/converters.xml | 5 +++ Resources/config/webservice.xml | 4 +++ Tests/Converter/DateTypeConverterTest.php | 41 +++++++++++++++++++++ 5 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 Converter/DateTypeConverter.php create mode 100644 Tests/Converter/DateTypeConverterTest.php diff --git a/Converter/DateTimeTypeConverter.php b/Converter/DateTimeTypeConverter.php index 47a9ace..9f692fc 100644 --- a/Converter/DateTimeTypeConverter.php +++ b/Converter/DateTimeTypeConverter.php @@ -39,6 +39,6 @@ class DateTimeTypeConverter implements TypeConverterInterface public function convertPhpToXml(SoapResponse $response, $data) { - return sprintf('%s', $data->format('Y-m-d\TH:i:sP')); + return sprintf('<%1$s>%2$s', $this->getTypeName(), $data->format('Y-m-d\TH:i:sP')); } } diff --git a/Converter/DateTypeConverter.php b/Converter/DateTypeConverter.php new file mode 100644 index 0000000..61f7119 --- /dev/null +++ b/Converter/DateTypeConverter.php @@ -0,0 +1,44 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace BeSimple\SoapBundle\Converter; + +use BeSimple\SoapBundle\Soap\SoapRequest; +use BeSimple\SoapBundle\Soap\SoapResponse; +use BeSimple\SoapBundle\Util\String; + +/** + * @author Francis Besset + */ +class DateTypeConverter implements TypeConverterInterface +{ + public function getTypeNamespace() + { + return 'http://www.w3.org/2001/XMLSchema'; + } + + public function getTypeName() + { + return 'date'; + } + + public function convertXmlToPhp(SoapRequest $request, $data) + { + $doc = new \DOMDocument(); + $doc->loadXML($data); + + return new \DateTime($doc->textContent); + } + + public function convertPhpToXml(SoapResponse $response, $data) + { + return sprintf('<%1$s>%2$s', $this->getTypeName(), $data->format('Y-m-d')); + } +} diff --git a/Resources/config/converters.xml b/Resources/config/converters.xml index 25b7cd1..ec6e5c5 100644 --- a/Resources/config/converters.xml +++ b/Resources/config/converters.xml @@ -7,6 +7,7 @@ BeSimple\SoapBundle\Converter\ConverterRepository BeSimple\SoapBundle\Converter\DateTimeTypeConverter + BeSimple\SoapBundle\Converter\DateTypeConverter @@ -15,5 +16,9 @@ + + + + diff --git a/Resources/config/webservice.xml b/Resources/config/webservice.xml index 873148a..5b0f211 100644 --- a/Resources/config/webservice.xml +++ b/Resources/config/webservice.xml @@ -81,6 +81,10 @@ dateTime xsd:dateTime + + date + xsd:date + diff --git a/Tests/Converter/DateTypeConverterTest.php b/Tests/Converter/DateTypeConverterTest.php new file mode 100644 index 0000000..db53dd1 --- /dev/null +++ b/Tests/Converter/DateTypeConverterTest.php @@ -0,0 +1,41 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace BeSimple\SoapBundle\Tests\Converter; + +use BeSimple\SoapBundle\Soap\SoapRequest; +use BeSimple\SoapBundle\Soap\SoapResponse; +use BeSimple\SoapBundle\Converter\DateTypeConverter; + +/** + * UnitTest for \BeSimple\SoapBundle\Converter\DateTimeTypeConverter. + */ +class DateTypeConverterTest extends \PHPUnit_Framework_TestCase +{ + public function testConvertXmlToPhp() + { + $converter = new DateTypeConverter(); + + $dateXml = '2002-10-10'; + $date = $converter->convertXmlToPhp(new SoapRequest(), $dateXml); + + $this->assertEquals(new \DateTime('2002-10-10'), $date); + } + + public function testConvertPhpToXml() + { + $converter = new DateTypeConverter(); + + $date = new \DateTime('2002-10-10'); + $dateXml = $converter->convertPhpToXml(new SoapResponse(), $date); + + $this->assertEquals('2002-10-10', $dateXml); + } +}