Completed
Push — master ( f7ebb3...191d10 )
by Alex
23s queued 11s
created

SerialiseNavigationTrait::shouldExpandSegment()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 2
nc 2
nop 1
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
use POData\UriProcessor\RequestDescription;
16
17
trait SerialiseNavigationTrait
18
{
19
    /**
20
     * @var RootProjectionNode
21
     */
22
    protected $rootNode = null;
23
24
    /**
25
     * Find a 'ExpandedProjectionNode' instance in the projection tree
26
     * which describes the current segment.
27
     *
28
     * @return null|RootProjectionNode|ExpandedProjectionNode
29
     * @throws InvalidOperationException
30
     */
31
    protected function getCurrentExpandedProjectionNode()
32
    {
33
        if (null === $this->rootNode) {
34
            $this->rootNode = $this->getRequest()->getRootProjectionNode();
35
        }
36
        $expandedProjectionNode = $this->rootNode;
37
        if (null === $expandedProjectionNode) {
38
            return null;
39
        }
40
        $segmentNames = $this->getLightStack();
41
        $depth = count($segmentNames);
42
        // $depth == 1 means serialization of root entry
43
        //(the resource identified by resource path) is going on,
44
        //so control won't get into the below for loop.
45
        //we will directly return the root node,
46
        //which is 'ExpandedProjectionNode'
47
        // for resource identified by resource path.
48
        if (0 != $depth) {
49
            for ($i = 1; $i < $depth; ++$i) {
50
                $segName = $segmentNames[$i]['prop'];
51
                $expandedProjectionNode = $expandedProjectionNode->findNode($segName);
52
                if (null === $expandedProjectionNode) {
53
                    throw new InvalidOperationException('is_null($expandedProjectionNode)');
54
                }
55
                if (!$expandedProjectionNode instanceof ExpandedProjectionNode) {
56
                    $msg = '$expandedProjectionNode not instanceof ExpandedProjectionNode';
57
                    throw new InvalidOperationException($msg);
58
                }
59
            }
60
        }
61
62
        return $expandedProjectionNode;
63
    }
64
65
    /**
66
     * Gets collection of projection nodes under the current node.
67
     *
68
     * @return ProjectionNode[]|ExpandedProjectionNode[]|null List of nodes describing projections for the current
69
     *                                                        segment, If this method returns null it means no
70
     *                                                        projections are to be applied and the entire resource for
71
     *                                                        the current segment should be serialized, If it returns
72
     *                                                        non-null only the properties described by the returned
73
     *                                                        projection segments should be serialized
74
     * @throws InvalidOperationException
75
     */
76
    protected function getProjectionNodes()
77
    {
78
        $expandedProjectionNode = $this->getCurrentExpandedProjectionNode();
79
        if (null === $expandedProjectionNode || $expandedProjectionNode->canSelectAllProperties()) {
80
            return null;
81
        }
82
83
        return $expandedProjectionNode->getChildNodes();
84
    }
85
86
    /**
87
     * Check whether to expand a navigation property or not.
88
     *
89
     * @param string $navigationPropertyName Name of navigation property in question
90
     *
91
     * @return bool True if the given navigation should be expanded, otherwise false
92
     * @throws InvalidOperationException
93
     */
94
    protected function shouldExpandSegment($navigationPropertyName)
95
    {
96
        $expandedProjectionNode = $this->getCurrentExpandedProjectionNode();
97
        if (null === $expandedProjectionNode) {
98
            return false;
99
        }
100
        $expandedProjectionNode = $expandedProjectionNode->findNode($navigationPropertyName);
101
102
        // null is a valid input to an instanceof call as of PHP 5.6 - will always return false
103
        return $expandedProjectionNode instanceof ExpandedProjectionNode;
104
    }
105
106
    /**
107
     * Gets reference to the request submitted by client.
108
     *
109
     * @return RequestDescription
110
     * @throws InvalidOperationException
111
     */
112
    abstract public function getRequest();
113
114
    /**
115
     * @return array
116
     */
117
    abstract protected function getLightStack();
118
}
119