From 5c3357b879067011d9a2fd356a7cba4e98878c96 Mon Sep 17 00:00:00 2001 From: Francis Besset Date: Sat, 10 Sep 2011 19:16:27 +0200 Subject: [PATCH] Added DateTimeTypeConverter --- .../Converter/DateTimeTypeConverter.php | 42 ++++++++++++++++++ .../Converter/DateTimeTypeConverterTest.php | 43 +++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 src/BeSimple/SoapCommon/Converter/DateTimeTypeConverter.php create mode 100644 tests/BeSimple/Tests/SoapCommon/Converter/DateTimeTypeConverterTest.php diff --git a/src/BeSimple/SoapCommon/Converter/DateTimeTypeConverter.php b/src/BeSimple/SoapCommon/Converter/DateTimeTypeConverter.php new file mode 100644 index 0000000..6446e35 --- /dev/null +++ b/src/BeSimple/SoapCommon/Converter/DateTimeTypeConverter.php @@ -0,0 +1,42 @@ + + * (c) Francis Besset + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace BeSimple\SoapCommon\Converter; + +/** + * @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($data) + { + $doc = new \DOMDocument(); + $doc->loadXML($data); + + return new \DateTime($doc->textContent); + } + + public function convertPhpToXml($data) + { + return sprintf('<%1$s>%2$s', $this->getTypeName(), $data->format('Y-m-d\TH:i:sP')); + } +} \ No newline at end of file diff --git a/tests/BeSimple/Tests/SoapCommon/Converter/DateTimeTypeConverterTest.php b/tests/BeSimple/Tests/SoapCommon/Converter/DateTimeTypeConverterTest.php new file mode 100644 index 0000000..d7913f5 --- /dev/null +++ b/tests/BeSimple/Tests/SoapCommon/Converter/DateTimeTypeConverterTest.php @@ -0,0 +1,43 @@ + + * (c) Francis Besset + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace BeSimple\SoapCommon\Tests\Converter; + +use BeSimple\SoapCommon\Converter\DateTimeTypeConverter; + +/** + * UnitTest for \BeSimple\SoapCommon\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($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($date); + + $this->assertEquals('2002-10-10T12:00:00-05:00', $dateXml); + } +} \ No newline at end of file