1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: alex |
5
|
|
|
* Date: 16/02/20 |
6
|
|
|
* Time: 1:19 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace AlgoWeb\PODataLaravel\Serialisers; |
10
|
|
|
|
11
|
|
|
use Illuminate\Database\Eloquent\Model; |
12
|
|
|
use Illuminate\Support\Collection; |
13
|
|
|
use POData\Common\InvalidOperationException; |
14
|
|
|
use POData\Common\Messages; |
15
|
|
|
use POData\Common\ODataException; |
16
|
|
|
use POData\Providers\Metadata\IMetadataProvider; |
17
|
|
|
use POData\Providers\Metadata\ResourceEntityType; |
18
|
|
|
use POData\Providers\Metadata\ResourceType; |
19
|
|
|
use POData\Providers\Metadata\Type\IType; |
20
|
|
|
use POData\Providers\Query\QueryResult; |
21
|
|
|
|
22
|
|
|
trait SerialiseUtilitiesTrait |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @param int $resourceKind |
26
|
|
|
* @return bool |
27
|
|
|
*/ |
28
|
|
|
public static function isMatchPrimitive($resourceKind) |
29
|
|
|
{ |
30
|
|
|
if (16 > $resourceKind) { |
31
|
|
|
return false; |
32
|
|
|
} |
33
|
|
|
if (28 < $resourceKind) { |
34
|
|
|
return false; |
35
|
|
|
} |
36
|
|
|
return 0 == ($resourceKind % 4); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param QueryResult $entryObjects |
41
|
|
|
* @throws InvalidOperationException |
42
|
|
|
*/ |
43
|
|
|
protected function checkElementsInput(QueryResult &$entryObjects) |
44
|
|
|
{ |
45
|
|
|
$res = $entryObjects->results; |
46
|
|
|
if (!(is_array($res) || $res instanceof Collection)) { |
47
|
|
|
throw new InvalidOperationException('!is_array($entryObjects->results)'); |
48
|
|
|
} |
49
|
|
|
if (is_array($res) && 0 == count($res)) { |
50
|
|
|
$entryObjects->hasMore = false; |
51
|
|
|
} |
52
|
|
|
if ($res instanceof Collection && 0 == $res->count()) { |
53
|
|
|
$entryObjects->hasMore = false; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param QueryResult $entryObject |
59
|
|
|
* @throws InvalidOperationException |
60
|
|
|
*/ |
61
|
|
|
protected function checkSingleElementInput(QueryResult $entryObject) |
62
|
|
|
{ |
63
|
|
|
if (!$entryObject->results instanceof Model) { |
64
|
|
|
$res = $entryObject->results; |
65
|
|
|
$msg = is_array($res) ? 'Entry object must be single Model' : get_class($res); |
66
|
|
|
throw new InvalidOperationException($msg); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param Model $entityInstance |
72
|
|
|
* @param ResourceType $resourceType |
73
|
|
|
* @param string $containerName |
74
|
|
|
* @return string |
75
|
|
|
* @throws InvalidOperationException |
76
|
|
|
* @throws ODataException |
77
|
|
|
* @throws \ReflectionException |
78
|
|
|
*/ |
79
|
|
|
protected function getEntryInstanceKey($entityInstance, ResourceType $resourceType, $containerName) |
80
|
|
|
{ |
81
|
|
|
$typeName = $resourceType->getName(); |
82
|
|
|
$keyProperties = $resourceType->getKeyProperties(); |
83
|
|
|
if (0 == count($keyProperties)) { |
84
|
|
|
throw new InvalidOperationException('count($keyProperties) == 0'); |
85
|
|
|
} |
86
|
|
|
$keyString = $containerName . '('; |
87
|
|
|
$comma = null; |
88
|
|
|
foreach ($keyProperties as $keyName => $resourceProperty) { |
89
|
|
|
$keyType = $resourceProperty->getInstanceType(); |
90
|
|
|
if (!$keyType instanceof IType) { |
91
|
|
|
throw new InvalidOperationException('$keyType not instanceof IType'); |
92
|
|
|
} |
93
|
|
|
$keyName = $resourceProperty->getName(); |
94
|
|
|
$keyValue = $entityInstance->$keyName; |
95
|
|
|
if (!isset($keyValue)) { |
96
|
|
|
throw ODataException::createInternalServerError( |
97
|
|
|
Messages::badQueryNullKeysAreNotSupported($typeName, $keyName) |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$keyValue = $keyType->convertToOData($keyValue); |
102
|
|
|
$keyString .= $comma . $keyName . '=' . $keyValue; |
103
|
|
|
$comma = ','; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$keyString .= ')'; |
107
|
|
|
|
108
|
|
|
return $keyString; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param ResourceEntityType $resourceType |
113
|
|
|
* @param string $payloadClass |
114
|
|
|
* @return ResourceEntityType|ResourceType |
115
|
|
|
* @throws InvalidOperationException |
116
|
|
|
* @throws \ReflectionException |
117
|
|
|
*/ |
118
|
|
|
protected function getConcreteTypeFromAbstractType(ResourceEntityType $resourceType, $payloadClass) |
119
|
|
|
{ |
120
|
|
|
if ($resourceType->isAbstract()) { |
121
|
|
|
$derived = $this->getMetadata()->getDerivedTypes($resourceType); |
122
|
|
|
if (0 == count($derived)) { |
123
|
|
|
throw new InvalidOperationException('Supplied abstract type must have at least one derived type'); |
124
|
|
|
} |
125
|
|
|
$derived = array_filter( |
126
|
|
|
$derived, |
127
|
|
|
function (ResourceType $element) { |
128
|
|
|
return !$element->isAbstract(); |
129
|
|
|
} |
130
|
|
|
); |
131
|
|
|
foreach ($derived as $rawType) { |
132
|
|
|
$name = $rawType->getInstanceType()->getName(); |
133
|
|
|
if ($payloadClass == $name) { |
134
|
|
|
$resourceType = $rawType; |
135
|
|
|
break; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
// despite all set up, checking, etc, if we haven't picked a concrete resource type, |
140
|
|
|
// wheels have fallen off, so blow up |
141
|
|
|
if ($resourceType->isAbstract()) { |
142
|
|
|
throw new InvalidOperationException('Concrete resource type not selected for payload ' . $payloadClass); |
143
|
|
|
} |
144
|
|
|
return $resourceType; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return IMetadataProvider |
149
|
|
|
*/ |
150
|
|
|
abstract protected function getMetadata(); |
151
|
|
|
} |
152
|
|
|
|