From 8bc2d1aa204eac2f0af4b90dd2eb0d3894c95879 Mon Sep 17 00:00:00 2001 From: Christian Kerl Date: Wed, 24 Aug 2011 18:00:31 +0200 Subject: [PATCH] added TypeConverter for PHP's DateTime to XML's dateTime --- Converter/DateTimeTypeConverter.php | 41 ++++++++++++++++++ Tests/Converter/DateTimeTypeConverterTest.php | 43 +++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 Converter/DateTimeTypeConverter.php create mode 100644 Tests/Converter/DateTimeTypeConverterTest.php diff --git a/Converter/DateTimeTypeConverter.php b/Converter/DateTimeTypeConverter.php new file mode 100644 index 0000000..da84758 --- /dev/null +++ b/Converter/DateTimeTypeConverter.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\Converter; + +use BeSimple\SoapBundle\Soap\SoapRequest; +use BeSimple\SoapBundle\Soap\SoapResponse; +use BeSimple\SoapBundle\Util\String; + +/** + * @author Christian Kerl + */ +class DateTimeTypeConverter implements TypeConverterInterface +{ + public function getTypeNamespace() + { + return 'http://www.w3.org/2001/XMLSchema'; + } + + public function getTypeName() + { + return 'dateTime'; + } + + public function convertXmlToPhp(SoapRequest $request, $data) + { + return new \DateTime(strip_tags($data)); + } + + public function convertPhpToXml(SoapResponse $response, $data) + { + return sprintf('%s', $data->format('Y-m-d\TH:i:sP')); + } +} diff --git a/Tests/Converter/DateTimeTypeConverterTest.php b/Tests/Converter/DateTimeTypeConverterTest.php new file mode 100644 index 0000000..197ca3d --- /dev/null +++ b/Tests/Converter/DateTimeTypeConverterTest.php @@ -0,0 +1,43 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace BeSimple\SoapBundle\Tests\ServiceBinding; + +use BeSimple\SoapBundle\Soap\SoapRequest; +use BeSimple\SoapBundle\Soap\SoapResponse; +use BeSimple\SoapBundle\Converter\DateTimeTypeConverter; + +/** + * UnitTest for \BeSimple\SoapBundle\Converter\DateTimeTypeConverter. + * + * @author Christian Kerl + */ +class DateTimeTypeConverterTest extends \PHPUnit_Framework_TestCase +{ + public function testConvertXmlToPhp() + { + $converter = new DateTimeTypeConverter(); + $dateXml = '2002-10-10T12:00:00-05:00'; + + $date = $converter->convertXmlToPhp(new SoapRequest(), $dateXml); + + $this->assertEquals(new \DateTime('2002-10-10T12:00:00-05:00'), $date); + } + + public function testConvertPhpToXml() + { + $converter = new DateTimeTypeConverter(); + $date = new \DateTime('2002-10-10T12:00:00-05:00'); + + $dateXml = $converter->convertPhpToXml(new SoapResponse(), $date); + + $this->assertEquals('2002-10-10T12:00:00-05:00', $dateXml); + } +}