1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GoetasWebservices\SoapServices\SoapClient\Arguments\Headers\Handler; |
4
|
|
|
|
5
|
|
|
use GoetasWebservices\SoapServices\SoapClient\Arguments\Headers\Header; |
6
|
|
|
use JMS\Serializer\DeserializationContext; |
7
|
|
|
use JMS\Serializer\GraphNavigator; |
8
|
|
|
use JMS\Serializer\Handler\SubscribingHandlerInterface; |
9
|
|
|
use JMS\Serializer\Metadata\StaticPropertyMetadata; |
10
|
|
|
use JMS\Serializer\SerializationContext; |
11
|
|
|
use JMS\Serializer\XmlDeserializationVisitor; |
12
|
|
|
use JMS\Serializer\XmlSerializationVisitor; |
13
|
|
|
use Symfony\Component\DependencyInjection\SimpleXMLElement; |
14
|
|
|
|
15
|
|
|
class HeaderHandler implements SubscribingHandlerInterface |
16
|
|
|
{ |
17
|
|
|
const SOAP = 'http://schemas.xmlsoap.org/soap/envelope/'; |
18
|
|
|
const SOAP_12 = 'http://www.w3.org/2003/05/soap-envelope'; |
19
|
|
|
|
20
|
|
|
protected $headerData = []; |
21
|
|
|
|
22
|
36 |
|
public static function getSubscribingMethods() |
23
|
|
|
{ |
24
|
|
|
return array( |
25
|
|
|
array( |
26
|
36 |
|
'direction' => GraphNavigator::DIRECTION_SERIALIZATION, |
27
|
36 |
|
'format' => 'xml', |
28
|
36 |
|
'type' => HeaderPlaceholder::class, |
29
|
|
|
'method' => 'serializeHeader' |
30
|
36 |
|
), |
31
|
|
|
array( |
32
|
36 |
|
'direction' => GraphNavigator::DIRECTION_DESERIALIZATION, |
33
|
36 |
|
'format' => 'xml', |
34
|
36 |
|
'type' => 'GoetasWebservices\SoapServices\SoapEnvelope\Headers', |
35
|
|
|
'method' => 'deserializeHeaders' |
36
|
36 |
|
), |
37
|
36 |
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function deserializeHeaders(XmlDeserializationVisitor $visitor, \SimpleXMLElement $data, array $type, DeserializationContext $context) |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
$newType = [ |
43
|
|
|
'name' => $type['params'][0], |
44
|
|
|
'params' => [] |
45
|
|
|
]; |
46
|
|
|
return $context->getNavigator()->accept($data, $newType, $context); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
2 |
|
public function addHeaderData(HeaderPlaceholder $reference, $data) |
50
|
|
|
{ |
51
|
2 |
|
$this->headerData[spl_object_hash($reference)][] = $data; |
52
|
2 |
|
} |
53
|
|
|
|
54
|
2 |
|
public function serializeHeader(XmlSerializationVisitor $visitor, HeaderPlaceholder $data, array $type, SerializationContext $context) |
|
|
|
|
55
|
|
|
{ |
56
|
2 |
|
if (!isset($this->headerData[spl_object_hash($data)])) { |
57
|
|
|
return; |
58
|
|
|
} |
59
|
2 |
|
$factory = $context->getMetadataFactory(); |
60
|
|
|
/** |
61
|
|
|
* @var $header Header |
62
|
|
|
*/ |
63
|
2 |
|
foreach ($this->headerData[spl_object_hash($data)] as $header) { |
64
|
|
|
/** |
65
|
|
|
* @var $classMetadata \JMS\Serializer\Metadata\ClassMetadata |
66
|
|
|
*/ |
67
|
2 |
|
$classMetadata = $factory->getMetadataForClass(get_class($header->getData())); |
68
|
|
|
|
69
|
2 |
|
$name = ($pos = strpos($classMetadata->xmlRootName, ':')) !== false ? substr($classMetadata->xmlRootName, $pos + 1) : $classMetadata->xmlRootName; |
70
|
|
|
|
71
|
2 |
|
$metadata = new StaticPropertyMetadata($classMetadata->name, $name, $header->getData()); |
72
|
2 |
|
$metadata->xmlNamespace = $classMetadata->xmlRootNamespace; |
73
|
2 |
|
$metadata->serializedName = $name; |
74
|
|
|
|
75
|
2 |
|
$visitor->visitProperty($metadata, $header->getData(), $context); |
|
|
|
|
76
|
|
|
|
77
|
2 |
|
$this->handleOptions($visitor, $header); |
78
|
2 |
|
} |
79
|
2 |
|
} |
80
|
|
|
|
81
|
2 |
|
private function handleOptions(XmlSerializationVisitor $visitor, $header) |
82
|
|
|
{ |
83
|
2 |
|
$options = $header->getOptions(); |
84
|
2 |
|
if (!count($options)) { |
85
|
2 |
|
return; |
86
|
|
|
} |
87
|
|
|
/** |
88
|
|
|
* @var $currentNode \DOMNode |
89
|
|
|
*/ |
90
|
2 |
|
$currentNode = $visitor->getCurrentNode(); |
91
|
2 |
|
foreach ($options as $option => $value) { |
92
|
2 |
|
if (in_array($option, ['mustUnderstand', 'required', 'role', 'actor'])) { |
93
|
|
|
|
94
|
2 |
|
if ($currentNode->ownerDocument->documentElement->namespaceURI === self::SOAP_12) { |
95
|
1 |
|
$envelopeNS = self::SOAP_12; |
96
|
1 |
|
} else { |
97
|
1 |
|
$envelopeNS = self::SOAP; |
98
|
|
|
} |
99
|
2 |
|
$this->setAttributeOnNode($currentNode->lastChild, $option, $value, $envelopeNS); |
|
|
|
|
100
|
2 |
|
} |
101
|
2 |
|
} |
102
|
2 |
|
} |
103
|
|
|
|
104
|
2 |
|
private function setAttributeOnNode(\DOMElement $node, $name, $value, $namespace) |
105
|
|
|
{ |
106
|
2 |
|
if (!($prefix = $node->lookupPrefix($namespace)) && !($prefix = $node->ownerDocument->lookupPrefix($namespace))) { |
107
|
|
|
$prefix = 'ns-' . substr(sha1($namespace), 0, 8); |
108
|
|
|
} |
109
|
2 |
|
$node->setAttributeNS($namespace, $prefix . ':' . $name, is_bool($value) || is_null($value) ? ($value ? 'true' : 'false') : $value); |
110
|
2 |
|
} |
111
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
|
116
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.