From d633516e1f19d79bae62ebdd356853a65a2c1f3c Mon Sep 17 00:00:00 2001 From: Andreas Schamberger Date: Sun, 8 Jan 2012 09:50:11 +0100 Subject: [PATCH] replace internal type converter interface with cleaner solution --- .../InternalTypeConverterInterface.php | 58 ------------------- .../Converter/MtomTypeConverter.php | 28 ++++++--- .../Converter/SoapKernelAwareInterface.php | 32 ++++++++++ .../SoapCommon/Converter/SwaTypeConverter.php | 26 +++++++-- src/BeSimple/SoapCommon/SoapKernel.php | 37 ------------ 5 files changed, 72 insertions(+), 109 deletions(-) delete mode 100644 src/BeSimple/SoapCommon/Converter/InternalTypeConverterInterface.php create mode 100644 src/BeSimple/SoapCommon/Converter/SoapKernelAwareInterface.php diff --git a/src/BeSimple/SoapCommon/Converter/InternalTypeConverterInterface.php b/src/BeSimple/SoapCommon/Converter/InternalTypeConverterInterface.php deleted file mode 100644 index 9c86b66..0000000 --- a/src/BeSimple/SoapCommon/Converter/InternalTypeConverterInterface.php +++ /dev/null @@ -1,58 +0,0 @@ - - * (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; - -use BeSimple\SoapCommon\SoapKernel; - -/** - * Internal type converter interface. - * - * @author Andreas Schamberger - * @author Christian Kerl - */ -interface InternalTypeConverterInterface -{ - /** - * Get type namespace. - * - * @return string - */ - function getTypeNamespace(); - - /** - * Get type name. - * - * @return string - */ - function getTypeName(); - - /** - * Convert given XML string to PHP type. - * - * @param string $data XML string - * @param \BeSimple\SoapCommon\SoapKernel $soapKernel SoapKernel instance - * - * @return mixed - */ - function convertXmlToPhp($data, SoapKernel $soapKernel); - - /** - * Convert PHP type to XML string. - * - * @param mixed $data PHP type - * @param \BeSimple\SoapCommon\SoapKernel $soapKernel SoapKernel instance - * - * @return string - */ - function convertPhpToXml($data, SoapKernel $soapKernel); -} \ No newline at end of file diff --git a/src/BeSimple/SoapCommon/Converter/MtomTypeConverter.php b/src/BeSimple/SoapCommon/Converter/MtomTypeConverter.php index a9709e2..52ea961 100644 --- a/src/BeSimple/SoapCommon/Converter/MtomTypeConverter.php +++ b/src/BeSimple/SoapCommon/Converter/MtomTypeConverter.php @@ -15,17 +15,21 @@ namespace BeSimple\SoapCommon\Converter; use BeSimple\SoapCommon\Helper; use BeSimple\SoapCommon\Mime\Part as MimePart; use BeSimple\SoapCommon\SoapKernel; -use BeSimple\SoapCommon\SoapRequest as CommonSoapRequest; -use BeSimple\SoapCommon\SoapResponse as CommonSoapResponse; -use BeSimple\SoapCommon\Converter\InternalTypeConverterInterface; +use BeSimple\SoapCommon\Converter\SoapKernelAwareInterface; +use BeSimple\SoapCommon\Converter\TypeConverterInterface; /** * MTOM type converter. * * @author Andreas Schamberger */ -class MtomTypeConverter implements InternalTypeConverterInterface +class MtomTypeConverter implements TypeConverterInterface, SoapKernelAwareInterface { + /** + * @var \BeSimple\SoapCommon\SoapKernel $soapKernel SoapKernel instance + */ + protected $soapKernel = null; + /** * {@inheritDoc} */ @@ -45,7 +49,7 @@ class MtomTypeConverter implements InternalTypeConverterInterface /** * {@inheritDoc} */ - public function convertXmlToPhp($data, SoapKernel $soapKernel) + public function convertXmlToPhp($data) { $doc = new \DOMDocument(); $doc->loadXML($data); @@ -60,7 +64,7 @@ class MtomTypeConverter implements InternalTypeConverterInterface if ('cid:' === substr($ref, 0, 4)) { $contentId = urldecode(substr($ref, 4)); - if (null !== ($part = $soapKernel->getAttachment($contentId))) { + if (null !== ($part = $this->soapKernel->getAttachment($contentId))) { return $part->getContent(); } else { @@ -75,12 +79,12 @@ class MtomTypeConverter implements InternalTypeConverterInterface /** * {@inheritDoc} */ - public function convertPhpToXml($data, SoapKernel $soapKernel) + public function convertPhpToXml($data) { $part = new MimePart($data); $contentId = trim($part->getHeader('Content-ID'), '<>'); - $soapKernel->addAttachment($part); + $this->soapKernel->addAttachment($part); $doc = new \DOMDocument(); $node = $doc->createElement($this->getTypeName()); @@ -93,4 +97,12 @@ class MtomTypeConverter implements InternalTypeConverterInterface return $doc->saveXML(); } + + /** + * {@inheritDoc} + */ + public function setKernel(SoapKernel $soapKernel) + { + $this->soapKernel = $soapKernel; + } } diff --git a/src/BeSimple/SoapCommon/Converter/SoapKernelAwareInterface.php b/src/BeSimple/SoapCommon/Converter/SoapKernelAwareInterface.php new file mode 100644 index 0000000..655a258 --- /dev/null +++ b/src/BeSimple/SoapCommon/Converter/SoapKernelAwareInterface.php @@ -0,0 +1,32 @@ + + * (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; + +use BeSimple\SoapCommon\SoapKernel; + +/** + * Internal type converter interface. + * + * @author Andreas Schamberger + */ +interface SoapKernelAwareInterface +{ + /** + * Set SoapKernel instance. + * + * @param \BeSimple\SoapCommon\SoapKernel $soapKernel SoapKernel instance + * + * @return void + */ + function setKernel(SoapKernel $soapKernel); +} \ No newline at end of file diff --git a/src/BeSimple/SoapCommon/Converter/SwaTypeConverter.php b/src/BeSimple/SoapCommon/Converter/SwaTypeConverter.php index 32ba467..40d6372 100644 --- a/src/BeSimple/SoapCommon/Converter/SwaTypeConverter.php +++ b/src/BeSimple/SoapCommon/Converter/SwaTypeConverter.php @@ -15,15 +15,21 @@ namespace BeSimple\SoapCommon\Converter; use BeSimple\SoapCommon\Helper; use BeSimple\SoapCommon\Mime\Part as MimePart; use BeSimple\SoapCommon\SoapKernel; -use BeSimple\SoapCommon\Converter\InternalTypeConverterInterface; +use BeSimple\SoapCommon\Converter\SoapKernelAwareInterface; +use BeSimple\SoapCommon\Converter\TypeConverterInterface; /** * SwA type converter. * * @author Andreas Schamberger */ -class SwaTypeConverter implements InternalTypeConverterInterface +class SwaTypeConverter implements TypeConverterInterface, SoapKernelAwareInterface { + /** + * @var \BeSimple\SoapCommon\SoapKernel $soapKernel SoapKernel instance + */ + protected $soapKernel = null; + /** * {@inheritDoc} */ @@ -43,7 +49,7 @@ class SwaTypeConverter implements InternalTypeConverterInterface /** * {@inheritDoc} */ - public function convertXmlToPhp($data, SoapKernel $soapKernel) + public function convertXmlToPhp($data) { $doc = new \DOMDocument(); $doc->loadXML($data); @@ -55,7 +61,7 @@ class SwaTypeConverter implements InternalTypeConverterInterface if ('cid:' === substr($ref, 0, 4)) { $contentId = urldecode(substr($ref, 4)); - if (null !== ($part = $soapKernel->getAttachment($contentId))) { + if (null !== ($part = $this->soapKernel->getAttachment($contentId))) { return $part->getContent(); } else { @@ -70,13 +76,21 @@ class SwaTypeConverter implements InternalTypeConverterInterface /** * {@inheritDoc} */ - public function convertPhpToXml($data, SoapKernel $soapKernel) + public function convertPhpToXml($data) { $part = new MimePart($data); $contentId = trim($part->getHeader('Content-ID'), '<>'); - $soapKernel->addAttachment($part); + $this->soapKernel->addAttachment($part); return sprintf('<%s href="%s"/>', $this->getTypeName(), $contentId); } + + /** + * {@inheritDoc} + */ + public function setKernel(SoapKernel $soapKernel) + { + $this->soapKernel = $soapKernel; + } } diff --git a/src/BeSimple/SoapCommon/SoapKernel.php b/src/BeSimple/SoapCommon/SoapKernel.php index f0ead8c..da57db0 100644 --- a/src/BeSimple/SoapCommon/SoapKernel.php +++ b/src/BeSimple/SoapCommon/SoapKernel.php @@ -134,41 +134,4 @@ class SoapKernel $this->attachments = $response->getAttachments(); } - - /** - * Configure filter and type converter for SwA/MTOM. - * - * @param array &$options SOAP constructor options array. - * - * @return void - */ - public function configureMime(array &$options) - { - if (isset($options['attachment_type']) && Helper::ATTACHMENTS_TYPE_BASE64 !== $options['attachment_type']) { - // register mime filter in SoapKernel - $mimeFilter = new MimeFilter($options['attachment_type']); - $this->registerFilter($mimeFilter); - // configure type converter - if (Helper::ATTACHMENTS_TYPE_SWA === $options['attachment_type']) { - $converter = new SwaTypeConverter(); - } elseif (Helper::ATTACHMENTS_TYPE_MTOM === $options['attachment_type']) { - $converter = new MtomTypeConverter(); - } - // configure typemap - if (!isset($options['typemap'])) { - $options['typemap'] = array(); - } - $soapKernel = $this; - $options['typemap'][] = array( - 'type_name' => $converter->getTypeName(), - 'type_ns' => $converter->getTypeNamespace(), - 'from_xml' => function($input) use ($converter, $soapKernel) { - return $converter->convertXmlToPhp($input, $soapKernel); - }, - 'to_xml' => function($input) use ($converter, $soapKernel) { - return $converter->convertPhpToXml($input, $soapKernel); - }, - ); - } - } } \ No newline at end of file