1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace POData\ObjectModel; |
4
|
|
|
|
5
|
|
|
use POData\ObjectModel\ODataEntry; |
6
|
|
|
use POData\Providers\Metadata\ResourceEntityType; |
7
|
|
|
|
8
|
|
|
class ModelDeserialiser |
9
|
|
|
{ |
10
|
|
|
// take a supplied resourceEntityType and ODataEntry object, check that they match, and retrieve the |
11
|
|
|
// non-key properties for same |
12
|
|
|
|
13
|
|
|
private static $nonKeyPropertiesCache = []; |
14
|
|
|
|
15
|
|
|
public function __construct() |
16
|
|
|
{ |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Filter supplied ODataEntry into $data array for use in resource create/update |
21
|
|
|
* |
22
|
|
|
* @param ResourceEntityType $entityType Entity type to deserialise to |
23
|
|
|
* @param ODataEntry $payload Raw data to deserialise |
24
|
|
|
* |
25
|
|
|
* @return mixed[] |
26
|
|
|
* @throws \InvalidArgumentException |
27
|
|
|
*/ |
28
|
|
|
public function bulkDeserialise(ResourceEntityType $entityType, ODataEntry $payload) |
29
|
|
|
{ |
30
|
|
|
if (!isset($payload->type)) { |
31
|
|
|
$msg = 'ODataEntry payload type not set'; |
32
|
|
|
throw new \InvalidArgumentException($msg); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$payloadType = $payload->type->term; |
36
|
|
|
$pay = explode('.', $payloadType); |
37
|
|
|
$payloadType = $pay[count($pay)-1]; |
38
|
|
|
$actualType = $entityType->getName(); |
39
|
|
|
|
40
|
|
|
if ($payloadType !== $actualType) { |
41
|
|
|
$msg = 'Payload resource type does not match supplied resource type.'; |
42
|
|
|
throw new \InvalidArgumentException($msg); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if (!isset(self::$nonKeyPropertiesCache[$actualType])) { |
46
|
|
|
$rawProp = $entityType->getAllProperties(); |
47
|
|
|
$keyProp = $entityType->getKeyProperties(); |
48
|
|
|
$keyNames = array_keys($keyProp); |
49
|
|
|
$nonRelProp = []; |
50
|
|
|
foreach ($rawProp as $prop) { |
51
|
|
|
$propName = $prop->getName(); |
52
|
|
|
if (!in_array($propName, $keyNames) && !($prop->getResourceType() instanceof ResourceEntityType)) { |
53
|
|
|
$nonRelProp[$propName] = $prop; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
self::$nonKeyPropertiesCache[$actualType] = $nonRelProp; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$nonRelProp = self::$nonKeyPropertiesCache[$actualType]; |
60
|
|
|
|
61
|
|
|
// assemble data array |
62
|
|
|
$data = []; |
63
|
|
|
foreach ($payload->propertyContent->properties as $propName => $propSpec) { |
64
|
|
|
if (array_key_exists($propName, $nonRelProp)) { |
65
|
|
|
$rawVal = $propSpec->value; |
66
|
|
|
$data[$propName] = $rawVal; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $data; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function reset() |
74
|
|
|
{ |
75
|
|
|
self::$nonKeyPropertiesCache = []; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|