|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: alex |
|
5
|
|
|
* Date: 15/02/20 |
|
6
|
|
|
* Time: 6:00 PM |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace AlgoWeb\PODataLaravel\Serialisers; |
|
10
|
|
|
|
|
11
|
|
|
use Illuminate\Support\Facades\App; |
|
12
|
|
|
use POData\Common\InvalidOperationException; |
|
13
|
|
|
use POData\IService; |
|
14
|
|
|
use POData\Providers\Metadata\IMetadataProvider; |
|
15
|
|
|
use POData\Providers\Metadata\ResourceSetWrapper; |
|
16
|
|
|
use POData\UriProcessor\RequestDescription; |
|
17
|
|
|
use POData\UriProcessor\SegmentStack; |
|
18
|
|
|
|
|
19
|
|
|
trait SerialiseDepWrapperTrait |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* The service implementation. |
|
23
|
|
|
* |
|
24
|
|
|
* @var IService |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $service; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Holds reference to segment stack being processed. |
|
30
|
|
|
* |
|
31
|
|
|
* @var SegmentStack |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $stack; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Lightweight stack tracking for recursive descent fill. |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $lightStack = []; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Request description instance describes OData request the |
|
43
|
|
|
* the client has submitted and result of the request. |
|
44
|
|
|
* |
|
45
|
|
|
* @var RequestDescription |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $request; |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var ModelSerialiser |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $modelSerialiser; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var IMetadataProvider |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $metaProvider; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Absolute service Uri. |
|
62
|
|
|
* |
|
63
|
|
|
* @var string |
|
64
|
|
|
*/ |
|
65
|
|
|
protected $absoluteServiceUri; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Absolute service Uri with slash. |
|
69
|
|
|
* |
|
70
|
|
|
* @var string |
|
71
|
|
|
*/ |
|
72
|
|
|
protected $absoluteServiceUriWithSlash; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Gets the data service instance. |
|
76
|
|
|
* |
|
77
|
|
|
* @return IService |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getService() |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->service; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Sets the data service instance. |
|
86
|
|
|
* |
|
87
|
|
|
* @param IService $service |
|
88
|
|
|
* @return void |
|
89
|
|
|
*/ |
|
90
|
|
|
public function setService(IService $service) |
|
91
|
|
|
{ |
|
92
|
|
|
$this->service = $service; |
|
93
|
|
|
$this->absoluteServiceUri = $service->getHost()->getAbsoluteServiceUri()->getUrlAsString(); |
|
94
|
|
|
$this->absoluteServiceUriWithSlash = rtrim($this->absoluteServiceUri, '/') . '/'; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Gets the segment stack instance. |
|
99
|
|
|
* |
|
100
|
|
|
* @return SegmentStack |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getStack() |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->stack; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Gets reference to the request submitted by client. |
|
109
|
|
|
* |
|
110
|
|
|
* @return RequestDescription |
|
111
|
|
|
* @throws InvalidOperationException |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getRequest() |
|
114
|
|
|
{ |
|
115
|
|
|
if (null == $this->request) { |
|
116
|
|
|
throw new InvalidOperationException('Request not yet set'); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $this->request; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Sets reference to the request submitted by client. |
|
124
|
|
|
* |
|
125
|
|
|
* @param RequestDescription $request |
|
126
|
|
|
*/ |
|
127
|
|
|
public function setRequest(RequestDescription $request) |
|
128
|
|
|
{ |
|
129
|
|
|
$this->request = $request; |
|
130
|
|
|
$this->stack->setRequest($request); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @return ModelSerialiser |
|
135
|
|
|
*/ |
|
136
|
|
|
public function getModelSerialiser() |
|
137
|
|
|
{ |
|
138
|
|
|
return $this->modelSerialiser; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @return IMetadataProvider |
|
143
|
|
|
*/ |
|
144
|
|
|
protected function getMetadata() |
|
145
|
|
|
{ |
|
146
|
|
|
if (null == $this->metaProvider) { |
|
147
|
|
|
$this->metaProvider = App::make('metadata'); |
|
148
|
|
|
} |
|
149
|
|
|
return $this->metaProvider; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @return array |
|
154
|
|
|
*/ |
|
155
|
|
|
protected function getLightStack() |
|
156
|
|
|
{ |
|
157
|
|
|
return $this->lightStack; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @throws InvalidOperationException |
|
162
|
|
|
*/ |
|
163
|
|
|
protected function loadStackIfEmpty() |
|
164
|
|
|
{ |
|
165
|
|
|
if (0 == count($this->lightStack)) { |
|
166
|
|
|
$typeName = $this->getRequest()->getTargetResourceType()->getName(); |
|
167
|
|
|
array_push($this->lightStack, ['type' => $typeName, 'property' => $typeName, 'count' => 1]); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Resource set wrapper for the resource being serialized. |
|
173
|
|
|
* |
|
174
|
|
|
* @return ResourceSetWrapper |
|
175
|
|
|
* @throws InvalidOperationException |
|
176
|
|
|
*/ |
|
177
|
|
|
protected function getCurrentResourceSetWrapper() |
|
178
|
|
|
{ |
|
179
|
|
|
$segmentWrappers = $this->getStack()->getSegmentWrappers(); |
|
180
|
|
|
$count = count($segmentWrappers); |
|
181
|
|
|
|
|
182
|
|
|
return 0 == $count ? $this->getRequest()->getTargetResourceSetWrapper() : $segmentWrappers[$count-1]; |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|