|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GoetasWebservices\SoapServices\SoapClient\Result; |
|
4
|
|
|
|
|
5
|
|
|
use GoetasWebservices\SoapServices\SoapClient\SerializerUtils; |
|
6
|
|
|
use JMS\Serializer\Serializer; |
|
7
|
|
|
use Metadata\MetadataFactoryInterface; |
|
8
|
|
|
use Metadata\PropertyMetadata; |
|
9
|
|
|
|
|
10
|
|
|
class ResultCreator implements ResultCreatorInterface |
|
11
|
|
|
{ |
|
12
|
|
|
private $unwrap = false; |
|
13
|
|
|
/** |
|
14
|
|
|
* @var Serializer |
|
15
|
|
|
*/ |
|
16
|
|
|
private $serializer; |
|
17
|
|
|
|
|
18
|
36 |
|
public function __construct(Serializer $serializer, $unwrap = false) |
|
19
|
|
|
{ |
|
20
|
36 |
|
$this->serializer = $serializer; |
|
21
|
36 |
|
$this->unwrap = $unwrap; |
|
22
|
36 |
|
} |
|
23
|
|
|
|
|
24
|
17 |
|
private function getPropertyValue($propertyMetadata, $object) { |
|
25
|
17 |
|
$reflectionProperty = new \ReflectionProperty($propertyMetadata->class, $propertyMetadata->name); |
|
26
|
17 |
|
$reflectionProperty->setAccessible(true); |
|
27
|
17 |
|
return $reflectionProperty->getValue($object); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
17 |
|
public function prepareResult($object, array $output) |
|
31
|
|
|
{ |
|
32
|
17 |
|
if (!count($output['parts'])) { |
|
33
|
|
|
return null; |
|
34
|
|
|
} |
|
35
|
17 |
|
$factory = SerializerUtils::getMetadataFactory($this->serializer); |
|
36
|
|
|
|
|
37
|
17 |
|
$classMetadata = $factory->getMetadataForClass($output['message_fqcn']); |
|
38
|
17 |
|
$bodyMetadata = $classMetadata->propertyMetadata['body']; |
|
39
|
17 |
|
$bodyClassMetadata = $factory->getMetadataForClass($bodyMetadata->type['name']); |
|
40
|
17 |
|
$body = $this->getPropertyValue($bodyMetadata, $object); |
|
41
|
17 |
|
$parts = []; |
|
42
|
17 |
|
foreach ($bodyClassMetadata->propertyMetadata as $propertyMetadata) { |
|
43
|
17 |
|
$parts[$propertyMetadata->name] = $this->getPropertyValue($propertyMetadata, $body); |
|
44
|
17 |
|
} |
|
45
|
17 |
|
if (count($output['parts']) > 1) { |
|
46
|
2 |
|
return $parts; |
|
47
|
|
|
} else { |
|
48
|
15 |
|
if ($this->unwrap) { |
|
49
|
2 |
|
foreach ($bodyClassMetadata->propertyMetadata as $propertyMetadata) { |
|
50
|
2 |
|
$propClassMetadata = $factory->getMetadataForClass($propertyMetadata->type['name']); |
|
51
|
|
|
|
|
52
|
2 |
|
if (count($propClassMetadata->propertyMetadata) > 1) { |
|
53
|
|
|
throw new \Exception("When using wrapped mode, the wrapped object can not have multiple properties"); |
|
54
|
|
|
} |
|
55
|
2 |
|
if (!count($propClassMetadata->propertyMetadata)) { |
|
56
|
|
|
return null; |
|
57
|
|
|
} |
|
58
|
2 |
|
$propertyMetadata = reset($propClassMetadata->propertyMetadata); |
|
59
|
2 |
|
return $this->getPropertyValue($propertyMetadata, reset($parts)); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
13 |
|
return reset($parts); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|