@@ -30,395 +30,395 @@ |
||
| 30 | 30 | |
| 31 | 31 | class Comment implements IComment { |
| 32 | 32 | |
| 33 | - protected $data = [ |
|
| 34 | - 'id' => '', |
|
| 35 | - 'parentId' => '0', |
|
| 36 | - 'topmostParentId' => '0', |
|
| 37 | - 'childrenCount' => '0', |
|
| 38 | - 'message' => '', |
|
| 39 | - 'verb' => '', |
|
| 40 | - 'actorType' => '', |
|
| 41 | - 'actorId' => '', |
|
| 42 | - 'objectType' => '', |
|
| 43 | - 'objectId' => '', |
|
| 44 | - 'creationDT' => null, |
|
| 45 | - 'latestChildDT' => null, |
|
| 46 | - ]; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Comment constructor. |
|
| 50 | - * |
|
| 51 | - * @param array $data optional, array with keys according to column names from |
|
| 52 | - * the comments database scheme |
|
| 53 | - */ |
|
| 54 | - public function __construct(array $data = null) { |
|
| 55 | - if(is_array($data)) { |
|
| 56 | - $this->fromArray($data); |
|
| 57 | - } |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * returns the ID of the comment |
|
| 62 | - * |
|
| 63 | - * It may return an empty string, if the comment was not stored. |
|
| 64 | - * It is expected that the concrete Comment implementation gives an ID |
|
| 65 | - * by itself (e.g. after saving). |
|
| 66 | - * |
|
| 67 | - * @return string |
|
| 68 | - * @since 9.0.0 |
|
| 69 | - */ |
|
| 70 | - public function getId() { |
|
| 71 | - return $this->data['id']; |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * sets the ID of the comment and returns itself |
|
| 76 | - * |
|
| 77 | - * It is only allowed to set the ID only, if the current id is an empty |
|
| 78 | - * string (which means it is not stored in a database, storage or whatever |
|
| 79 | - * the concrete implementation does), or vice versa. Changing a given ID is |
|
| 80 | - * not permitted and must result in an IllegalIDChangeException. |
|
| 81 | - * |
|
| 82 | - * @param string $id |
|
| 83 | - * @return IComment |
|
| 84 | - * @throws IllegalIDChangeException |
|
| 85 | - * @since 9.0.0 |
|
| 86 | - */ |
|
| 87 | - public function setId($id) { |
|
| 88 | - if(!is_string($id)) { |
|
| 89 | - throw new \InvalidArgumentException('String expected.'); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - $id = trim($id); |
|
| 93 | - if($this->data['id'] === '' || ($this->data['id'] !== '' && $id === '')) { |
|
| 94 | - $this->data['id'] = $id; |
|
| 95 | - return $this; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - throw new IllegalIDChangeException('Not allowed to assign a new ID to an already saved comment.'); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * returns the parent ID of the comment |
|
| 103 | - * |
|
| 104 | - * @return string |
|
| 105 | - * @since 9.0.0 |
|
| 106 | - */ |
|
| 107 | - public function getParentId() { |
|
| 108 | - return $this->data['parentId']; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * sets the parent ID and returns itself |
|
| 113 | - * |
|
| 114 | - * @param string $parentId |
|
| 115 | - * @return IComment |
|
| 116 | - * @since 9.0.0 |
|
| 117 | - */ |
|
| 118 | - public function setParentId($parentId) { |
|
| 119 | - if(!is_string($parentId)) { |
|
| 120 | - throw new \InvalidArgumentException('String expected.'); |
|
| 121 | - } |
|
| 122 | - $this->data['parentId'] = trim($parentId); |
|
| 123 | - return $this; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * returns the topmost parent ID of the comment |
|
| 128 | - * |
|
| 129 | - * @return string |
|
| 130 | - * @since 9.0.0 |
|
| 131 | - */ |
|
| 132 | - public function getTopmostParentId() { |
|
| 133 | - return $this->data['topmostParentId']; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * sets the topmost parent ID and returns itself |
|
| 139 | - * |
|
| 140 | - * @param string $id |
|
| 141 | - * @return IComment |
|
| 142 | - * @since 9.0.0 |
|
| 143 | - */ |
|
| 144 | - public function setTopmostParentId($id) { |
|
| 145 | - if(!is_string($id)) { |
|
| 146 | - throw new \InvalidArgumentException('String expected.'); |
|
| 147 | - } |
|
| 148 | - $this->data['topmostParentId'] = trim($id); |
|
| 149 | - return $this; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * returns the number of children |
|
| 154 | - * |
|
| 155 | - * @return int |
|
| 156 | - * @since 9.0.0 |
|
| 157 | - */ |
|
| 158 | - public function getChildrenCount() { |
|
| 159 | - return $this->data['childrenCount']; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * sets the number of children |
|
| 164 | - * |
|
| 165 | - * @param int $count |
|
| 166 | - * @return IComment |
|
| 167 | - * @since 9.0.0 |
|
| 168 | - */ |
|
| 169 | - public function setChildrenCount($count) { |
|
| 170 | - if(!is_int($count)) { |
|
| 171 | - throw new \InvalidArgumentException('Integer expected.'); |
|
| 172 | - } |
|
| 173 | - $this->data['childrenCount'] = $count; |
|
| 174 | - return $this; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * returns the message of the comment |
|
| 179 | - * |
|
| 180 | - * @return string |
|
| 181 | - * @since 9.0.0 |
|
| 182 | - */ |
|
| 183 | - public function getMessage() { |
|
| 184 | - return $this->data['message']; |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * sets the message of the comment and returns itself |
|
| 189 | - * |
|
| 190 | - * @param string $message |
|
| 191 | - * @param int $maxLength |
|
| 192 | - * @return IComment |
|
| 193 | - * @throws MessageTooLongException |
|
| 194 | - * @since 9.0.0 |
|
| 195 | - */ |
|
| 196 | - public function setMessage($message, $maxLength = self::MAX_MESSAGE_LENGTH) { |
|
| 197 | - if(!is_string($message)) { |
|
| 198 | - throw new \InvalidArgumentException('String expected.'); |
|
| 199 | - } |
|
| 200 | - $message = trim($message); |
|
| 201 | - if ($maxLength && mb_strlen($message, 'UTF-8') > $maxLength) { |
|
| 202 | - throw new MessageTooLongException('Comment message must not exceed ' . $maxLength. ' characters'); |
|
| 203 | - } |
|
| 204 | - $this->data['message'] = $message; |
|
| 205 | - return $this; |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * returns an array containing mentions that are included in the comment |
|
| 210 | - * |
|
| 211 | - * @return array each mention provides a 'type' and an 'id', see example below |
|
| 212 | - * @since 11.0.0 |
|
| 213 | - * |
|
| 214 | - * The return array looks like: |
|
| 215 | - * [ |
|
| 216 | - * [ |
|
| 217 | - * 'type' => 'user', |
|
| 218 | - * 'id' => 'citizen4' |
|
| 219 | - * ], |
|
| 220 | - * [ |
|
| 221 | - * 'type' => 'group', |
|
| 222 | - * 'id' => 'media' |
|
| 223 | - * ], |
|
| 224 | - * … |
|
| 225 | - * ] |
|
| 226 | - * |
|
| 227 | - */ |
|
| 228 | - public function getMentions() { |
|
| 229 | - $ok = preg_match_all("/\B(?<![^a-z0-9_\-@\.\'\s])@(\"guest\/[a-f0-9]+\"|\"[a-z0-9_\-@\.\' ]+\"|[a-z0-9_\-@\.\']+)/i", $this->getMessage(), $mentions); |
|
| 230 | - if(!$ok || !isset($mentions[0]) || !is_array($mentions[0])) { |
|
| 231 | - return []; |
|
| 232 | - } |
|
| 233 | - $uids = array_unique($mentions[0]); |
|
| 234 | - $result = []; |
|
| 235 | - foreach ($uids as $uid) { |
|
| 236 | - $cleanUid = trim(substr($uid, 1), '"'); |
|
| 237 | - if (strpos($cleanUid, 'guest/') === 0) { |
|
| 238 | - $result[] = ['type' => 'guest', 'id' => $cleanUid]; |
|
| 239 | - } else { |
|
| 240 | - $result[] = ['type' => 'user', 'id' => $cleanUid]; |
|
| 241 | - } |
|
| 242 | - } |
|
| 243 | - return $result; |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - /** |
|
| 247 | - * returns the verb of the comment |
|
| 248 | - * |
|
| 249 | - * @return string |
|
| 250 | - * @since 9.0.0 |
|
| 251 | - */ |
|
| 252 | - public function getVerb() { |
|
| 253 | - return $this->data['verb']; |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - /** |
|
| 257 | - * sets the verb of the comment, e.g. 'comment' or 'like' |
|
| 258 | - * |
|
| 259 | - * @param string $verb |
|
| 260 | - * @return IComment |
|
| 261 | - * @since 9.0.0 |
|
| 262 | - */ |
|
| 263 | - public function setVerb($verb) { |
|
| 264 | - if(!is_string($verb) || !trim($verb)) { |
|
| 265 | - throw new \InvalidArgumentException('Non-empty String expected.'); |
|
| 266 | - } |
|
| 267 | - $this->data['verb'] = trim($verb); |
|
| 268 | - return $this; |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - /** |
|
| 272 | - * returns the actor type |
|
| 273 | - * |
|
| 274 | - * @return string |
|
| 275 | - * @since 9.0.0 |
|
| 276 | - */ |
|
| 277 | - public function getActorType() { |
|
| 278 | - return $this->data['actorType']; |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - /** |
|
| 282 | - * returns the actor ID |
|
| 283 | - * |
|
| 284 | - * @return string |
|
| 285 | - * @since 9.0.0 |
|
| 286 | - */ |
|
| 287 | - public function getActorId() { |
|
| 288 | - return $this->data['actorId']; |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - /** |
|
| 292 | - * sets (overwrites) the actor type and id |
|
| 293 | - * |
|
| 294 | - * @param string $actorType e.g. 'users' |
|
| 295 | - * @param string $actorId e.g. 'zombie234' |
|
| 296 | - * @return IComment |
|
| 297 | - * @since 9.0.0 |
|
| 298 | - */ |
|
| 299 | - public function setActor($actorType, $actorId) { |
|
| 300 | - if( |
|
| 301 | - !is_string($actorType) || !trim($actorType) |
|
| 302 | - || !is_string($actorId) || !trim($actorId) |
|
| 303 | - ) { |
|
| 304 | - throw new \InvalidArgumentException('String expected.'); |
|
| 305 | - } |
|
| 306 | - $this->data['actorType'] = trim($actorType); |
|
| 307 | - $this->data['actorId'] = trim($actorId); |
|
| 308 | - return $this; |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - /** |
|
| 312 | - * returns the creation date of the comment. |
|
| 313 | - * |
|
| 314 | - * If not explicitly set, it shall default to the time of initialization. |
|
| 315 | - * |
|
| 316 | - * @return \DateTime |
|
| 317 | - * @since 9.0.0 |
|
| 318 | - */ |
|
| 319 | - public function getCreationDateTime() { |
|
| 320 | - return $this->data['creationDT']; |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - /** |
|
| 324 | - * sets the creation date of the comment and returns itself |
|
| 325 | - * |
|
| 326 | - * @param \DateTime $timestamp |
|
| 327 | - * @return IComment |
|
| 328 | - * @since 9.0.0 |
|
| 329 | - */ |
|
| 330 | - public function setCreationDateTime(\DateTime $timestamp) { |
|
| 331 | - $this->data['creationDT'] = $timestamp; |
|
| 332 | - return $this; |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - /** |
|
| 336 | - * returns the DateTime of the most recent child, if set, otherwise null |
|
| 337 | - * |
|
| 338 | - * @return \DateTime|null |
|
| 339 | - * @since 9.0.0 |
|
| 340 | - */ |
|
| 341 | - public function getLatestChildDateTime() { |
|
| 342 | - return $this->data['latestChildDT']; |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - /** |
|
| 346 | - * sets the date of the most recent child |
|
| 347 | - * |
|
| 348 | - * @param \DateTime $dateTime |
|
| 349 | - * @return IComment |
|
| 350 | - * @since 9.0.0 |
|
| 351 | - */ |
|
| 352 | - public function setLatestChildDateTime(\DateTime $dateTime = null) { |
|
| 353 | - $this->data['latestChildDT'] = $dateTime; |
|
| 354 | - return $this; |
|
| 355 | - } |
|
| 356 | - |
|
| 357 | - /** |
|
| 358 | - * returns the object type the comment is attached to |
|
| 359 | - * |
|
| 360 | - * @return string |
|
| 361 | - * @since 9.0.0 |
|
| 362 | - */ |
|
| 363 | - public function getObjectType() { |
|
| 364 | - return $this->data['objectType']; |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - /** |
|
| 368 | - * returns the object id the comment is attached to |
|
| 369 | - * |
|
| 370 | - * @return string |
|
| 371 | - * @since 9.0.0 |
|
| 372 | - */ |
|
| 373 | - public function getObjectId() { |
|
| 374 | - return $this->data['objectId']; |
|
| 375 | - } |
|
| 376 | - |
|
| 377 | - /** |
|
| 378 | - * sets (overwrites) the object of the comment |
|
| 379 | - * |
|
| 380 | - * @param string $objectType e.g. 'files' |
|
| 381 | - * @param string $objectId e.g. '16435' |
|
| 382 | - * @return IComment |
|
| 383 | - * @since 9.0.0 |
|
| 384 | - */ |
|
| 385 | - public function setObject($objectType, $objectId) { |
|
| 386 | - if( |
|
| 387 | - !is_string($objectType) || !trim($objectType) |
|
| 388 | - || !is_string($objectId) || !trim($objectId) |
|
| 389 | - ) { |
|
| 390 | - throw new \InvalidArgumentException('String expected.'); |
|
| 391 | - } |
|
| 392 | - $this->data['objectType'] = trim($objectType); |
|
| 393 | - $this->data['objectId'] = trim($objectId); |
|
| 394 | - return $this; |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - /** |
|
| 398 | - * sets the comment data based on an array with keys as taken from the |
|
| 399 | - * database. |
|
| 400 | - * |
|
| 401 | - * @param array $data |
|
| 402 | - * @return IComment |
|
| 403 | - */ |
|
| 404 | - protected function fromArray($data) { |
|
| 405 | - foreach(array_keys($data) as $key) { |
|
| 406 | - // translate DB keys to internal setter names |
|
| 407 | - $setter = 'set' . implode('', array_map('ucfirst', explode('_', $key))); |
|
| 408 | - $setter = str_replace('Timestamp', 'DateTime', $setter); |
|
| 409 | - |
|
| 410 | - if(method_exists($this, $setter)) { |
|
| 411 | - $this->$setter($data[$key]); |
|
| 412 | - } |
|
| 413 | - } |
|
| 414 | - |
|
| 415 | - foreach(['actor', 'object'] as $role) { |
|
| 416 | - if(isset($data[$role . '_type']) && isset($data[$role . '_id'])) { |
|
| 417 | - $setter = 'set' . ucfirst($role); |
|
| 418 | - $this->$setter($data[$role . '_type'], $data[$role . '_id']); |
|
| 419 | - } |
|
| 420 | - } |
|
| 421 | - |
|
| 422 | - return $this; |
|
| 423 | - } |
|
| 33 | + protected $data = [ |
|
| 34 | + 'id' => '', |
|
| 35 | + 'parentId' => '0', |
|
| 36 | + 'topmostParentId' => '0', |
|
| 37 | + 'childrenCount' => '0', |
|
| 38 | + 'message' => '', |
|
| 39 | + 'verb' => '', |
|
| 40 | + 'actorType' => '', |
|
| 41 | + 'actorId' => '', |
|
| 42 | + 'objectType' => '', |
|
| 43 | + 'objectId' => '', |
|
| 44 | + 'creationDT' => null, |
|
| 45 | + 'latestChildDT' => null, |
|
| 46 | + ]; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Comment constructor. |
|
| 50 | + * |
|
| 51 | + * @param array $data optional, array with keys according to column names from |
|
| 52 | + * the comments database scheme |
|
| 53 | + */ |
|
| 54 | + public function __construct(array $data = null) { |
|
| 55 | + if(is_array($data)) { |
|
| 56 | + $this->fromArray($data); |
|
| 57 | + } |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * returns the ID of the comment |
|
| 62 | + * |
|
| 63 | + * It may return an empty string, if the comment was not stored. |
|
| 64 | + * It is expected that the concrete Comment implementation gives an ID |
|
| 65 | + * by itself (e.g. after saving). |
|
| 66 | + * |
|
| 67 | + * @return string |
|
| 68 | + * @since 9.0.0 |
|
| 69 | + */ |
|
| 70 | + public function getId() { |
|
| 71 | + return $this->data['id']; |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * sets the ID of the comment and returns itself |
|
| 76 | + * |
|
| 77 | + * It is only allowed to set the ID only, if the current id is an empty |
|
| 78 | + * string (which means it is not stored in a database, storage or whatever |
|
| 79 | + * the concrete implementation does), or vice versa. Changing a given ID is |
|
| 80 | + * not permitted and must result in an IllegalIDChangeException. |
|
| 81 | + * |
|
| 82 | + * @param string $id |
|
| 83 | + * @return IComment |
|
| 84 | + * @throws IllegalIDChangeException |
|
| 85 | + * @since 9.0.0 |
|
| 86 | + */ |
|
| 87 | + public function setId($id) { |
|
| 88 | + if(!is_string($id)) { |
|
| 89 | + throw new \InvalidArgumentException('String expected.'); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + $id = trim($id); |
|
| 93 | + if($this->data['id'] === '' || ($this->data['id'] !== '' && $id === '')) { |
|
| 94 | + $this->data['id'] = $id; |
|
| 95 | + return $this; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + throw new IllegalIDChangeException('Not allowed to assign a new ID to an already saved comment.'); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * returns the parent ID of the comment |
|
| 103 | + * |
|
| 104 | + * @return string |
|
| 105 | + * @since 9.0.0 |
|
| 106 | + */ |
|
| 107 | + public function getParentId() { |
|
| 108 | + return $this->data['parentId']; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * sets the parent ID and returns itself |
|
| 113 | + * |
|
| 114 | + * @param string $parentId |
|
| 115 | + * @return IComment |
|
| 116 | + * @since 9.0.0 |
|
| 117 | + */ |
|
| 118 | + public function setParentId($parentId) { |
|
| 119 | + if(!is_string($parentId)) { |
|
| 120 | + throw new \InvalidArgumentException('String expected.'); |
|
| 121 | + } |
|
| 122 | + $this->data['parentId'] = trim($parentId); |
|
| 123 | + return $this; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * returns the topmost parent ID of the comment |
|
| 128 | + * |
|
| 129 | + * @return string |
|
| 130 | + * @since 9.0.0 |
|
| 131 | + */ |
|
| 132 | + public function getTopmostParentId() { |
|
| 133 | + return $this->data['topmostParentId']; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * sets the topmost parent ID and returns itself |
|
| 139 | + * |
|
| 140 | + * @param string $id |
|
| 141 | + * @return IComment |
|
| 142 | + * @since 9.0.0 |
|
| 143 | + */ |
|
| 144 | + public function setTopmostParentId($id) { |
|
| 145 | + if(!is_string($id)) { |
|
| 146 | + throw new \InvalidArgumentException('String expected.'); |
|
| 147 | + } |
|
| 148 | + $this->data['topmostParentId'] = trim($id); |
|
| 149 | + return $this; |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * returns the number of children |
|
| 154 | + * |
|
| 155 | + * @return int |
|
| 156 | + * @since 9.0.0 |
|
| 157 | + */ |
|
| 158 | + public function getChildrenCount() { |
|
| 159 | + return $this->data['childrenCount']; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * sets the number of children |
|
| 164 | + * |
|
| 165 | + * @param int $count |
|
| 166 | + * @return IComment |
|
| 167 | + * @since 9.0.0 |
|
| 168 | + */ |
|
| 169 | + public function setChildrenCount($count) { |
|
| 170 | + if(!is_int($count)) { |
|
| 171 | + throw new \InvalidArgumentException('Integer expected.'); |
|
| 172 | + } |
|
| 173 | + $this->data['childrenCount'] = $count; |
|
| 174 | + return $this; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * returns the message of the comment |
|
| 179 | + * |
|
| 180 | + * @return string |
|
| 181 | + * @since 9.0.0 |
|
| 182 | + */ |
|
| 183 | + public function getMessage() { |
|
| 184 | + return $this->data['message']; |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * sets the message of the comment and returns itself |
|
| 189 | + * |
|
| 190 | + * @param string $message |
|
| 191 | + * @param int $maxLength |
|
| 192 | + * @return IComment |
|
| 193 | + * @throws MessageTooLongException |
|
| 194 | + * @since 9.0.0 |
|
| 195 | + */ |
|
| 196 | + public function setMessage($message, $maxLength = self::MAX_MESSAGE_LENGTH) { |
|
| 197 | + if(!is_string($message)) { |
|
| 198 | + throw new \InvalidArgumentException('String expected.'); |
|
| 199 | + } |
|
| 200 | + $message = trim($message); |
|
| 201 | + if ($maxLength && mb_strlen($message, 'UTF-8') > $maxLength) { |
|
| 202 | + throw new MessageTooLongException('Comment message must not exceed ' . $maxLength. ' characters'); |
|
| 203 | + } |
|
| 204 | + $this->data['message'] = $message; |
|
| 205 | + return $this; |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * returns an array containing mentions that are included in the comment |
|
| 210 | + * |
|
| 211 | + * @return array each mention provides a 'type' and an 'id', see example below |
|
| 212 | + * @since 11.0.0 |
|
| 213 | + * |
|
| 214 | + * The return array looks like: |
|
| 215 | + * [ |
|
| 216 | + * [ |
|
| 217 | + * 'type' => 'user', |
|
| 218 | + * 'id' => 'citizen4' |
|
| 219 | + * ], |
|
| 220 | + * [ |
|
| 221 | + * 'type' => 'group', |
|
| 222 | + * 'id' => 'media' |
|
| 223 | + * ], |
|
| 224 | + * … |
|
| 225 | + * ] |
|
| 226 | + * |
|
| 227 | + */ |
|
| 228 | + public function getMentions() { |
|
| 229 | + $ok = preg_match_all("/\B(?<![^a-z0-9_\-@\.\'\s])@(\"guest\/[a-f0-9]+\"|\"[a-z0-9_\-@\.\' ]+\"|[a-z0-9_\-@\.\']+)/i", $this->getMessage(), $mentions); |
|
| 230 | + if(!$ok || !isset($mentions[0]) || !is_array($mentions[0])) { |
|
| 231 | + return []; |
|
| 232 | + } |
|
| 233 | + $uids = array_unique($mentions[0]); |
|
| 234 | + $result = []; |
|
| 235 | + foreach ($uids as $uid) { |
|
| 236 | + $cleanUid = trim(substr($uid, 1), '"'); |
|
| 237 | + if (strpos($cleanUid, 'guest/') === 0) { |
|
| 238 | + $result[] = ['type' => 'guest', 'id' => $cleanUid]; |
|
| 239 | + } else { |
|
| 240 | + $result[] = ['type' => 'user', 'id' => $cleanUid]; |
|
| 241 | + } |
|
| 242 | + } |
|
| 243 | + return $result; |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + /** |
|
| 247 | + * returns the verb of the comment |
|
| 248 | + * |
|
| 249 | + * @return string |
|
| 250 | + * @since 9.0.0 |
|
| 251 | + */ |
|
| 252 | + public function getVerb() { |
|
| 253 | + return $this->data['verb']; |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + /** |
|
| 257 | + * sets the verb of the comment, e.g. 'comment' or 'like' |
|
| 258 | + * |
|
| 259 | + * @param string $verb |
|
| 260 | + * @return IComment |
|
| 261 | + * @since 9.0.0 |
|
| 262 | + */ |
|
| 263 | + public function setVerb($verb) { |
|
| 264 | + if(!is_string($verb) || !trim($verb)) { |
|
| 265 | + throw new \InvalidArgumentException('Non-empty String expected.'); |
|
| 266 | + } |
|
| 267 | + $this->data['verb'] = trim($verb); |
|
| 268 | + return $this; |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + /** |
|
| 272 | + * returns the actor type |
|
| 273 | + * |
|
| 274 | + * @return string |
|
| 275 | + * @since 9.0.0 |
|
| 276 | + */ |
|
| 277 | + public function getActorType() { |
|
| 278 | + return $this->data['actorType']; |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + /** |
|
| 282 | + * returns the actor ID |
|
| 283 | + * |
|
| 284 | + * @return string |
|
| 285 | + * @since 9.0.0 |
|
| 286 | + */ |
|
| 287 | + public function getActorId() { |
|
| 288 | + return $this->data['actorId']; |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + /** |
|
| 292 | + * sets (overwrites) the actor type and id |
|
| 293 | + * |
|
| 294 | + * @param string $actorType e.g. 'users' |
|
| 295 | + * @param string $actorId e.g. 'zombie234' |
|
| 296 | + * @return IComment |
|
| 297 | + * @since 9.0.0 |
|
| 298 | + */ |
|
| 299 | + public function setActor($actorType, $actorId) { |
|
| 300 | + if( |
|
| 301 | + !is_string($actorType) || !trim($actorType) |
|
| 302 | + || !is_string($actorId) || !trim($actorId) |
|
| 303 | + ) { |
|
| 304 | + throw new \InvalidArgumentException('String expected.'); |
|
| 305 | + } |
|
| 306 | + $this->data['actorType'] = trim($actorType); |
|
| 307 | + $this->data['actorId'] = trim($actorId); |
|
| 308 | + return $this; |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + /** |
|
| 312 | + * returns the creation date of the comment. |
|
| 313 | + * |
|
| 314 | + * If not explicitly set, it shall default to the time of initialization. |
|
| 315 | + * |
|
| 316 | + * @return \DateTime |
|
| 317 | + * @since 9.0.0 |
|
| 318 | + */ |
|
| 319 | + public function getCreationDateTime() { |
|
| 320 | + return $this->data['creationDT']; |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + /** |
|
| 324 | + * sets the creation date of the comment and returns itself |
|
| 325 | + * |
|
| 326 | + * @param \DateTime $timestamp |
|
| 327 | + * @return IComment |
|
| 328 | + * @since 9.0.0 |
|
| 329 | + */ |
|
| 330 | + public function setCreationDateTime(\DateTime $timestamp) { |
|
| 331 | + $this->data['creationDT'] = $timestamp; |
|
| 332 | + return $this; |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + /** |
|
| 336 | + * returns the DateTime of the most recent child, if set, otherwise null |
|
| 337 | + * |
|
| 338 | + * @return \DateTime|null |
|
| 339 | + * @since 9.0.0 |
|
| 340 | + */ |
|
| 341 | + public function getLatestChildDateTime() { |
|
| 342 | + return $this->data['latestChildDT']; |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + /** |
|
| 346 | + * sets the date of the most recent child |
|
| 347 | + * |
|
| 348 | + * @param \DateTime $dateTime |
|
| 349 | + * @return IComment |
|
| 350 | + * @since 9.0.0 |
|
| 351 | + */ |
|
| 352 | + public function setLatestChildDateTime(\DateTime $dateTime = null) { |
|
| 353 | + $this->data['latestChildDT'] = $dateTime; |
|
| 354 | + return $this; |
|
| 355 | + } |
|
| 356 | + |
|
| 357 | + /** |
|
| 358 | + * returns the object type the comment is attached to |
|
| 359 | + * |
|
| 360 | + * @return string |
|
| 361 | + * @since 9.0.0 |
|
| 362 | + */ |
|
| 363 | + public function getObjectType() { |
|
| 364 | + return $this->data['objectType']; |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + /** |
|
| 368 | + * returns the object id the comment is attached to |
|
| 369 | + * |
|
| 370 | + * @return string |
|
| 371 | + * @since 9.0.0 |
|
| 372 | + */ |
|
| 373 | + public function getObjectId() { |
|
| 374 | + return $this->data['objectId']; |
|
| 375 | + } |
|
| 376 | + |
|
| 377 | + /** |
|
| 378 | + * sets (overwrites) the object of the comment |
|
| 379 | + * |
|
| 380 | + * @param string $objectType e.g. 'files' |
|
| 381 | + * @param string $objectId e.g. '16435' |
|
| 382 | + * @return IComment |
|
| 383 | + * @since 9.0.0 |
|
| 384 | + */ |
|
| 385 | + public function setObject($objectType, $objectId) { |
|
| 386 | + if( |
|
| 387 | + !is_string($objectType) || !trim($objectType) |
|
| 388 | + || !is_string($objectId) || !trim($objectId) |
|
| 389 | + ) { |
|
| 390 | + throw new \InvalidArgumentException('String expected.'); |
|
| 391 | + } |
|
| 392 | + $this->data['objectType'] = trim($objectType); |
|
| 393 | + $this->data['objectId'] = trim($objectId); |
|
| 394 | + return $this; |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + /** |
|
| 398 | + * sets the comment data based on an array with keys as taken from the |
|
| 399 | + * database. |
|
| 400 | + * |
|
| 401 | + * @param array $data |
|
| 402 | + * @return IComment |
|
| 403 | + */ |
|
| 404 | + protected function fromArray($data) { |
|
| 405 | + foreach(array_keys($data) as $key) { |
|
| 406 | + // translate DB keys to internal setter names |
|
| 407 | + $setter = 'set' . implode('', array_map('ucfirst', explode('_', $key))); |
|
| 408 | + $setter = str_replace('Timestamp', 'DateTime', $setter); |
|
| 409 | + |
|
| 410 | + if(method_exists($this, $setter)) { |
|
| 411 | + $this->$setter($data[$key]); |
|
| 412 | + } |
|
| 413 | + } |
|
| 414 | + |
|
| 415 | + foreach(['actor', 'object'] as $role) { |
|
| 416 | + if(isset($data[$role . '_type']) && isset($data[$role . '_id'])) { |
|
| 417 | + $setter = 'set' . ucfirst($role); |
|
| 418 | + $this->$setter($data[$role . '_type'], $data[$role . '_id']); |
|
| 419 | + } |
|
| 420 | + } |
|
| 421 | + |
|
| 422 | + return $this; |
|
| 423 | + } |
|
| 424 | 424 | } |
@@ -52,7 +52,7 @@ discard block |
||
| 52 | 52 | * the comments database scheme |
| 53 | 53 | */ |
| 54 | 54 | public function __construct(array $data = null) { |
| 55 | - if(is_array($data)) { |
|
| 55 | + if (is_array($data)) { |
|
| 56 | 56 | $this->fromArray($data); |
| 57 | 57 | } |
| 58 | 58 | } |
@@ -85,12 +85,12 @@ discard block |
||
| 85 | 85 | * @since 9.0.0 |
| 86 | 86 | */ |
| 87 | 87 | public function setId($id) { |
| 88 | - if(!is_string($id)) { |
|
| 88 | + if (!is_string($id)) { |
|
| 89 | 89 | throw new \InvalidArgumentException('String expected.'); |
| 90 | 90 | } |
| 91 | 91 | |
| 92 | 92 | $id = trim($id); |
| 93 | - if($this->data['id'] === '' || ($this->data['id'] !== '' && $id === '')) { |
|
| 93 | + if ($this->data['id'] === '' || ($this->data['id'] !== '' && $id === '')) { |
|
| 94 | 94 | $this->data['id'] = $id; |
| 95 | 95 | return $this; |
| 96 | 96 | } |
@@ -116,7 +116,7 @@ discard block |
||
| 116 | 116 | * @since 9.0.0 |
| 117 | 117 | */ |
| 118 | 118 | public function setParentId($parentId) { |
| 119 | - if(!is_string($parentId)) { |
|
| 119 | + if (!is_string($parentId)) { |
|
| 120 | 120 | throw new \InvalidArgumentException('String expected.'); |
| 121 | 121 | } |
| 122 | 122 | $this->data['parentId'] = trim($parentId); |
@@ -142,7 +142,7 @@ discard block |
||
| 142 | 142 | * @since 9.0.0 |
| 143 | 143 | */ |
| 144 | 144 | public function setTopmostParentId($id) { |
| 145 | - if(!is_string($id)) { |
|
| 145 | + if (!is_string($id)) { |
|
| 146 | 146 | throw new \InvalidArgumentException('String expected.'); |
| 147 | 147 | } |
| 148 | 148 | $this->data['topmostParentId'] = trim($id); |
@@ -167,7 +167,7 @@ discard block |
||
| 167 | 167 | * @since 9.0.0 |
| 168 | 168 | */ |
| 169 | 169 | public function setChildrenCount($count) { |
| 170 | - if(!is_int($count)) { |
|
| 170 | + if (!is_int($count)) { |
|
| 171 | 171 | throw new \InvalidArgumentException('Integer expected.'); |
| 172 | 172 | } |
| 173 | 173 | $this->data['childrenCount'] = $count; |
@@ -194,12 +194,12 @@ discard block |
||
| 194 | 194 | * @since 9.0.0 |
| 195 | 195 | */ |
| 196 | 196 | public function setMessage($message, $maxLength = self::MAX_MESSAGE_LENGTH) { |
| 197 | - if(!is_string($message)) { |
|
| 197 | + if (!is_string($message)) { |
|
| 198 | 198 | throw new \InvalidArgumentException('String expected.'); |
| 199 | 199 | } |
| 200 | 200 | $message = trim($message); |
| 201 | 201 | if ($maxLength && mb_strlen($message, 'UTF-8') > $maxLength) { |
| 202 | - throw new MessageTooLongException('Comment message must not exceed ' . $maxLength. ' characters'); |
|
| 202 | + throw new MessageTooLongException('Comment message must not exceed '.$maxLength.' characters'); |
|
| 203 | 203 | } |
| 204 | 204 | $this->data['message'] = $message; |
| 205 | 205 | return $this; |
@@ -227,7 +227,7 @@ discard block |
||
| 227 | 227 | */ |
| 228 | 228 | public function getMentions() { |
| 229 | 229 | $ok = preg_match_all("/\B(?<![^a-z0-9_\-@\.\'\s])@(\"guest\/[a-f0-9]+\"|\"[a-z0-9_\-@\.\' ]+\"|[a-z0-9_\-@\.\']+)/i", $this->getMessage(), $mentions); |
| 230 | - if(!$ok || !isset($mentions[0]) || !is_array($mentions[0])) { |
|
| 230 | + if (!$ok || !isset($mentions[0]) || !is_array($mentions[0])) { |
|
| 231 | 231 | return []; |
| 232 | 232 | } |
| 233 | 233 | $uids = array_unique($mentions[0]); |
@@ -261,7 +261,7 @@ discard block |
||
| 261 | 261 | * @since 9.0.0 |
| 262 | 262 | */ |
| 263 | 263 | public function setVerb($verb) { |
| 264 | - if(!is_string($verb) || !trim($verb)) { |
|
| 264 | + if (!is_string($verb) || !trim($verb)) { |
|
| 265 | 265 | throw new \InvalidArgumentException('Non-empty String expected.'); |
| 266 | 266 | } |
| 267 | 267 | $this->data['verb'] = trim($verb); |
@@ -297,9 +297,9 @@ discard block |
||
| 297 | 297 | * @since 9.0.0 |
| 298 | 298 | */ |
| 299 | 299 | public function setActor($actorType, $actorId) { |
| 300 | - if( |
|
| 300 | + if ( |
|
| 301 | 301 | !is_string($actorType) || !trim($actorType) |
| 302 | - || !is_string($actorId) || !trim($actorId) |
|
| 302 | + || !is_string($actorId) || !trim($actorId) |
|
| 303 | 303 | ) { |
| 304 | 304 | throw new \InvalidArgumentException('String expected.'); |
| 305 | 305 | } |
@@ -383,9 +383,9 @@ discard block |
||
| 383 | 383 | * @since 9.0.0 |
| 384 | 384 | */ |
| 385 | 385 | public function setObject($objectType, $objectId) { |
| 386 | - if( |
|
| 386 | + if ( |
|
| 387 | 387 | !is_string($objectType) || !trim($objectType) |
| 388 | - || !is_string($objectId) || !trim($objectId) |
|
| 388 | + || !is_string($objectId) || !trim($objectId) |
|
| 389 | 389 | ) { |
| 390 | 390 | throw new \InvalidArgumentException('String expected.'); |
| 391 | 391 | } |
@@ -402,20 +402,20 @@ discard block |
||
| 402 | 402 | * @return IComment |
| 403 | 403 | */ |
| 404 | 404 | protected function fromArray($data) { |
| 405 | - foreach(array_keys($data) as $key) { |
|
| 405 | + foreach (array_keys($data) as $key) { |
|
| 406 | 406 | // translate DB keys to internal setter names |
| 407 | - $setter = 'set' . implode('', array_map('ucfirst', explode('_', $key))); |
|
| 407 | + $setter = 'set'.implode('', array_map('ucfirst', explode('_', $key))); |
|
| 408 | 408 | $setter = str_replace('Timestamp', 'DateTime', $setter); |
| 409 | 409 | |
| 410 | - if(method_exists($this, $setter)) { |
|
| 410 | + if (method_exists($this, $setter)) { |
|
| 411 | 411 | $this->$setter($data[$key]); |
| 412 | 412 | } |
| 413 | 413 | } |
| 414 | 414 | |
| 415 | - foreach(['actor', 'object'] as $role) { |
|
| 416 | - if(isset($data[$role . '_type']) && isset($data[$role . '_id'])) { |
|
| 417 | - $setter = 'set' . ucfirst($role); |
|
| 418 | - $this->$setter($data[$role . '_type'], $data[$role . '_id']); |
|
| 415 | + foreach (['actor', 'object'] as $role) { |
|
| 416 | + if (isset($data[$role.'_type']) && isset($data[$role.'_id'])) { |
|
| 417 | + $setter = 'set'.ucfirst($role); |
|
| 418 | + $this->$setter($data[$role.'_type'], $data[$role.'_id']); |
|
| 419 | 419 | } |
| 420 | 420 | } |
| 421 | 421 | |