-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy pathDocumentLiteralWrappedResponseMessageBinder.php
More file actions
110 lines (87 loc) · 3.5 KB
/
DocumentLiteralWrappedResponseMessageBinder.php
File metadata and controls
110 lines (87 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/*
* This file is part of the BeSimpleSoapBundle.
*
* (c) Christian Kerl <christian-kerl@web.de>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace BeSimple\SoapBundle\ServiceBinding;
use BeSimple\SoapBundle\ServiceDefinition\Method;
use BeSimple\SoapCommon\Definition\Type\ArrayOfType;
use BeSimple\SoapCommon\Definition\Type\ComplexType;
use BeSimple\SoapCommon\Definition\Type\TypeRepository;
use BeSimple\SoapCommon\Util\MessageBinder;
/**
* @author Christian Kerl <christian-kerl@web.de>
*/
class DocumentLiteralWrappedResponseMessageBinder implements MessageBinderInterface
{
public function processMessage(Method $messageDefinition, $message, TypeRepository $typeRepository)
{
$this->typeRepository = $typeRepository;
$result = new \stdClass();
//$result->{$messageDefinition->getName().'Result'} = $message;
foreach ($messageDefinition->getOutput()->all() as $name => $part) {
//$result->{$name} = $message;
$result->{$name} = $this->processType($part->getType(), $message);
break; // only one iteration
}
return $result;
}
private function processType($phpType, $message)
{
$isArray = false;
$type = $this->typeRepository->getType($phpType);
if ($type instanceof ArrayOfType) {
$isArray = true;
$type = $this->typeRepository->getType($type->get('item')->getType());
}
if ($type instanceof ComplexType) {
$phpType = $type->getPhpType();
if ($isArray) {
$array = array();
// See https://github.com/BeSimple/BeSimpleSoapBundle/issues/29
if (is_array($message) && in_array('BeSimple\SoapCommon\Type\AbstractKeyValue', class_parents($phpType))) {
$keyValue = array();
foreach ($message as $key => $value) {
$keyValue[] = new $phpType($key, $value);
}
$message = $keyValue;
}
foreach ($message as $complexType) {
$array[] = $this->checkComplexType($phpType, $complexType);
}
$message = $array;
} else {
$message = $this->checkComplexType($phpType, $message);
}
}
return $message;
}
private function checkComplexType($phpType, $message)
{
$hash = spl_object_hash($message);
if (isset($this->messageRefs[$hash])) {
return $this->messageRefs[$hash];
}
$this->messageRefs[$hash] = $message;
if (!$message instanceof $phpType) {
throw new \InvalidArgumentException(sprintf('The instance class must be "%s", "%s" given.', $phpType, get_class($message)));
}
$messageBinder = new MessageBinder($message);
foreach ($this->typeRepository->getType($phpType)->all() as $type) {
$property = $type->getName();
$value = $messageBinder->readProperty($property);
if (null !== $value) {
$value = $this->processType($type->getType(), $value);
$messageBinder->writeProperty($property, $value);
}
if (!$type->isNillable() && null === $value) {
throw new \InvalidArgumentException(sprintf('"%s::%s" cannot be null.', $phpType, $type->getName()));
}
}
return $message;
}
}