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\ResourceType; |
17
|
|
|
use POData\Providers\Metadata\Type\IType; |
18
|
|
|
use POData\Providers\Query\QueryResult; |
19
|
|
|
|
20
|
|
|
trait SerialiseUtilitiesTrait |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @param int $resourceKind |
24
|
|
|
* @return bool |
25
|
|
|
*/ |
26
|
|
|
public static function isMatchPrimitive($resourceKind) |
27
|
|
|
{ |
28
|
|
|
if (16 > $resourceKind) { |
29
|
|
|
return false; |
30
|
|
|
} |
31
|
|
|
if (28 < $resourceKind) { |
32
|
|
|
return false; |
33
|
|
|
} |
34
|
|
|
return 0 == ($resourceKind % 4); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param QueryResult $entryObjects |
39
|
|
|
* @throws InvalidOperationException |
40
|
|
|
*/ |
41
|
|
|
protected function checkElementsInput(QueryResult &$entryObjects) |
42
|
|
|
{ |
43
|
|
|
$res = $entryObjects->results; |
44
|
|
|
if (!(is_array($res) || $res instanceof Collection)) { |
45
|
|
|
throw new InvalidOperationException('!is_array($entryObjects->results)'); |
46
|
|
|
} |
47
|
|
|
if (is_array($res) && 0 == count($res)) { |
48
|
|
|
$entryObjects->hasMore = false; |
49
|
|
|
} |
50
|
|
|
if ($res instanceof Collection && 0 == $res->count()) { |
51
|
|
|
$entryObjects->hasMore = false; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param QueryResult $entryObject |
57
|
|
|
* @throws InvalidOperationException |
58
|
|
|
*/ |
59
|
|
|
protected function checkSingleElementInput(QueryResult $entryObject) |
60
|
|
|
{ |
61
|
|
|
if (!$entryObject->results instanceof Model) { |
62
|
|
|
$res = $entryObject->results; |
63
|
|
|
$msg = is_array($res) ? 'Entry object must be single Model' : get_class($res); |
64
|
|
|
throw new InvalidOperationException($msg); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param Model $entityInstance |
70
|
|
|
* @param ResourceType $resourceType |
71
|
|
|
* @param string $containerName |
72
|
|
|
* @return string |
73
|
|
|
* @throws InvalidOperationException |
74
|
|
|
* @throws ODataException |
75
|
|
|
* @throws \ReflectionException |
76
|
|
|
*/ |
77
|
|
|
protected function getEntryInstanceKey($entityInstance, ResourceType $resourceType, $containerName) |
78
|
|
|
{ |
79
|
|
|
$typeName = $resourceType->getName(); |
80
|
|
|
$keyProperties = $resourceType->getKeyProperties(); |
81
|
|
|
if (0 == count($keyProperties)) { |
82
|
|
|
throw new InvalidOperationException('count($keyProperties) == 0'); |
83
|
|
|
} |
84
|
|
|
$keyString = $containerName . '('; |
85
|
|
|
$comma = null; |
86
|
|
|
foreach ($keyProperties as $keyName => $resourceProperty) { |
87
|
|
|
$keyType = $resourceProperty->getInstanceType(); |
88
|
|
|
if (!$keyType instanceof IType) { |
89
|
|
|
throw new InvalidOperationException('$keyType not instanceof IType'); |
90
|
|
|
} |
91
|
|
|
$keyName = $resourceProperty->getName(); |
92
|
|
|
$keyValue = $entityInstance->$keyName; |
93
|
|
|
if (!isset($keyValue)) { |
94
|
|
|
throw ODataException::createInternalServerError( |
95
|
|
|
Messages::badQueryNullKeysAreNotSupported($typeName, $keyName) |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$keyValue = $keyType->convertToOData($keyValue); |
100
|
|
|
$keyString .= $comma . $keyName . '=' . $keyValue; |
101
|
|
|
$comma = ','; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$keyString .= ')'; |
105
|
|
|
|
106
|
|
|
return $keyString; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|