dmt-software /
jms-soap-serializer
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace DMT\Soap\Serializer; |
||||
| 4 | |||||
| 5 | use DOMDocument; |
||||
| 6 | use JMS\Serializer\EventDispatcher\EventSubscriberInterface; |
||||
| 7 | use JMS\Serializer\EventDispatcher\ObjectEvent; |
||||
| 8 | use JMS\Serializer\Metadata\ClassMetadata; |
||||
| 9 | use JMS\Serializer\SerializationContext; |
||||
| 10 | use JMS\Serializer\XmlSerializationVisitor; |
||||
| 11 | |||||
| 12 | /** |
||||
| 13 | * Class SoapHeaderEventSubscriber |
||||
| 14 | * |
||||
| 15 | * @package DMT\Soap |
||||
| 16 | */ |
||||
| 17 | class SoapHeaderEventSubscriber implements EventSubscriberInterface |
||||
| 18 | { |
||||
| 19 | /** |
||||
| 20 | * @var SoapHeaderInterface |
||||
| 21 | */ |
||||
| 22 | protected $header; |
||||
| 23 | |||||
| 24 | /** |
||||
| 25 | * {@inheritdoc} |
||||
| 26 | */ |
||||
| 27 | 2 | public static function getSubscribedEvents(): array |
|||
| 28 | { |
||||
| 29 | return [ |
||||
| 30 | [ |
||||
| 31 | 2 | 'event' => 'serializer.post_serialize', |
|||
| 32 | 'method' => 'addSoapHeader', |
||||
| 33 | 'format' => 'soap', |
||||
| 34 | ], |
||||
| 35 | ]; |
||||
| 36 | } |
||||
| 37 | |||||
| 38 | /** |
||||
| 39 | * EventSubscriber constructor. |
||||
| 40 | * |
||||
| 41 | * @param SoapHeaderInterface $header |
||||
| 42 | */ |
||||
| 43 | 2 | public function __construct(SoapHeaderInterface $header) |
|||
| 44 | { |
||||
| 45 | 2 | $this->header = $header; |
|||
| 46 | 2 | } |
|||
| 47 | |||||
| 48 | /** |
||||
| 49 | * @param ObjectEvent $event |
||||
| 50 | */ |
||||
| 51 | 2 | public function addSoapHeader(ObjectEvent $event) |
|||
| 52 | { |
||||
| 53 | /** @var SerializationContext $context */ |
||||
| 54 | 2 | $context = $event->getContext(); |
|||
| 55 | /** @var XmlSerializationVisitor $visitor */ |
||||
| 56 | 2 | $visitor = $event->getVisitor(); |
|||
| 57 | /** @var DOMDocument $document */ |
||||
| 58 | 2 | $document = $visitor->getDocument(); |
|||
| 59 | |||||
| 60 | 2 | if (!$this->hasSoapHeader($document) && $context->getDepth() === 0) { |
|||
| 61 | /** @var ClassMetadata $metadata */ |
||||
| 62 | 2 | $metadata = $context->getMetadataFactory()->getMetadataForClass(get_class($this->header)); |
|||
| 63 | |||||
| 64 | 2 | $visitor->setCurrentNode($document->firstChild); |
|||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 65 | 2 | $visitor->setCurrentNode( |
|||
| 66 | 2 | $header = $document->firstChild->insertBefore( |
|||
|
0 ignored issues
–
show
The method
insertBefore() does not exist on null.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||
| 67 | 2 | $document->createElementNS($document->lookupNamespaceUri('soap'), 'Header'), |
|||
| 68 | 2 | $document->firstChild->firstChild |
|||
| 69 | ) |
||||
| 70 | ); |
||||
| 71 | |||||
| 72 | 2 | $tagName = $metadata->xmlRootName; |
|||
| 73 | 2 | if ($metadata->xmlRootPrefix !== null) { |
|||
| 74 | 1 | $tagName = $metadata->xmlRootPrefix . ':' . $metadata->xmlRootName; |
|||
| 75 | } |
||||
| 76 | |||||
| 77 | 2 | $visitor->setCurrentNode( |
|||
| 78 | 2 | $header->appendChild( |
|||
| 79 | 2 | $document->createElementNS($metadata->xmlRootNamespace, $tagName) |
|||
| 80 | ) |
||||
| 81 | ); |
||||
| 82 | |||||
| 83 | 2 | $context->getNavigator()->accept($this->header, null); |
|||
| 84 | } |
||||
| 85 | 2 | } |
|||
| 86 | |||||
| 87 | /** |
||||
| 88 | * Check if the header is already added. |
||||
| 89 | * |
||||
| 90 | * @param DOMDocument $document |
||||
| 91 | * |
||||
| 92 | * @return bool |
||||
| 93 | */ |
||||
| 94 | 2 | protected function hasSoapHeader(DOMDocument $document): bool |
|||
| 95 | { |
||||
| 96 | 2 | return $document->getElementsByTagNameNS($document->lookupNamespaceUri('soap'), 'Header')->length > 0; |
|||
| 97 | } |
||||
| 98 | } |
||||
| 99 |