dmt-software / jms-soap-serializer
JMS serialize visitor that (de)serializes SOAP requests/responses
Installs: 70 068
Dependents: 3
Suggesters: 0
Security: 0
Stars: 6
Watchers: 3
Forks: 3
Open Issues: 0
Requires
- php: >=7.2
- ext-dom: *
- ext-intl: *
- ext-simplexml: *
- doctrine/annotations: ^2.0
- jms/serializer: ^3.19
Requires (Dev)
- ext-soap: *
- phpunit/phpunit: ^7.2 || ^8.0
README
Install
composer require dmt-software/jms-soap-serializer
Usage
Configure Serializer
use DMT\Soap\Serializer\SoapDeserializationVisitorFactory; use DMT\Soap\Serializer\SoapSerializationVisitorFactory; use DMT\Soap\Serializer\SoapMessageEventSubscriber; use JMS\Serializer\EventDispatcher\EventDispatcher; use JMS\Serializer\SerializerBuilder; $builder = SerializerBuilder::create() ->setSerializationVisitor('soap', new SoapSerializationVisitorFactory()) ->setDeserializationVisitor('soap', new SoapDeserializationVisitorFactory()) ->configureListeners( function (EventDispatcher $dispatcher) { $dispatcher->addSubscriber( new SoapMessageEventSubscriber() ); } ); $serializer = $builder->build();
Enable (de)serialization of DateTime objects
use DMT\Soap\Serializer\SoapDateHandler; use JMS\Serializer\Handler\HandlerRegistry; /** @var JMS\Serializer\SerializerBuilder $builder */ $builder->configureHandlers( function(HandlerRegistry $registry) { $registry->registerSubscribingHandler(new SoapDateHandler()); } );
Configure Serializer with SoapHeader
use DMT\Soap\Serializer\SoapHeaderInterface; use DMT\Soap\Serializer\SoapHeaderEventSubscriber; use DMT\Soap\Serializer\SoapMessageEventSubscriber; use JMS\Serializer\EventDispatcher\EventDispatcher; /** @var JMS\Serializer\SerializerBuilder $builder */ $builder->configureListeners( function (EventDispatcher $dispatcher) { $dispatcher->addSubscriber( new SoapMessageEventSubscriber() ); /** @var SoapHeaderInterface $soapHeader */ $dispatcher->addSubscriber( new SoapHeaderEventSubscriber($soapHeader) ); } );
Using SOAP 1.2
use DMT\Soap\Serializer\SoapNamespaceInterface; use DMT\Soap\Serializer\SoapSerializationVisitorFactory; /** @var JMS\Serializer\SerializerBuilder $builder */ $builder->setSerializationVisitor( 'soap', (new SoapSerializationVisitorFactory()) ->setSoapVersion(SoapNamespaceInterface::SOAP_1_2) );
Using Serializer
Serialize SOAP Request
use JMS\Serializer\Serializer; /** @var Message $requestMessage */ /** @var Serializer $serializer */ $request = $serializer->serialize($requestMessage, 'soap'); // $request = '<soap:Envelope ...><soap:Body><ns1:Message>...</ns1:Message></soap:Body></soap:Envelope>';
Deserialize SOAP Response
use JMS\Serializer\Serializer; /** @var Serializer $serializer */ $response = $serializer->deserialize('<env:Envelope ... </env:Envelope>', ResponseMessage::class, 'soap'); // $response instanceof ResponseMessage
Debugging
Failing to make a request
When creating a SOAP message you must provide a XmlRoot and XmlRootNamespace. If you forgot to provide them an exception is thrown:
Missing XmlRootName or XmlRootNamespace for {{ YourSOAPRequest }}.
To fix this, add the XmlRoot annotation to your configuration:
namespace Any\NS; use JMS\Serializer\Annotation as JMS; /** * @JMS\XmlRoot("YourSOAPRequest", namespace="http://ns-for-your-request") */ class YourSOAPRequest { //... }
or if you're using yaml configuration:
Any\NS\YourSOAPRequest: ... xml_root_name: YourSOAPRequest xml_root_namespace: http://ns-for-your-request ...