subject.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace phpnotify;
  3. /**
  4. * That class represents subject, also known as topic in library.
  5. *
  6. * That represents single topic, also known as subject in the library. It
  7. * would be build by notifier, but could also being builded manually without
  8. * it. Subject would be used to generating new notifications, which could be
  9. * personalized and sended to notifications server
  10. */
  11. class subject extends authorization_store {
  12. /**
  13. * URL of the subject.
  14. * @var string
  15. */
  16. private string $url;
  17. /**
  18. * This create new subject.
  19. *
  20. * This create new topic from the server URL, subject name and also
  21. * authentication method. It would being used by notifier class, for
  22. * generating subjects manually use {@see subject::create} static
  23. * function. When subject is incorrect or url generated from that is
  24. * not valid URL, raises TypeError.
  25. *
  26. * @throws TypeError When subject is not correct
  27. * @throws TypeError When final subject URL is not valid.
  28. *
  29. * @param string $server Server URL.
  30. * @param string $subject Subject name.
  31. * @param authorization_method $authorization Authorization method to use.
  32. */
  33. public function __construct(
  34. string $server,
  35. string $subject,
  36. authorization_method $authorization
  37. ) {
  38. parent::__construct($authorization);
  39. $subject = trim($subject);
  40. if (strlen($subject) === 0 || strpos($subject, ' ') !== false) {
  41. throw new TypeError(
  42. 'Subject could not being empty and contains space.'
  43. );
  44. }
  45. $this->url = $this->get_topic_url($server, $subject);
  46. if (!filter_var($this->url, FILTER_VALIDATE_URL)) {
  47. throw new TypeError('"'.$subject.'" is not property topic name.');
  48. }
  49. }
  50. /**
  51. * That function create new subject directly from server name and
  52. * subject name. It use empty authorization method by default, but it
  53. * could be changed. When server URL is invalid throws TypeError.
  54. *
  55. * @throws TypeError When server URL is invalid.
  56. *
  57. * @param string $server URL of the server.
  58. * @param string $subject Name of the subject.
  59. *
  60. * @return subject New created subject.
  61. */
  62. public static function create(string $server, string $subject): subject {
  63. if (!filter_var($server, FILTER_VALIDATE_URL)) {
  64. throw new TypeError('"'.$server.'" is not property URL.');
  65. }
  66. return new self($server, $subject, new empty_authorization());
  67. }
  68. /**
  69. * This return full subject url.
  70. *
  71. * This function return full URL to the subject. It require server
  72. * URL and subject name.
  73. *
  74. * @param string $server Server URL.
  75. * @param string $subject Subject to use.
  76. *
  77. * @return string Full subject URL.
  78. */
  79. private function get_topic_url(
  80. string $server,
  81. string $subject
  82. ): string {
  83. if ($server[strlen($server) - 1] === '/') {
  84. return $server.$subject;
  85. }
  86. return $server.'/'.$subject;
  87. }
  88. /**
  89. * This function generate new notification to publish in subject.
  90. *
  91. * This create new notification, to publish in that subject. Content
  92. * given as parameter is content of the notofication. Notification
  93. * could be infill with more data before send, that function not sending
  94. * notification, but only create it.
  95. *
  96. * @param string $content Content of the new notification.
  97. *
  98. * @return notification New notification to infill with data and send.
  99. */
  100. public function new_notification(string $content): notification {
  101. return new notification(
  102. $this->url,
  103. $content,
  104. $this->get_access_method()
  105. );
  106. }
  107. }