replace internal type converter interface with cleaner solution

This commit is contained in:
Andreas Schamberger 2012-01-08 09:50:11 +01:00
parent bf8965895e
commit d633516e1f
5 changed files with 72 additions and 109 deletions

View File

@ -1,58 +0,0 @@
<?php
/*
* This file is part of the BeSimpleSoapCommon.
*
* (c) Christian Kerl <christian-kerl@web.de>
* (c) Francis Besset <francis.besset@gmail.com>
*
* 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 <mail@andreass.net>
* @author Christian Kerl <christian-kerl@web.de>
*/
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);
}

View File

@ -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 <mail@andreass.net>
*/
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;
}
}

View File

@ -0,0 +1,32 @@
<?php
/*
* This file is part of the BeSimpleSoapCommon.
*
* (c) Christian Kerl <christian-kerl@web.de>
* (c) Francis Besset <francis.besset@gmail.com>
*
* 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 <mail@andreass.net>
*/
interface SoapKernelAwareInterface
{
/**
* Set SoapKernel instance.
*
* @param \BeSimple\SoapCommon\SoapKernel $soapKernel SoapKernel instance
*
* @return void
*/
function setKernel(SoapKernel $soapKernel);
}

View File

@ -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 <mail@andreass.net>
*/
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;
}
}

View File

@ -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);
},
);
}
}
}