Completed
Pull Request — master (#269)
by Christopher
14:37
created

ODataLink::getExpandedResult()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 0
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace POData\ObjectModel;
6
7
/**
8
 * Class ODataLink represents an OData Navigation Link.
9
 */
10
class ODataLink
11
{
12
    /**
13
     * Name of the link. This becomes last segment of rel attribute value.
14
     *
15
     * @var string
16
     */
17
    private $name;
18
    /**
19
     * Title of the link. This become value of title attribute.
20
     *
21
     * @var string
22
     */
23
    private $title;
24
    /**
25
     * Type of link.
26
     *
27
     * @var string
28
     */
29
    private $type;
30
    /**
31
     * Url to the navigation property. This become value of href attribute.
32
     *
33
     * @var string|null
34
     */
35
    private $url;
36
    /**
37
     * Checks is Expand result contains single entity or collection of
38
     * entities i.e. feed.
39
     *
40
     * @var bool
41
     */
42
    private $isCollection;
43
    /**
44
     * The expanded result. This becomes the inline content of the link.
45
     *
46
     * @var ODataExpandedResult|null
47
     */
48
    private $expandedResult;
49
    /**
50
     * True if Link is Expanded, False if not.
51
     *
52
     * @var bool
53
     */
54
    private $isExpanded;
55
56
    /**
57
     * ODataLink constructor.
58
     * @param string                   $name
59
     * @param string                   $title
60
     * @param string                   $type
61
     * @param string                   $url
62
     * @param bool                     $isCollection
63
     * @param ODataExpandedResult|null $expandedResult
64
     */
65
    public function __construct(string $name = null, string $title = null, string $type = null, string $url = null, bool $isCollection = null, ODataExpandedResult $expandedResult = null, bool $isExpanded = null)
66
    {
67
        $this
68
            ->setName($name)
69
            ->setTitle($title)
70
            ->setType($type)
71
            ->setUrl($url)
72
            ->setIsCollection($isCollection)
73
            ->setExpandedResult($expandedResult)
74
            ->setIsExpanded($isExpanded);
75
    }
76
    /**
77
     * @return string
78
     */
79
    public function getName(): ?string
80
    {
81
        return $this->name;
82
    }
83
84
    /**
85
     * @param  string    $name
86
     * @return ODataLink
87
     */
88
    public function setName(?string $name): ODataLink
89
    {
90
        $this->name = $name;
91
        return $this;
92
    }
93
94
    /**
95
     * @return string
96
     */
97
    public function getTitle(): ?string
98
    {
99
        return $this->title;
100
    }
101
102
    /**
103
     * @param  string    $title
104
     * @return ODataLink
105
     */
106
    public function setTitle(?string $title): ODataLink
107
    {
108
        $this->title = $title;
109
        return $this;
110
    }
111
112
    /**
113
     * @return string|null
114
     */
115
    public function getType(): ?string
116
    {
117
        return $this->type;
118
    }
119
120
    /**
121
     * @param  string    $type
122
     * @return ODataLink
123
     */
124
    public function setType(?string $type): ODataLink
125
    {
126
        $this->type = $type;
127
        return $this;
128
    }
129
130
    /**
131
     * @return string
132
     */
133
    public function getUrl(): ?string
134
    {
135
        return $this->url;
136
    }
137
138
    /**
139
     * @param  string    $url
140
     * @return ODataLink
141
     */
142
    public function setUrl(?string $url): ODataLink
143
    {
144
        $this->url = $url;
145
        return $this;
146
    }
147
148
    /**
149
     * @return bool
150
     */
151
    public function isCollection(): ?bool
152
    {
153
        return $this->isCollection;
154
    }
155
156
    /**
157
     * @param  bool      $isCollection
158
     * @return ODataLink
159
     */
160
    public function setIsCollection(?bool $isCollection): ODataLink
161
    {
162
        $this->isCollection = $isCollection;
163
        return $this;
164
    }
165
166
    /**
167
     * @return bool
168
     */
169
    public function isExpanded(): ?bool
170
    {
171
        return $this->isExpanded;
172
    }
173
174
    /**
175
     * @param  bool      $isExpanded
176
     * @return ODataLink
177
     */
178
    public function setIsExpanded(?bool $isExpanded): ODataLink
179
    {
180
        $this->isExpanded = $isExpanded;
181
        return $this;
182
    }
183
184
    /**
185
     * @return null|ODataExpandedResult
186
     */
187
    public function getExpandedResult(): ?ODataExpandedResult
188
    {
189
        if (!$this->isExpanded) {
190
            return null;
191
        }
192
        if ($this->expandedResult === null) {
193
            return null;
194
        }
195
        return $this->expandedResult;
196
    }
197
198
    /**
199
     * @param ODataExpandedResult $eResult
200
     */
201
    public function setExpandedResult(?ODataExpandedResult $eResult): self
202
    {
203
        if (null === $eResult) {
204
            $this->expandedResult = $eResult;
205
            return $this;
206
        }
207
        if (null !== $eResult->getFeed()) {
208
            $this->isExpanded     = true;
209
            $this->isCollection   = true;
210
            $this->expandedResult = $eResult;
211
        }
212
        if (null !== $eResult->getEntry()) {
213
            $this->isExpanded     = true;
214
            $this->isCollection   = false;
215
            $this->expandedResult = $eResult;
216
        }
217
        return $this;
218
    }
219
220
    /**
221
     * @return bool
222
     */
223
    public function isEmpty()
224
    {
225
        return !array_reduce(
226
            ['isExpanded', 'isCollection', 'expandedResult', 'title', 'type', 'name', 'url'],
227
            function ($carry, $value){
228
                return $carry || isset($this->{$value});
229
            }, false);
230
    }
231
}
232