From 9b0ec8816deed7315c494fc42809d431e2642d6b Mon Sep 17 00:00:00 2001 From: Francis Besset Date: Sat, 10 Sep 2011 19:17:31 +0200 Subject: [PATCH] Added DateTypeConverter --- .../Converter/DateTypeConverter.php | 42 +++++++++++++++++++ .../Converter/DateTypeConverterTest.php | 41 ++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 src/BeSimple/SoapCommon/Converter/DateTypeConverter.php create mode 100644 tests/BeSimple/Tests/SoapCommon/Converter/DateTypeConverterTest.php diff --git a/src/BeSimple/SoapCommon/Converter/DateTypeConverter.php b/src/BeSimple/SoapCommon/Converter/DateTypeConverter.php new file mode 100644 index 0000000..4e21425 --- /dev/null +++ b/src/BeSimple/SoapCommon/Converter/DateTypeConverter.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 Francis Besset + */ +class DateTypeConverter implements TypeConverterInterface +{ + public function getTypeNamespace() + { + return 'http://www.w3.org/2001/XMLSchema'; + } + + public function getTypeName() + { + return 'date'; + } + + 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')); + } +} diff --git a/tests/BeSimple/Tests/SoapCommon/Converter/DateTypeConverterTest.php b/tests/BeSimple/Tests/SoapCommon/Converter/DateTypeConverterTest.php new file mode 100644 index 0000000..6e164cb --- /dev/null +++ b/tests/BeSimple/Tests/SoapCommon/Converter/DateTypeConverterTest.php @@ -0,0 +1,41 @@ + + * (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\DateTypeConverter; + +/** + * UnitTest for \BeSimple\SoapCommon\Converter\DateTypeConverter. + */ +class DateTypeConverterTest extends \PHPUnit_Framework_TestCase +{ + public function testConvertXmlToPhp() + { + $converter = new DateTypeConverter(); + + $dateXml = '2002-10-10'; + $date = $converter->convertXmlToPhp($dateXml); + + $this->assertEquals(new \DateTime('2002-10-10'), $date); + } + + public function testConvertPhpToXml() + { + $converter = new DateTypeConverter(); + + $date = new \DateTime('2002-10-10'); + $dateXml = $converter->convertPhpToXml($date); + + $this->assertEquals('2002-10-10', $dateXml); + } +} \ No newline at end of file