Completed
Push — master ( 932cd1...29fc25 )
by Christopher
01:11
created

SegmentStack::setRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace POData\UriProcessor;
4
5
use POData\Common\InvalidOperationException;
6
use POData\Providers\Metadata\ResourceSetWrapper;
7
8
class SegmentStack
9
{
10
    /**
11
     * Collection of segment names.
12
     *
13
     * @var string[]
14
     */
15
    private $segmentNames;
16
17
    /**
18
     * Collection of segment ResourceSetWrapper instances.
19
     *
20
     * @var ResourceSetWrapper[]
21
     */
22
    private $segmentResourceSetWrappers;
23
24
    /**
25
     * Description of the OData request that a client has submitted.
26
     *
27
     * @var RequestDescription
28
     */
29
    private $request;
30
31
    private $mismatch = "Mismatch between size of names array and wrappers array";
32
33
    public function __construct(RequestDescription $request = null)
34
    {
35
        $this->request = $request;
36
        $this->segmentNames = [];
37
        $this->segmentResourceSetWrappers = [];
38
        assert(count($this->segmentNames) == count($this->segmentResourceSetWrappers), $this->mismatch);
39
    }
40
41
42
    /**
43
     * Pushes information about the segment whose instance is going to be
44
     * retrieved from the IDSQP implementation
45
     * Note: Calls to this method should be balanced with calls to popSegment.
46
     *
47
     * @param string             $segmentName         Name of segment to push
48
     * @param ResourceSetWrapper &$resourceSetWrapper The resource set wrapper
49
     *                                                to push
50
     *
51
     * @return bool true if the segment was push, false otherwise
52
     */
53
    public function pushSegment($segmentName, ResourceSetWrapper &$resourceSetWrapper)
54
    {
55
        if (!is_string($segmentName)) {
56
            throw new InvalidOperationException('segmentName must be a string');
57
        }
58
        $rootProjectionNode = $this->getRequest()->getRootProjectionNode();
59
        if (!is_null($rootProjectionNode) && $rootProjectionNode->isExpansionSpecified()) {
60
            array_push($this->segmentNames, $segmentName);
61
            array_push($this->segmentResourceSetWrappers, $resourceSetWrapper);
62
            assert(count($this->segmentNames) == count($this->segmentResourceSetWrappers), $this->mismatch);
63
64
            return true;
65
        }
66
67
        return false;
68
    }
69
70
    /**
71
     * Pops segment information from the 'Segment Stack'
72
     * Note: Calls to this method should be balanced with previous calls
73
     * to _pushSegment.
74
     *
75
     * @param bool $needPop Is a pop required. Only true if last push
76
     *                      was successful
77
     *
78
     * @throws InvalidOperationException If found un-balanced call
79
     *                                   with _pushSegment
80
     */
81
    public function popSegment($needPop)
82
    {
83
        if ($needPop) {
84
            if (!empty($this->segmentNames)) {
85
                array_pop($this->segmentNames);
86
                array_pop($this->segmentResourceSetWrappers);
87
88
                assert(count($this->segmentNames) == count($this->segmentResourceSetWrappers), $this->mismatch);
89
            } else {
90
                throw new InvalidOperationException(
91
                    'Found non-balanced call to pushSegment and popSegment'
92
                );
93
            }
94
        }
95
    }
96
97
    /**
98
     * Retrieve stored segment names
99
     *
100
     * @return \string[]
101
     */
102
    public function getSegmentNames()
103
    {
104
        return $this->segmentNames;
105
    }
106
107
    /**
108
     * Retrieve stored segment wrappers
109
     *
110
     * @return ResourceSetWrapper[]
111
     */
112
    public function getSegmentWrappers()
113
    {
114
        return $this->segmentResourceSetWrappers;
115
    }
116
117
    /**
118
     * Gets reference to the request submitted by client.
119
     *
120
     * @return RequestDescription
121
     */
122
    public function getRequest()
123
    {
124
        assert(null != $this->request, "Request must not be null");
125
        return $this->request;
126
    }
127
128
    /**
129
     * Sets reference to the request submitted by client.
130
     * @param RequestDescription $request
131
     *
132
     */
133
    public function setRequest(RequestDescription $request)
134
    {
135
        $this->request = $request;
136
    }
137
}
138