1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: alex |
5
|
|
|
* Date: 15/02/20 |
6
|
|
|
* Time: 8:05 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace AlgoWeb\PODataLaravel\Serialisers; |
10
|
|
|
|
11
|
|
|
use POData\Common\InvalidOperationException; |
12
|
|
|
use POData\UriProcessor\QueryProcessor\ExpandProjectionParser\ExpandedProjectionNode; |
13
|
|
|
use POData\UriProcessor\QueryProcessor\ExpandProjectionParser\ProjectionNode; |
14
|
|
|
use POData\UriProcessor\QueryProcessor\ExpandProjectionParser\RootProjectionNode; |
15
|
|
|
|
16
|
|
|
trait SerialiseNavigationTrait |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var RootProjectionNode |
20
|
|
|
*/ |
21
|
|
|
protected $rootNode = null; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Find a 'ExpandedProjectionNode' instance in the projection tree |
25
|
|
|
* which describes the current segment. |
26
|
|
|
* |
27
|
|
|
* @return null|RootProjectionNode|ExpandedProjectionNode |
28
|
|
|
* @throws InvalidOperationException |
29
|
|
|
*/ |
30
|
|
|
protected function getCurrentExpandedProjectionNode() |
31
|
|
|
{ |
32
|
|
|
if (null === $this->rootNode) { |
33
|
|
|
$this->rootNode = $this->getRequest()->getRootProjectionNode(); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
$expandedProjectionNode = $this->rootNode; |
36
|
|
|
if (null === $expandedProjectionNode) { |
37
|
|
|
return null; |
38
|
|
|
} |
39
|
|
|
$segmentNames = $this->getLightStack(); |
|
|
|
|
40
|
|
|
$depth = count($segmentNames); |
41
|
|
|
// $depth == 1 means serialization of root entry |
42
|
|
|
//(the resource identified by resource path) is going on, |
43
|
|
|
//so control won't get into the below for loop. |
44
|
|
|
//we will directly return the root node, |
45
|
|
|
//which is 'ExpandedProjectionNode' |
46
|
|
|
// for resource identified by resource path. |
47
|
|
|
if (0 != $depth) { |
48
|
|
|
for ($i = 1; $i < $depth; ++$i) { |
49
|
|
|
$segName = $segmentNames[$i]['prop']; |
50
|
|
|
$expandedProjectionNode = $expandedProjectionNode->findNode($segName); |
51
|
|
|
if (null === $expandedProjectionNode) { |
52
|
|
|
throw new InvalidOperationException('is_null($expandedProjectionNode)'); |
53
|
|
|
} |
54
|
|
|
if (!$expandedProjectionNode instanceof ExpandedProjectionNode) { |
55
|
|
|
$msg = '$expandedProjectionNode not instanceof ExpandedProjectionNode'; |
56
|
|
|
throw new InvalidOperationException($msg); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $expandedProjectionNode; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Gets collection of projection nodes under the current node. |
66
|
|
|
* |
67
|
|
|
* @return ProjectionNode[]|ExpandedProjectionNode[]|null List of nodes describing projections for the current |
68
|
|
|
* segment, If this method returns null it means no |
69
|
|
|
* projections are to be applied and the entire resource for |
70
|
|
|
* the current segment should be serialized, If it returns |
71
|
|
|
* non-null only the properties described by the returned |
72
|
|
|
* projection segments should be serialized |
73
|
|
|
* @throws InvalidOperationException |
74
|
|
|
*/ |
75
|
|
|
protected function getProjectionNodes() |
76
|
|
|
{ |
77
|
|
|
$expandedProjectionNode = $this->getCurrentExpandedProjectionNode(); |
78
|
|
|
if (null === $expandedProjectionNode || $expandedProjectionNode->canSelectAllProperties()) { |
79
|
|
|
return null; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $expandedProjectionNode->getChildNodes(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Check whether to expand a navigation property or not. |
87
|
|
|
* |
88
|
|
|
* @param string $navigationPropertyName Name of navigation property in question |
89
|
|
|
* |
90
|
|
|
* @return bool True if the given navigation should be expanded, otherwise false |
91
|
|
|
* @throws InvalidOperationException |
92
|
|
|
*/ |
93
|
|
|
protected function shouldExpandSegment($navigationPropertyName) |
94
|
|
|
{ |
95
|
|
|
$expandedProjectionNode = $this->getCurrentExpandedProjectionNode(); |
96
|
|
|
if (null === $expandedProjectionNode) { |
97
|
|
|
return false; |
98
|
|
|
} |
99
|
|
|
$expandedProjectionNode = $expandedProjectionNode->findNode($navigationPropertyName); |
100
|
|
|
|
101
|
|
|
// null is a valid input to an instanceof call as of PHP 5.6 - will always return false |
102
|
|
|
return $expandedProjectionNode instanceof ExpandedProjectionNode; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|