1
|
|
|
<?php |
2
|
|
|
namespace GoetasWebservices\SoapServices\SoapClient\Arguments; |
3
|
|
|
|
4
|
|
|
use Doctrine\Instantiator\Instantiator; |
5
|
|
|
use GoetasWebservices\SoapServices\SoapClient\Arguments\Headers\Handler\HeaderHandler; |
6
|
|
|
use GoetasWebservices\SoapServices\SoapClient\Arguments\Headers\Handler\HeaderPlaceholder; |
7
|
|
|
use GoetasWebservices\SoapServices\SoapClient\Arguments\Headers\Header; |
8
|
|
|
use JMS\Serializer\Serializer; |
9
|
|
|
|
10
|
|
|
class ArgumentsReader implements ArgumentsReaderInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var Serializer |
14
|
|
|
*/ |
15
|
|
|
private $serializer; |
16
|
|
|
/** |
17
|
|
|
* @var HeaderHandler |
18
|
|
|
*/ |
19
|
|
|
private $headerHandler; |
20
|
|
|
|
21
|
16 |
|
public function __construct(Serializer $serializer, HeaderHandler $headerHandler) |
22
|
|
|
{ |
23
|
16 |
|
$this->serializer = $serializer; |
24
|
16 |
|
$this->headerHandler = $headerHandler; |
25
|
16 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param array $args |
29
|
|
|
* @param array $input |
30
|
|
|
* @return null|object |
31
|
|
|
*/ |
32
|
14 |
|
public function readArguments(array $args, array $input) |
33
|
|
|
{ |
34
|
|
|
|
35
|
|
|
$envelope = array_filter($args, function ($item) use ($input) { |
36
|
14 |
|
return $item instanceof $input['message_fqcn']; |
37
|
14 |
|
}); |
38
|
14 |
|
if ($envelope) { |
|
|
|
|
39
|
|
|
return reset($envelope); |
40
|
|
|
} |
41
|
|
|
|
42
|
14 |
|
$instantiator = new Instantiator(); |
43
|
14 |
|
$envelope = $instantiator->instantiate($input['message_fqcn']); |
44
|
|
|
|
45
|
14 |
|
$args = $this->handleHeaders($args, $input, $envelope); |
46
|
|
|
|
47
|
14 |
|
if (!count($input['parts'])) { |
48
|
2 |
|
return $envelope; |
49
|
|
|
} |
50
|
|
|
|
51
|
12 |
|
$body = $instantiator->instantiate($input['part_fqcn']); |
52
|
12 |
|
$envelope->setBody($body); |
53
|
12 |
|
$factory = $this->serializer->getMetadataFactory(); |
54
|
12 |
|
$classMetadata = $factory->getMetadataForClass($input['part_fqcn']); |
55
|
|
|
|
56
|
12 |
|
if (count($input['parts']) > 1) { |
57
|
|
|
|
58
|
1 |
|
if (count($input['parts']) !== count($args)) { |
59
|
|
|
throw new \Exception("Expected to have exactly " . count($input['parts']) . " arguments, supplied " . count($args)); |
60
|
|
|
} |
61
|
|
|
|
62
|
1 |
|
foreach ($input['parts'] as $paramName => $elementName) { |
63
|
1 |
|
$propertyMetadata = $classMetadata->propertyMetadata[$paramName]; |
64
|
1 |
|
$propertyMetadata->setValue($body, array_shift($args)); |
65
|
1 |
|
} |
66
|
1 |
|
return $envelope; |
67
|
|
|
} |
68
|
|
|
|
69
|
11 |
|
$propertyName = key($input['parts']); |
70
|
11 |
|
$propertyMetadata = $classMetadata->propertyMetadata[$propertyName]; |
71
|
11 |
|
if ($args[0] instanceof $propertyMetadata->type['name']) { |
72
|
1 |
|
$propertyMetadata->setValue($body, reset($args)); |
73
|
1 |
|
return $envelope; |
74
|
|
|
} |
75
|
|
|
|
76
|
10 |
|
$instance2 = $instantiator->instantiate($propertyMetadata->type['name']); |
77
|
10 |
|
$classMetadata2 = $factory->getMetadataForClass($propertyMetadata->type['name']); |
78
|
10 |
|
$propertyMetadata->setValue($body, $instance2); |
79
|
|
|
|
80
|
10 |
|
foreach ($classMetadata2->propertyMetadata as $propertyMetadata2) { |
81
|
10 |
|
if (!count($args)) { |
82
|
|
|
throw new \Exception("Not enough arguments provided. Can't fina a parameter to set " . $propertyMetadata2->name); |
83
|
|
|
} |
84
|
10 |
|
$value = array_shift($args); |
85
|
10 |
|
$propertyMetadata2->setValue($instance2, $value); |
86
|
10 |
|
} |
87
|
10 |
|
return $envelope; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param array $args |
92
|
|
|
* @param array $input |
93
|
|
|
* @param $envelope |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
14 |
|
private function handleHeaders(array $args, array $input, $envelope) |
97
|
|
|
{ |
98
|
|
|
$headers = array_filter($args, function ($item) use ($input) { |
99
|
14 |
|
return $item instanceof $input['headers_fqcn']; |
100
|
14 |
|
}); |
101
|
14 |
|
if ($headers) { |
|
|
|
|
102
|
|
|
$envelope->setHeader(reset($headers)); |
103
|
|
|
} else { |
104
|
|
|
|
105
|
|
|
$headers = array_filter($args, function ($item) { |
106
|
14 |
|
return $item instanceof Header; |
107
|
14 |
|
}); |
108
|
14 |
|
if (count($headers)) { |
109
|
1 |
|
$headerPlaceholder = new HeaderPlaceholder(); |
110
|
1 |
|
foreach ($headers as $headerInfo) { |
111
|
1 |
|
$this->headerHandler->addHeaderData($headerPlaceholder, $headerInfo); |
112
|
1 |
|
} |
113
|
1 |
|
$envelope->setHeader($headerPlaceholder); |
114
|
1 |
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
14 |
|
$args = array_filter($args, function ($item) use ($input) { |
118
|
14 |
|
return !($item instanceof Header) && !($item instanceof $input['headers_fqcn']); |
119
|
14 |
|
}); |
120
|
14 |
|
return $args; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.