1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Stack Exchange Api Client library. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2015 Beñat Espiña <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace BenatEspina\StackExchangeApiClient\Model\Traits; |
13
|
|
|
|
14
|
|
|
use BenatEspina\StackExchangeApiClient\Model\Interfaces\RelatedSiteInterface; |
15
|
|
|
use BenatEspina\StackExchangeApiClient\Model\RelatedSite; |
16
|
|
|
use BenatEspina\StackExchangeApiClient\Util\Util; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Trait SiteTrait. |
20
|
|
|
* |
21
|
|
|
* @author Beñat Espiña <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
trait SiteTrait |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* An array of related sites. |
27
|
|
|
* |
28
|
|
|
* @var array<\BenatEspina\StackExchangeApiClient\Model\Interfaces\RelatedSiteInterface>|null |
29
|
|
|
*/ |
30
|
|
|
protected $relatedSites = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Site state that can be 'normal', 'closed_beta', 'open_beta', or 'linked_meta'. |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $siteState; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Site type that can be 'main_site' or 'meta_site'. |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $siteType; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Site url. |
48
|
|
|
* |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
protected $siteUrl; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Adds related site. |
55
|
|
|
* |
56
|
|
|
* @param \Benatespina\StackExchangeApiClient\Model\Interfaces\RelatedSiteInterface $relatedSite The related site |
57
|
|
|
* |
58
|
|
|
* @return $this self Object |
59
|
|
|
*/ |
60
|
|
|
public function addRelatedSite(RelatedSiteInterface $relatedSite) |
61
|
|
|
{ |
62
|
|
|
$this->relatedSites[] = $relatedSite; |
63
|
|
|
|
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Removes related site. |
69
|
|
|
* |
70
|
|
|
* @param \Benatespina\StackExchangeApiClient\Model\Interfaces\RelatedSiteInterface $relatedSite The related site |
71
|
|
|
* |
72
|
|
|
* @return $this self Object |
73
|
|
|
*/ |
74
|
|
|
public function removeRelatedSite(RelatedSiteInterface $relatedSite) |
75
|
|
|
{ |
76
|
|
|
$this->relatedSites = Util::removeElement($relatedSite, $this->relatedSites); |
|
|
|
|
77
|
|
|
|
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Gets array of related sites. |
83
|
|
|
* |
84
|
|
|
* @return array<\Benatespina\StackExchangeApiClient\Model\Interfaces\RelatedSiteInterface>|null |
85
|
|
|
*/ |
86
|
|
|
public function getRelatedSites() |
87
|
|
|
{ |
88
|
|
|
return $this->relatedSites; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Sets site state. |
93
|
|
|
* |
94
|
|
|
* @param string $siteState The site state that can be 'normal', 'closed_beta', 'open_beta', or 'linked_meta' |
95
|
|
|
* |
96
|
|
|
* @return $this self Object |
97
|
|
|
*/ |
98
|
|
|
public function setSiteState($siteState) |
99
|
|
|
{ |
100
|
|
|
if (Util::coincidesElement( |
101
|
|
|
$siteState, ['closed_beta', 'linked_meta', 'normal', 'open_beta'] |
102
|
|
|
) === true |
103
|
|
|
) { |
104
|
|
|
$this->siteState = $siteState; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Gets site state. |
112
|
|
|
* |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
public function getSiteState() |
116
|
|
|
{ |
117
|
|
|
return $this->siteState; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Sets site type. |
122
|
|
|
* |
123
|
|
|
* @param string $siteType The site state that can be 'main_site' or 'meta_site' |
124
|
|
|
* |
125
|
|
|
* @return $this self Object |
126
|
|
|
*/ |
127
|
|
|
public function setSiteType($siteType) |
128
|
|
|
{ |
129
|
|
|
if (Util::coincidesElement($siteType, ['main_site', 'meta_site']) === true) { |
130
|
|
|
$this->siteType = $siteType; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Gets site type. |
138
|
|
|
* |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
|
public function getSiteType() |
142
|
|
|
{ |
143
|
|
|
return $this->siteType; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Sets site url. |
148
|
|
|
* |
149
|
|
|
* @param string $siteUrl The site url |
150
|
|
|
* |
151
|
|
|
* @return $this self Object |
152
|
|
|
*/ |
153
|
|
|
public function setSiteUrl($siteUrl) |
154
|
|
|
{ |
155
|
|
|
$this->siteUrl = $siteUrl; |
156
|
|
|
|
157
|
|
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Gets site name. |
162
|
|
|
* |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
|
|
public function getSiteUrl() |
166
|
|
|
{ |
167
|
|
|
return $this->siteUrl; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Loads the variables if the data exist into resource. It works like a constructor. |
172
|
|
|
* |
173
|
|
|
* @param null|mixed[] $resource The resource |
174
|
|
|
* @param string[] $stateConstants The array of site state constants |
175
|
|
|
* @param string[] $typeConstants The array of site type constants |
176
|
|
|
*/ |
177
|
|
|
protected function loadSite($resource, $stateConstants, $typeConstants) |
178
|
|
|
{ |
179
|
|
|
$sites = Util::setIfArrayExists($resource, 'related_sites'); |
180
|
|
|
foreach ($sites as $site) { |
181
|
|
|
$this->relatedSites[] = new RelatedSite($site); |
182
|
|
|
} |
183
|
|
|
$this->siteState = Util::isEqual($resource, 'site_state', $stateConstants); |
184
|
|
|
$this->siteType = Util::isEqual($resource, 'site_type', $typeConstants); |
185
|
|
|
$this->siteUrl = Util::setIfStringExists($resource, 'site_url'); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..