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