@@ -34,547 +34,547 @@ |
||
| 34 | 34 | |
| 35 | 35 | class Notification implements INotification { |
| 36 | 36 | |
| 37 | - /** @var IValidator */ |
|
| 38 | - protected $richValidator; |
|
| 39 | - |
|
| 40 | - /** @var string */ |
|
| 41 | - protected $app; |
|
| 42 | - |
|
| 43 | - /** @var string */ |
|
| 44 | - protected $user; |
|
| 45 | - |
|
| 46 | - /** @var \DateTime */ |
|
| 47 | - protected $dateTime; |
|
| 48 | - |
|
| 49 | - /** @var string */ |
|
| 50 | - protected $objectType; |
|
| 51 | - |
|
| 52 | - /** @var string */ |
|
| 53 | - protected $objectId; |
|
| 54 | - |
|
| 55 | - /** @var string */ |
|
| 56 | - protected $subject; |
|
| 57 | - |
|
| 58 | - /** @var array */ |
|
| 59 | - protected $subjectParameters; |
|
| 60 | - |
|
| 61 | - /** @var string */ |
|
| 62 | - protected $subjectParsed; |
|
| 63 | - |
|
| 64 | - /** @var string */ |
|
| 65 | - protected $subjectRich; |
|
| 66 | - |
|
| 67 | - /** @var array */ |
|
| 68 | - protected $subjectRichParameters; |
|
| 69 | - |
|
| 70 | - /** @var string */ |
|
| 71 | - protected $message; |
|
| 72 | - |
|
| 73 | - /** @var array */ |
|
| 74 | - protected $messageParameters; |
|
| 75 | - |
|
| 76 | - /** @var string */ |
|
| 77 | - protected $messageParsed; |
|
| 78 | - |
|
| 79 | - /** @var string */ |
|
| 80 | - protected $messageRich; |
|
| 81 | - |
|
| 82 | - /** @var array */ |
|
| 83 | - protected $messageRichParameters; |
|
| 84 | - |
|
| 85 | - /** @var string */ |
|
| 86 | - protected $link; |
|
| 87 | - |
|
| 88 | - /** @var string */ |
|
| 89 | - protected $icon; |
|
| 90 | - |
|
| 91 | - /** @var array */ |
|
| 92 | - protected $actions; |
|
| 93 | - |
|
| 94 | - /** @var array */ |
|
| 95 | - protected $actionsParsed; |
|
| 96 | - |
|
| 97 | - /** @var bool */ |
|
| 98 | - protected $hasPrimaryAction; |
|
| 99 | - |
|
| 100 | - /** @var bool */ |
|
| 101 | - protected $hasPrimaryParsedAction; |
|
| 102 | - |
|
| 103 | - public function __construct(IValidator $richValidator) { |
|
| 104 | - $this->richValidator = $richValidator; |
|
| 105 | - $this->app = ''; |
|
| 106 | - $this->user = ''; |
|
| 107 | - $this->dateTime = new \DateTime(); |
|
| 108 | - $this->dateTime->setTimestamp(0); |
|
| 109 | - $this->objectType = ''; |
|
| 110 | - $this->objectId = ''; |
|
| 111 | - $this->subject = ''; |
|
| 112 | - $this->subjectParameters = []; |
|
| 113 | - $this->subjectParsed = ''; |
|
| 114 | - $this->subjectRich = ''; |
|
| 115 | - $this->subjectRichParameters = []; |
|
| 116 | - $this->message = ''; |
|
| 117 | - $this->messageParameters = []; |
|
| 118 | - $this->messageParsed = ''; |
|
| 119 | - $this->messageRich = ''; |
|
| 120 | - $this->messageRichParameters = []; |
|
| 121 | - $this->link = ''; |
|
| 122 | - $this->icon = ''; |
|
| 123 | - $this->actions = []; |
|
| 124 | - $this->actionsParsed = []; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * @param string $app |
|
| 129 | - * @return $this |
|
| 130 | - * @throws \InvalidArgumentException if the app id is invalid |
|
| 131 | - * @since 8.2.0 |
|
| 132 | - */ |
|
| 133 | - public function setApp(string $app): INotification { |
|
| 134 | - if ($app === '' || isset($app[32])) { |
|
| 135 | - throw new \InvalidArgumentException('The given app name is invalid'); |
|
| 136 | - } |
|
| 137 | - $this->app = $app; |
|
| 138 | - return $this; |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * @return string |
|
| 143 | - * @since 8.2.0 |
|
| 144 | - */ |
|
| 145 | - public function getApp(): string { |
|
| 146 | - return $this->app; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * @param string $user |
|
| 151 | - * @return $this |
|
| 152 | - * @throws \InvalidArgumentException if the user id is invalid |
|
| 153 | - * @since 8.2.0 |
|
| 154 | - */ |
|
| 155 | - public function setUser(string $user): INotification { |
|
| 156 | - if ($user === '' || isset($user[64])) { |
|
| 157 | - throw new \InvalidArgumentException('The given user id is invalid'); |
|
| 158 | - } |
|
| 159 | - $this->user = $user; |
|
| 160 | - return $this; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * @return string |
|
| 165 | - * @since 8.2.0 |
|
| 166 | - */ |
|
| 167 | - public function getUser(): string { |
|
| 168 | - return $this->user; |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * @param \DateTime $dateTime |
|
| 173 | - * @return $this |
|
| 174 | - * @throws \InvalidArgumentException if the $dateTime is invalid |
|
| 175 | - * @since 9.0.0 |
|
| 176 | - */ |
|
| 177 | - public function setDateTime(\DateTime $dateTime): INotification { |
|
| 178 | - if ($dateTime->getTimestamp() === 0) { |
|
| 179 | - throw new \InvalidArgumentException('The given date time is invalid'); |
|
| 180 | - } |
|
| 181 | - $this->dateTime = $dateTime; |
|
| 182 | - return $this; |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * @return \DateTime |
|
| 187 | - * @since 9.0.0 |
|
| 188 | - */ |
|
| 189 | - public function getDateTime(): \DateTime { |
|
| 190 | - return $this->dateTime; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * @param string $type |
|
| 195 | - * @param string $id |
|
| 196 | - * @return $this |
|
| 197 | - * @throws \InvalidArgumentException if the object type or id is invalid |
|
| 198 | - * @since 8.2.0 - 9.0.0: Type of $id changed to string |
|
| 199 | - */ |
|
| 200 | - public function setObject(string $type, string $id): INotification { |
|
| 201 | - if ($type === '' || isset($type[64])) { |
|
| 202 | - throw new \InvalidArgumentException('The given object type is invalid'); |
|
| 203 | - } |
|
| 204 | - $this->objectType = $type; |
|
| 205 | - |
|
| 206 | - if ($id === '' || isset($id[64])) { |
|
| 207 | - throw new \InvalidArgumentException('The given object id is invalid'); |
|
| 208 | - } |
|
| 209 | - $this->objectId = $id; |
|
| 210 | - return $this; |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - /** |
|
| 214 | - * @return string |
|
| 215 | - * @since 8.2.0 |
|
| 216 | - */ |
|
| 217 | - public function getObjectType(): string { |
|
| 218 | - return $this->objectType; |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - /** |
|
| 222 | - * @return string |
|
| 223 | - * @since 8.2.0 - 9.0.0: Return type changed to string |
|
| 224 | - */ |
|
| 225 | - public function getObjectId(): string { |
|
| 226 | - return $this->objectId; |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - /** |
|
| 230 | - * @param string $subject |
|
| 231 | - * @param array $parameters |
|
| 232 | - * @return $this |
|
| 233 | - * @throws \InvalidArgumentException if the subject or parameters are invalid |
|
| 234 | - * @since 8.2.0 |
|
| 235 | - */ |
|
| 236 | - public function setSubject(string $subject, array $parameters = []): INotification { |
|
| 237 | - if ($subject === '' || isset($subject[64])) { |
|
| 238 | - throw new \InvalidArgumentException('The given subject is invalid'); |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - $this->subject = $subject; |
|
| 242 | - $this->subjectParameters = $parameters; |
|
| 243 | - |
|
| 244 | - return $this; |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - /** |
|
| 248 | - * @return string |
|
| 249 | - * @since 8.2.0 |
|
| 250 | - */ |
|
| 251 | - public function getSubject(): string { |
|
| 252 | - return $this->subject; |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - /** |
|
| 256 | - * @return array |
|
| 257 | - * @since 8.2.0 |
|
| 258 | - */ |
|
| 259 | - public function getSubjectParameters(): array { |
|
| 260 | - return $this->subjectParameters; |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - /** |
|
| 264 | - * @param string $subject |
|
| 265 | - * @return $this |
|
| 266 | - * @throws \InvalidArgumentException if the subject is invalid |
|
| 267 | - * @since 8.2.0 |
|
| 268 | - */ |
|
| 269 | - public function setParsedSubject(string $subject): INotification { |
|
| 270 | - if ($subject === '') { |
|
| 271 | - throw new \InvalidArgumentException('The given parsed subject is invalid'); |
|
| 272 | - } |
|
| 273 | - $this->subjectParsed = $subject; |
|
| 274 | - return $this; |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - /** |
|
| 278 | - * @return string |
|
| 279 | - * @since 8.2.0 |
|
| 280 | - */ |
|
| 281 | - public function getParsedSubject(): string { |
|
| 282 | - return $this->subjectParsed; |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - /** |
|
| 286 | - * @param string $subject |
|
| 287 | - * @param array $parameters |
|
| 288 | - * @return $this |
|
| 289 | - * @throws \InvalidArgumentException if the subject or parameters are invalid |
|
| 290 | - * @since 11.0.0 |
|
| 291 | - */ |
|
| 292 | - public function setRichSubject(string $subject, array $parameters = []): INotification { |
|
| 293 | - if ($subject === '') { |
|
| 294 | - throw new \InvalidArgumentException('The given parsed subject is invalid'); |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - $this->subjectRich = $subject; |
|
| 298 | - $this->subjectRichParameters = $parameters; |
|
| 299 | - |
|
| 300 | - return $this; |
|
| 301 | - } |
|
| 302 | - |
|
| 303 | - /** |
|
| 304 | - * @return string |
|
| 305 | - * @since 11.0.0 |
|
| 306 | - */ |
|
| 307 | - public function getRichSubject(): string { |
|
| 308 | - return $this->subjectRich; |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - /** |
|
| 312 | - * @return array[] |
|
| 313 | - * @since 11.0.0 |
|
| 314 | - */ |
|
| 315 | - public function getRichSubjectParameters(): array { |
|
| 316 | - return $this->subjectRichParameters; |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - /** |
|
| 320 | - * @param string $message |
|
| 321 | - * @param array $parameters |
|
| 322 | - * @return $this |
|
| 323 | - * @throws \InvalidArgumentException if the message or parameters are invalid |
|
| 324 | - * @since 8.2.0 |
|
| 325 | - */ |
|
| 326 | - public function setMessage(string $message, array $parameters = []): INotification { |
|
| 327 | - if ($message === '' || isset($message[64])) { |
|
| 328 | - throw new \InvalidArgumentException('The given message is invalid'); |
|
| 329 | - } |
|
| 330 | - |
|
| 331 | - $this->message = $message; |
|
| 332 | - $this->messageParameters = $parameters; |
|
| 333 | - |
|
| 334 | - return $this; |
|
| 335 | - } |
|
| 336 | - |
|
| 337 | - /** |
|
| 338 | - * @return string |
|
| 339 | - * @since 8.2.0 |
|
| 340 | - */ |
|
| 341 | - public function getMessage(): string { |
|
| 342 | - return $this->message; |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - /** |
|
| 346 | - * @return array |
|
| 347 | - * @since 8.2.0 |
|
| 348 | - */ |
|
| 349 | - public function getMessageParameters(): array { |
|
| 350 | - return $this->messageParameters; |
|
| 351 | - } |
|
| 352 | - |
|
| 353 | - /** |
|
| 354 | - * @param string $message |
|
| 355 | - * @return $this |
|
| 356 | - * @throws \InvalidArgumentException if the message is invalid |
|
| 357 | - * @since 8.2.0 |
|
| 358 | - */ |
|
| 359 | - public function setParsedMessage(string $message): INotification { |
|
| 360 | - if ($message === '') { |
|
| 361 | - throw new \InvalidArgumentException('The given parsed message is invalid'); |
|
| 362 | - } |
|
| 363 | - $this->messageParsed = $message; |
|
| 364 | - return $this; |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - /** |
|
| 368 | - * @return string |
|
| 369 | - * @since 8.2.0 |
|
| 370 | - */ |
|
| 371 | - public function getParsedMessage(): string { |
|
| 372 | - return $this->messageParsed; |
|
| 373 | - } |
|
| 374 | - |
|
| 375 | - /** |
|
| 376 | - * @param string $message |
|
| 377 | - * @param array $parameters |
|
| 378 | - * @return $this |
|
| 379 | - * @throws \InvalidArgumentException if the message or parameters are invalid |
|
| 380 | - * @since 11.0.0 |
|
| 381 | - */ |
|
| 382 | - public function setRichMessage(string $message, array $parameters = []): INotification { |
|
| 383 | - if ($message === '') { |
|
| 384 | - throw new \InvalidArgumentException('The given parsed message is invalid'); |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - $this->messageRich = $message; |
|
| 388 | - $this->messageRichParameters = $parameters; |
|
| 389 | - |
|
| 390 | - return $this; |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - /** |
|
| 394 | - * @return string |
|
| 395 | - * @since 11.0.0 |
|
| 396 | - */ |
|
| 397 | - public function getRichMessage(): string { |
|
| 398 | - return $this->messageRich; |
|
| 399 | - } |
|
| 400 | - |
|
| 401 | - /** |
|
| 402 | - * @return array[] |
|
| 403 | - * @since 11.0.0 |
|
| 404 | - */ |
|
| 405 | - public function getRichMessageParameters(): array { |
|
| 406 | - return $this->messageRichParameters; |
|
| 407 | - } |
|
| 408 | - |
|
| 409 | - /** |
|
| 410 | - * @param string $link |
|
| 411 | - * @return $this |
|
| 412 | - * @throws \InvalidArgumentException if the link is invalid |
|
| 413 | - * @since 8.2.0 |
|
| 414 | - */ |
|
| 415 | - public function setLink(string $link): INotification { |
|
| 416 | - if ($link === '' || isset($link[4000])) { |
|
| 417 | - throw new \InvalidArgumentException('The given link is invalid'); |
|
| 418 | - } |
|
| 419 | - $this->link = $link; |
|
| 420 | - return $this; |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - /** |
|
| 424 | - * @return string |
|
| 425 | - * @since 8.2.0 |
|
| 426 | - */ |
|
| 427 | - public function getLink(): string { |
|
| 428 | - return $this->link; |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - /** |
|
| 432 | - * @param string $icon |
|
| 433 | - * @return $this |
|
| 434 | - * @throws \InvalidArgumentException if the icon is invalid |
|
| 435 | - * @since 11.0.0 |
|
| 436 | - */ |
|
| 437 | - public function setIcon(string $icon): INotification { |
|
| 438 | - if ($icon === '' || isset($icon[4000])) { |
|
| 439 | - throw new \InvalidArgumentException('The given icon is invalid'); |
|
| 440 | - } |
|
| 441 | - $this->icon = $icon; |
|
| 442 | - return $this; |
|
| 443 | - } |
|
| 444 | - |
|
| 445 | - /** |
|
| 446 | - * @return string |
|
| 447 | - * @since 11.0.0 |
|
| 448 | - */ |
|
| 449 | - public function getIcon(): string { |
|
| 450 | - return $this->icon; |
|
| 451 | - } |
|
| 452 | - |
|
| 453 | - /** |
|
| 454 | - * @return IAction |
|
| 455 | - * @since 8.2.0 |
|
| 456 | - */ |
|
| 457 | - public function createAction(): IAction { |
|
| 458 | - return new Action(); |
|
| 459 | - } |
|
| 460 | - |
|
| 461 | - /** |
|
| 462 | - * @param IAction $action |
|
| 463 | - * @return $this |
|
| 464 | - * @throws \InvalidArgumentException if the action is invalid |
|
| 465 | - * @since 8.2.0 |
|
| 466 | - */ |
|
| 467 | - public function addAction(IAction $action): INotification { |
|
| 468 | - if (!$action->isValid()) { |
|
| 469 | - throw new \InvalidArgumentException('The given action is invalid'); |
|
| 470 | - } |
|
| 471 | - |
|
| 472 | - if ($action->isPrimary()) { |
|
| 473 | - if ($this->hasPrimaryAction) { |
|
| 474 | - throw new \InvalidArgumentException('The notification already has a primary action'); |
|
| 475 | - } |
|
| 476 | - |
|
| 477 | - $this->hasPrimaryAction = true; |
|
| 478 | - } |
|
| 479 | - |
|
| 480 | - $this->actions[] = $action; |
|
| 481 | - return $this; |
|
| 482 | - } |
|
| 483 | - |
|
| 484 | - /** |
|
| 485 | - * @return IAction[] |
|
| 486 | - * @since 8.2.0 |
|
| 487 | - */ |
|
| 488 | - public function getActions(): array { |
|
| 489 | - return $this->actions; |
|
| 490 | - } |
|
| 491 | - |
|
| 492 | - /** |
|
| 493 | - * @param IAction $action |
|
| 494 | - * @return $this |
|
| 495 | - * @throws \InvalidArgumentException if the action is invalid |
|
| 496 | - * @since 8.2.0 |
|
| 497 | - */ |
|
| 498 | - public function addParsedAction(IAction $action): INotification { |
|
| 499 | - if (!$action->isValidParsed()) { |
|
| 500 | - throw new \InvalidArgumentException('The given parsed action is invalid'); |
|
| 501 | - } |
|
| 502 | - |
|
| 503 | - if ($action->isPrimary()) { |
|
| 504 | - if ($this->hasPrimaryParsedAction) { |
|
| 505 | - throw new \InvalidArgumentException('The notification already has a primary action'); |
|
| 506 | - } |
|
| 507 | - |
|
| 508 | - $this->hasPrimaryParsedAction = true; |
|
| 509 | - |
|
| 510 | - // Make sure the primary action is always the first one |
|
| 511 | - array_unshift($this->actionsParsed, $action); |
|
| 512 | - } else { |
|
| 513 | - $this->actionsParsed[] = $action; |
|
| 514 | - } |
|
| 515 | - |
|
| 516 | - return $this; |
|
| 517 | - } |
|
| 518 | - |
|
| 519 | - /** |
|
| 520 | - * @return IAction[] |
|
| 521 | - * @since 8.2.0 |
|
| 522 | - */ |
|
| 523 | - public function getParsedActions(): array { |
|
| 524 | - return $this->actionsParsed; |
|
| 525 | - } |
|
| 526 | - |
|
| 527 | - /** |
|
| 528 | - * @return bool |
|
| 529 | - * @since 8.2.0 |
|
| 530 | - */ |
|
| 531 | - public function isValid(): bool { |
|
| 532 | - return |
|
| 533 | - $this->isValidCommon() |
|
| 534 | - && |
|
| 535 | - $this->getSubject() !== '' |
|
| 536 | - ; |
|
| 537 | - } |
|
| 538 | - |
|
| 539 | - /** |
|
| 540 | - * @return bool |
|
| 541 | - * @since 8.2.0 |
|
| 542 | - */ |
|
| 543 | - public function isValidParsed(): bool { |
|
| 544 | - if ($this->getRichSubject() !== '' || !empty($this->getRichSubjectParameters())) { |
|
| 545 | - try { |
|
| 546 | - $this->richValidator->validate($this->getRichSubject(), $this->getRichSubjectParameters()); |
|
| 547 | - } catch (InvalidObjectExeption $e) { |
|
| 548 | - return false; |
|
| 549 | - } |
|
| 550 | - } |
|
| 551 | - |
|
| 552 | - if ($this->getRichMessage() !== '' || !empty($this->getRichMessageParameters())) { |
|
| 553 | - try { |
|
| 554 | - $this->richValidator->validate($this->getRichMessage(), $this->getRichMessageParameters()); |
|
| 555 | - } catch (InvalidObjectExeption $e) { |
|
| 556 | - return false; |
|
| 557 | - } |
|
| 558 | - } |
|
| 559 | - |
|
| 560 | - return |
|
| 561 | - $this->isValidCommon() |
|
| 562 | - && |
|
| 563 | - $this->getParsedSubject() !== '' |
|
| 564 | - ; |
|
| 565 | - } |
|
| 566 | - |
|
| 567 | - protected function isValidCommon(): bool { |
|
| 568 | - return |
|
| 569 | - $this->getApp() !== '' |
|
| 570 | - && |
|
| 571 | - $this->getUser() !== '' |
|
| 572 | - && |
|
| 573 | - $this->getDateTime()->getTimestamp() !== 0 |
|
| 574 | - && |
|
| 575 | - $this->getObjectType() !== '' |
|
| 576 | - && |
|
| 577 | - $this->getObjectId() !== '' |
|
| 578 | - ; |
|
| 579 | - } |
|
| 37 | + /** @var IValidator */ |
|
| 38 | + protected $richValidator; |
|
| 39 | + |
|
| 40 | + /** @var string */ |
|
| 41 | + protected $app; |
|
| 42 | + |
|
| 43 | + /** @var string */ |
|
| 44 | + protected $user; |
|
| 45 | + |
|
| 46 | + /** @var \DateTime */ |
|
| 47 | + protected $dateTime; |
|
| 48 | + |
|
| 49 | + /** @var string */ |
|
| 50 | + protected $objectType; |
|
| 51 | + |
|
| 52 | + /** @var string */ |
|
| 53 | + protected $objectId; |
|
| 54 | + |
|
| 55 | + /** @var string */ |
|
| 56 | + protected $subject; |
|
| 57 | + |
|
| 58 | + /** @var array */ |
|
| 59 | + protected $subjectParameters; |
|
| 60 | + |
|
| 61 | + /** @var string */ |
|
| 62 | + protected $subjectParsed; |
|
| 63 | + |
|
| 64 | + /** @var string */ |
|
| 65 | + protected $subjectRich; |
|
| 66 | + |
|
| 67 | + /** @var array */ |
|
| 68 | + protected $subjectRichParameters; |
|
| 69 | + |
|
| 70 | + /** @var string */ |
|
| 71 | + protected $message; |
|
| 72 | + |
|
| 73 | + /** @var array */ |
|
| 74 | + protected $messageParameters; |
|
| 75 | + |
|
| 76 | + /** @var string */ |
|
| 77 | + protected $messageParsed; |
|
| 78 | + |
|
| 79 | + /** @var string */ |
|
| 80 | + protected $messageRich; |
|
| 81 | + |
|
| 82 | + /** @var array */ |
|
| 83 | + protected $messageRichParameters; |
|
| 84 | + |
|
| 85 | + /** @var string */ |
|
| 86 | + protected $link; |
|
| 87 | + |
|
| 88 | + /** @var string */ |
|
| 89 | + protected $icon; |
|
| 90 | + |
|
| 91 | + /** @var array */ |
|
| 92 | + protected $actions; |
|
| 93 | + |
|
| 94 | + /** @var array */ |
|
| 95 | + protected $actionsParsed; |
|
| 96 | + |
|
| 97 | + /** @var bool */ |
|
| 98 | + protected $hasPrimaryAction; |
|
| 99 | + |
|
| 100 | + /** @var bool */ |
|
| 101 | + protected $hasPrimaryParsedAction; |
|
| 102 | + |
|
| 103 | + public function __construct(IValidator $richValidator) { |
|
| 104 | + $this->richValidator = $richValidator; |
|
| 105 | + $this->app = ''; |
|
| 106 | + $this->user = ''; |
|
| 107 | + $this->dateTime = new \DateTime(); |
|
| 108 | + $this->dateTime->setTimestamp(0); |
|
| 109 | + $this->objectType = ''; |
|
| 110 | + $this->objectId = ''; |
|
| 111 | + $this->subject = ''; |
|
| 112 | + $this->subjectParameters = []; |
|
| 113 | + $this->subjectParsed = ''; |
|
| 114 | + $this->subjectRich = ''; |
|
| 115 | + $this->subjectRichParameters = []; |
|
| 116 | + $this->message = ''; |
|
| 117 | + $this->messageParameters = []; |
|
| 118 | + $this->messageParsed = ''; |
|
| 119 | + $this->messageRich = ''; |
|
| 120 | + $this->messageRichParameters = []; |
|
| 121 | + $this->link = ''; |
|
| 122 | + $this->icon = ''; |
|
| 123 | + $this->actions = []; |
|
| 124 | + $this->actionsParsed = []; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * @param string $app |
|
| 129 | + * @return $this |
|
| 130 | + * @throws \InvalidArgumentException if the app id is invalid |
|
| 131 | + * @since 8.2.0 |
|
| 132 | + */ |
|
| 133 | + public function setApp(string $app): INotification { |
|
| 134 | + if ($app === '' || isset($app[32])) { |
|
| 135 | + throw new \InvalidArgumentException('The given app name is invalid'); |
|
| 136 | + } |
|
| 137 | + $this->app = $app; |
|
| 138 | + return $this; |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * @return string |
|
| 143 | + * @since 8.2.0 |
|
| 144 | + */ |
|
| 145 | + public function getApp(): string { |
|
| 146 | + return $this->app; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * @param string $user |
|
| 151 | + * @return $this |
|
| 152 | + * @throws \InvalidArgumentException if the user id is invalid |
|
| 153 | + * @since 8.2.0 |
|
| 154 | + */ |
|
| 155 | + public function setUser(string $user): INotification { |
|
| 156 | + if ($user === '' || isset($user[64])) { |
|
| 157 | + throw new \InvalidArgumentException('The given user id is invalid'); |
|
| 158 | + } |
|
| 159 | + $this->user = $user; |
|
| 160 | + return $this; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * @return string |
|
| 165 | + * @since 8.2.0 |
|
| 166 | + */ |
|
| 167 | + public function getUser(): string { |
|
| 168 | + return $this->user; |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * @param \DateTime $dateTime |
|
| 173 | + * @return $this |
|
| 174 | + * @throws \InvalidArgumentException if the $dateTime is invalid |
|
| 175 | + * @since 9.0.0 |
|
| 176 | + */ |
|
| 177 | + public function setDateTime(\DateTime $dateTime): INotification { |
|
| 178 | + if ($dateTime->getTimestamp() === 0) { |
|
| 179 | + throw new \InvalidArgumentException('The given date time is invalid'); |
|
| 180 | + } |
|
| 181 | + $this->dateTime = $dateTime; |
|
| 182 | + return $this; |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * @return \DateTime |
|
| 187 | + * @since 9.0.0 |
|
| 188 | + */ |
|
| 189 | + public function getDateTime(): \DateTime { |
|
| 190 | + return $this->dateTime; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * @param string $type |
|
| 195 | + * @param string $id |
|
| 196 | + * @return $this |
|
| 197 | + * @throws \InvalidArgumentException if the object type or id is invalid |
|
| 198 | + * @since 8.2.0 - 9.0.0: Type of $id changed to string |
|
| 199 | + */ |
|
| 200 | + public function setObject(string $type, string $id): INotification { |
|
| 201 | + if ($type === '' || isset($type[64])) { |
|
| 202 | + throw new \InvalidArgumentException('The given object type is invalid'); |
|
| 203 | + } |
|
| 204 | + $this->objectType = $type; |
|
| 205 | + |
|
| 206 | + if ($id === '' || isset($id[64])) { |
|
| 207 | + throw new \InvalidArgumentException('The given object id is invalid'); |
|
| 208 | + } |
|
| 209 | + $this->objectId = $id; |
|
| 210 | + return $this; |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + /** |
|
| 214 | + * @return string |
|
| 215 | + * @since 8.2.0 |
|
| 216 | + */ |
|
| 217 | + public function getObjectType(): string { |
|
| 218 | + return $this->objectType; |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + /** |
|
| 222 | + * @return string |
|
| 223 | + * @since 8.2.0 - 9.0.0: Return type changed to string |
|
| 224 | + */ |
|
| 225 | + public function getObjectId(): string { |
|
| 226 | + return $this->objectId; |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + /** |
|
| 230 | + * @param string $subject |
|
| 231 | + * @param array $parameters |
|
| 232 | + * @return $this |
|
| 233 | + * @throws \InvalidArgumentException if the subject or parameters are invalid |
|
| 234 | + * @since 8.2.0 |
|
| 235 | + */ |
|
| 236 | + public function setSubject(string $subject, array $parameters = []): INotification { |
|
| 237 | + if ($subject === '' || isset($subject[64])) { |
|
| 238 | + throw new \InvalidArgumentException('The given subject is invalid'); |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + $this->subject = $subject; |
|
| 242 | + $this->subjectParameters = $parameters; |
|
| 243 | + |
|
| 244 | + return $this; |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + /** |
|
| 248 | + * @return string |
|
| 249 | + * @since 8.2.0 |
|
| 250 | + */ |
|
| 251 | + public function getSubject(): string { |
|
| 252 | + return $this->subject; |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + /** |
|
| 256 | + * @return array |
|
| 257 | + * @since 8.2.0 |
|
| 258 | + */ |
|
| 259 | + public function getSubjectParameters(): array { |
|
| 260 | + return $this->subjectParameters; |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + /** |
|
| 264 | + * @param string $subject |
|
| 265 | + * @return $this |
|
| 266 | + * @throws \InvalidArgumentException if the subject is invalid |
|
| 267 | + * @since 8.2.0 |
|
| 268 | + */ |
|
| 269 | + public function setParsedSubject(string $subject): INotification { |
|
| 270 | + if ($subject === '') { |
|
| 271 | + throw new \InvalidArgumentException('The given parsed subject is invalid'); |
|
| 272 | + } |
|
| 273 | + $this->subjectParsed = $subject; |
|
| 274 | + return $this; |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + /** |
|
| 278 | + * @return string |
|
| 279 | + * @since 8.2.0 |
|
| 280 | + */ |
|
| 281 | + public function getParsedSubject(): string { |
|
| 282 | + return $this->subjectParsed; |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + /** |
|
| 286 | + * @param string $subject |
|
| 287 | + * @param array $parameters |
|
| 288 | + * @return $this |
|
| 289 | + * @throws \InvalidArgumentException if the subject or parameters are invalid |
|
| 290 | + * @since 11.0.0 |
|
| 291 | + */ |
|
| 292 | + public function setRichSubject(string $subject, array $parameters = []): INotification { |
|
| 293 | + if ($subject === '') { |
|
| 294 | + throw new \InvalidArgumentException('The given parsed subject is invalid'); |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + $this->subjectRich = $subject; |
|
| 298 | + $this->subjectRichParameters = $parameters; |
|
| 299 | + |
|
| 300 | + return $this; |
|
| 301 | + } |
|
| 302 | + |
|
| 303 | + /** |
|
| 304 | + * @return string |
|
| 305 | + * @since 11.0.0 |
|
| 306 | + */ |
|
| 307 | + public function getRichSubject(): string { |
|
| 308 | + return $this->subjectRich; |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + /** |
|
| 312 | + * @return array[] |
|
| 313 | + * @since 11.0.0 |
|
| 314 | + */ |
|
| 315 | + public function getRichSubjectParameters(): array { |
|
| 316 | + return $this->subjectRichParameters; |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + /** |
|
| 320 | + * @param string $message |
|
| 321 | + * @param array $parameters |
|
| 322 | + * @return $this |
|
| 323 | + * @throws \InvalidArgumentException if the message or parameters are invalid |
|
| 324 | + * @since 8.2.0 |
|
| 325 | + */ |
|
| 326 | + public function setMessage(string $message, array $parameters = []): INotification { |
|
| 327 | + if ($message === '' || isset($message[64])) { |
|
| 328 | + throw new \InvalidArgumentException('The given message is invalid'); |
|
| 329 | + } |
|
| 330 | + |
|
| 331 | + $this->message = $message; |
|
| 332 | + $this->messageParameters = $parameters; |
|
| 333 | + |
|
| 334 | + return $this; |
|
| 335 | + } |
|
| 336 | + |
|
| 337 | + /** |
|
| 338 | + * @return string |
|
| 339 | + * @since 8.2.0 |
|
| 340 | + */ |
|
| 341 | + public function getMessage(): string { |
|
| 342 | + return $this->message; |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + /** |
|
| 346 | + * @return array |
|
| 347 | + * @since 8.2.0 |
|
| 348 | + */ |
|
| 349 | + public function getMessageParameters(): array { |
|
| 350 | + return $this->messageParameters; |
|
| 351 | + } |
|
| 352 | + |
|
| 353 | + /** |
|
| 354 | + * @param string $message |
|
| 355 | + * @return $this |
|
| 356 | + * @throws \InvalidArgumentException if the message is invalid |
|
| 357 | + * @since 8.2.0 |
|
| 358 | + */ |
|
| 359 | + public function setParsedMessage(string $message): INotification { |
|
| 360 | + if ($message === '') { |
|
| 361 | + throw new \InvalidArgumentException('The given parsed message is invalid'); |
|
| 362 | + } |
|
| 363 | + $this->messageParsed = $message; |
|
| 364 | + return $this; |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + /** |
|
| 368 | + * @return string |
|
| 369 | + * @since 8.2.0 |
|
| 370 | + */ |
|
| 371 | + public function getParsedMessage(): string { |
|
| 372 | + return $this->messageParsed; |
|
| 373 | + } |
|
| 374 | + |
|
| 375 | + /** |
|
| 376 | + * @param string $message |
|
| 377 | + * @param array $parameters |
|
| 378 | + * @return $this |
|
| 379 | + * @throws \InvalidArgumentException if the message or parameters are invalid |
|
| 380 | + * @since 11.0.0 |
|
| 381 | + */ |
|
| 382 | + public function setRichMessage(string $message, array $parameters = []): INotification { |
|
| 383 | + if ($message === '') { |
|
| 384 | + throw new \InvalidArgumentException('The given parsed message is invalid'); |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + $this->messageRich = $message; |
|
| 388 | + $this->messageRichParameters = $parameters; |
|
| 389 | + |
|
| 390 | + return $this; |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + /** |
|
| 394 | + * @return string |
|
| 395 | + * @since 11.0.0 |
|
| 396 | + */ |
|
| 397 | + public function getRichMessage(): string { |
|
| 398 | + return $this->messageRich; |
|
| 399 | + } |
|
| 400 | + |
|
| 401 | + /** |
|
| 402 | + * @return array[] |
|
| 403 | + * @since 11.0.0 |
|
| 404 | + */ |
|
| 405 | + public function getRichMessageParameters(): array { |
|
| 406 | + return $this->messageRichParameters; |
|
| 407 | + } |
|
| 408 | + |
|
| 409 | + /** |
|
| 410 | + * @param string $link |
|
| 411 | + * @return $this |
|
| 412 | + * @throws \InvalidArgumentException if the link is invalid |
|
| 413 | + * @since 8.2.0 |
|
| 414 | + */ |
|
| 415 | + public function setLink(string $link): INotification { |
|
| 416 | + if ($link === '' || isset($link[4000])) { |
|
| 417 | + throw new \InvalidArgumentException('The given link is invalid'); |
|
| 418 | + } |
|
| 419 | + $this->link = $link; |
|
| 420 | + return $this; |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + /** |
|
| 424 | + * @return string |
|
| 425 | + * @since 8.2.0 |
|
| 426 | + */ |
|
| 427 | + public function getLink(): string { |
|
| 428 | + return $this->link; |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + /** |
|
| 432 | + * @param string $icon |
|
| 433 | + * @return $this |
|
| 434 | + * @throws \InvalidArgumentException if the icon is invalid |
|
| 435 | + * @since 11.0.0 |
|
| 436 | + */ |
|
| 437 | + public function setIcon(string $icon): INotification { |
|
| 438 | + if ($icon === '' || isset($icon[4000])) { |
|
| 439 | + throw new \InvalidArgumentException('The given icon is invalid'); |
|
| 440 | + } |
|
| 441 | + $this->icon = $icon; |
|
| 442 | + return $this; |
|
| 443 | + } |
|
| 444 | + |
|
| 445 | + /** |
|
| 446 | + * @return string |
|
| 447 | + * @since 11.0.0 |
|
| 448 | + */ |
|
| 449 | + public function getIcon(): string { |
|
| 450 | + return $this->icon; |
|
| 451 | + } |
|
| 452 | + |
|
| 453 | + /** |
|
| 454 | + * @return IAction |
|
| 455 | + * @since 8.2.0 |
|
| 456 | + */ |
|
| 457 | + public function createAction(): IAction { |
|
| 458 | + return new Action(); |
|
| 459 | + } |
|
| 460 | + |
|
| 461 | + /** |
|
| 462 | + * @param IAction $action |
|
| 463 | + * @return $this |
|
| 464 | + * @throws \InvalidArgumentException if the action is invalid |
|
| 465 | + * @since 8.2.0 |
|
| 466 | + */ |
|
| 467 | + public function addAction(IAction $action): INotification { |
|
| 468 | + if (!$action->isValid()) { |
|
| 469 | + throw new \InvalidArgumentException('The given action is invalid'); |
|
| 470 | + } |
|
| 471 | + |
|
| 472 | + if ($action->isPrimary()) { |
|
| 473 | + if ($this->hasPrimaryAction) { |
|
| 474 | + throw new \InvalidArgumentException('The notification already has a primary action'); |
|
| 475 | + } |
|
| 476 | + |
|
| 477 | + $this->hasPrimaryAction = true; |
|
| 478 | + } |
|
| 479 | + |
|
| 480 | + $this->actions[] = $action; |
|
| 481 | + return $this; |
|
| 482 | + } |
|
| 483 | + |
|
| 484 | + /** |
|
| 485 | + * @return IAction[] |
|
| 486 | + * @since 8.2.0 |
|
| 487 | + */ |
|
| 488 | + public function getActions(): array { |
|
| 489 | + return $this->actions; |
|
| 490 | + } |
|
| 491 | + |
|
| 492 | + /** |
|
| 493 | + * @param IAction $action |
|
| 494 | + * @return $this |
|
| 495 | + * @throws \InvalidArgumentException if the action is invalid |
|
| 496 | + * @since 8.2.0 |
|
| 497 | + */ |
|
| 498 | + public function addParsedAction(IAction $action): INotification { |
|
| 499 | + if (!$action->isValidParsed()) { |
|
| 500 | + throw new \InvalidArgumentException('The given parsed action is invalid'); |
|
| 501 | + } |
|
| 502 | + |
|
| 503 | + if ($action->isPrimary()) { |
|
| 504 | + if ($this->hasPrimaryParsedAction) { |
|
| 505 | + throw new \InvalidArgumentException('The notification already has a primary action'); |
|
| 506 | + } |
|
| 507 | + |
|
| 508 | + $this->hasPrimaryParsedAction = true; |
|
| 509 | + |
|
| 510 | + // Make sure the primary action is always the first one |
|
| 511 | + array_unshift($this->actionsParsed, $action); |
|
| 512 | + } else { |
|
| 513 | + $this->actionsParsed[] = $action; |
|
| 514 | + } |
|
| 515 | + |
|
| 516 | + return $this; |
|
| 517 | + } |
|
| 518 | + |
|
| 519 | + /** |
|
| 520 | + * @return IAction[] |
|
| 521 | + * @since 8.2.0 |
|
| 522 | + */ |
|
| 523 | + public function getParsedActions(): array { |
|
| 524 | + return $this->actionsParsed; |
|
| 525 | + } |
|
| 526 | + |
|
| 527 | + /** |
|
| 528 | + * @return bool |
|
| 529 | + * @since 8.2.0 |
|
| 530 | + */ |
|
| 531 | + public function isValid(): bool { |
|
| 532 | + return |
|
| 533 | + $this->isValidCommon() |
|
| 534 | + && |
|
| 535 | + $this->getSubject() !== '' |
|
| 536 | + ; |
|
| 537 | + } |
|
| 538 | + |
|
| 539 | + /** |
|
| 540 | + * @return bool |
|
| 541 | + * @since 8.2.0 |
|
| 542 | + */ |
|
| 543 | + public function isValidParsed(): bool { |
|
| 544 | + if ($this->getRichSubject() !== '' || !empty($this->getRichSubjectParameters())) { |
|
| 545 | + try { |
|
| 546 | + $this->richValidator->validate($this->getRichSubject(), $this->getRichSubjectParameters()); |
|
| 547 | + } catch (InvalidObjectExeption $e) { |
|
| 548 | + return false; |
|
| 549 | + } |
|
| 550 | + } |
|
| 551 | + |
|
| 552 | + if ($this->getRichMessage() !== '' || !empty($this->getRichMessageParameters())) { |
|
| 553 | + try { |
|
| 554 | + $this->richValidator->validate($this->getRichMessage(), $this->getRichMessageParameters()); |
|
| 555 | + } catch (InvalidObjectExeption $e) { |
|
| 556 | + return false; |
|
| 557 | + } |
|
| 558 | + } |
|
| 559 | + |
|
| 560 | + return |
|
| 561 | + $this->isValidCommon() |
|
| 562 | + && |
|
| 563 | + $this->getParsedSubject() !== '' |
|
| 564 | + ; |
|
| 565 | + } |
|
| 566 | + |
|
| 567 | + protected function isValidCommon(): bool { |
|
| 568 | + return |
|
| 569 | + $this->getApp() !== '' |
|
| 570 | + && |
|
| 571 | + $this->getUser() !== '' |
|
| 572 | + && |
|
| 573 | + $this->getDateTime()->getTimestamp() !== 0 |
|
| 574 | + && |
|
| 575 | + $this->getObjectType() !== '' |
|
| 576 | + && |
|
| 577 | + $this->getObjectId() !== '' |
|
| 578 | + ; |
|
| 579 | + } |
|
| 580 | 580 | } |