1 | <?php |
||
47 | class ObjectUrl extends Url implements PathInterface |
||
48 | { |
||
49 | /** |
||
50 | * Object path |
||
51 | * |
||
52 | * @var LocalPath |
||
53 | */ |
||
54 | protected $localPath = null; |
||
55 | |||
56 | /******************************************************************************* |
||
57 | * PUBLIC METHODS |
||
58 | *******************************************************************************/ |
||
59 | |||
60 | /** |
||
61 | * Object URL constructor |
||
62 | * |
||
63 | * @param string $url Object URL |
||
64 | * @param boolean $remote Accept remote URL (less strict date component checking) |
||
65 | * @throws InvalidArgumentException If remote URLs are not allowed and a remote URL is given |
||
66 | */ |
||
67 | 31 | public function __construct($url, $remote = false) |
|
68 | { |
||
69 | 31 | parent::__construct($url); |
|
70 | |||
71 | // If it's an invalid remote object URL |
||
72 | 30 | if ($this->isAbsolute() && !$remote) { |
|
73 | 1 | throw new InvalidArgumentException( |
|
74 | 1 | sprintf('Unallowed remote object URL "%s"', $url), |
|
75 | InvalidArgumentException::UNALLOWED_REMOTE_OBJECT_URL |
||
76 | 1 | ); |
|
77 | } |
||
78 | |||
79 | // Instantiate the local path component |
||
80 | 29 | $this->localPath = new LocalPath( |
|
81 | 29 | empty($this->urlParts['path']) ? '' : $this->urlParts['path'], |
|
82 | 29 | $remote ? true : null, $this->urlParts['path'] |
|
|
|||
83 | 29 | ); |
|
84 | |||
85 | // Normalize the path prefix |
||
86 | 26 | if (!strlen($this->urlParts['path'])) { |
|
87 | 12 | $this->urlParts['path'] = null; |
|
88 | 12 | } |
|
89 | 26 | } |
|
90 | |||
91 | /** |
||
92 | * Return the object's creation date |
||
93 | * |
||
94 | * @return \DateTimeImmutable Object creation date |
||
95 | */ |
||
96 | 6 | public function getCreationDate() |
|
100 | |||
101 | /** |
||
102 | * Set the object's creation date |
||
103 | * |
||
104 | * @param \DateTimeImmutable $creationDate |
||
105 | * @return LocalPath New object path |
||
106 | */ |
||
107 | 1 | public function setCreationDate(\DateTimeImmutable $creationDate) |
|
112 | |||
113 | /** |
||
114 | * Return the object type |
||
115 | * |
||
116 | * @return Type Object type |
||
117 | */ |
||
118 | 6 | public function getType() |
|
122 | |||
123 | /** |
||
124 | * Set the object type |
||
125 | * |
||
126 | * @param Type $type Object type |
||
127 | * @return ObjectUrl New object URL |
||
128 | */ |
||
129 | 1 | public function setType(Type $type) |
|
134 | |||
135 | /** |
||
136 | * Return the object ID |
||
137 | * |
||
138 | * @return Id Object ID |
||
139 | */ |
||
140 | 9 | public function getId() |
|
144 | |||
145 | /** |
||
146 | * Set the object ID |
||
147 | * |
||
148 | * @param Id $uid Object ID |
||
149 | * @return ObjectUrl New object URL |
||
150 | */ |
||
151 | 1 | public function setId(Id $uid) |
|
152 | { |
||
153 | 1 | $this->localPath = $this->localPath->setId($uid); |
|
154 | 1 | return $this; |
|
155 | } |
||
156 | |||
157 | |||
158 | /** |
||
159 | * Return the object revision |
||
160 | * |
||
161 | * @return Revision Object revision |
||
162 | */ |
||
163 | 6 | public function getRevision() |
|
167 | |||
168 | /** |
||
169 | * Set the object revision |
||
170 | * |
||
171 | * @param Revision $revision Object revision |
||
172 | * @return ObjectUrl New object URL |
||
173 | */ |
||
174 | 1 | public function setRevision(Revision $revision) |
|
179 | |||
180 | /** |
||
181 | * Test if this URL matches all available parts of a given URL |
||
182 | * |
||
183 | * @param Url $url Comparison URL |
||
184 | * @return bool This URL matches all available parts of the given URL |
||
185 | */ |
||
186 | 7 | public function matches(Url $url) |
|
187 | { |
||
188 | // If the standard URL components don't match |
||
189 | 7 | if (!parent::matches($url)) { |
|
190 | 5 | return false; |
|
191 | } |
||
192 | |||
193 | // Extended tests if it's an object URL |
||
194 | 4 | if ($url instanceof self) { |
|
195 | // Test the object creation date |
||
196 | 1 | if ($this->getCreationDate() != $url->getCreationDate()) { |
|
197 | 1 | return false; |
|
198 | } |
||
199 | |||
200 | // Test the object ID |
||
201 | 1 | if ($this->getId()->serialize() !== $url->getId()->serialize()) { |
|
202 | 1 | return false; |
|
203 | } |
||
204 | |||
205 | // Test the object type |
||
206 | 1 | if ($this->getType()->serialize() !== $url->getType()->serialize()) { |
|
207 | 1 | return false; |
|
208 | } |
||
209 | |||
210 | // Test the object revision |
||
211 | 1 | if ($this->getRevision()->serialize() !== $url->getRevision()->serialize()) { |
|
212 | 1 | return false; |
|
213 | } |
||
214 | 1 | } |
|
215 | |||
216 | 4 | return true; |
|
217 | } |
||
218 | |||
219 | /** |
||
220 | * Return the local object path |
||
221 | * |
||
222 | * @return LocalPath Local object path |
||
223 | */ |
||
224 | 1 | public function getLocalPath() |
|
225 | { |
||
226 | 1 | return $this->localPath; |
|
227 | } |
||
228 | |||
229 | /** |
||
230 | * Return the repository URL part of this object URL |
||
231 | * |
||
232 | * @return string Repository URL |
||
233 | * @see https://github.com/apparat/apparat/blob/master/doc/URL-DESIGN.md#repository-url |
||
234 | */ |
||
235 | 10 | public function getRepositoryUrl() |
|
257 | |||
258 | /******************************************************************************* |
||
259 | * PRIVATE METHODS |
||
260 | *******************************************************************************/ |
||
261 | |||
262 | /** |
||
263 | * Return the a complete serialized object URL |
||
264 | * |
||
265 | * @param array $override Override components |
||
266 | * @return string Serialized URL |
||
267 | */ |
||
268 | 8 | protected function getUrlInternal(array &$override = []) |
|
282 | } |
||
283 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.