| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- <?php
- namespace phpnotify;
- /**
- * That class represents subject, also known as topic in library.
- *
- * That represents single topic, also known as subject in the library. It
- * would be build by notifier, but could also being builded manually without
- * it. Subject would be used to generating new notifications, which could be
- * personalized and sended to notifications server
- */
- class subject extends authorization_store {
- /**
- * URL of the subject.
- * @var string
- */
- private string $url;
- /**
- * This create new subject.
- *
- * This create new topic from the server URL, subject name and also
- * authentication method. It would being used by notifier class, for
- * generating subjects manually use {@see subject::create} static
- * function. When subject is incorrect or url generated from that is
- * not valid URL, raises TypeError.
- *
- * @throws TypeError When subject is not correct
- * @throws TypeError When final subject URL is not valid.
- *
- * @param string $server Server URL.
- * @param string $subject Subject name.
- * @param authorization_method $authorization Authorization method to use.
- */
- public function __construct(
- string $server,
- string $subject,
- authorization_method $authorization
- ) {
- parent::__construct($authorization);
- $subject = trim($subject);
- if (strlen($subject) === 0 || strpos($subject, ' ') !== false) {
- throw new TypeError(
- 'Subject could not being empty and contains space.'
- );
- }
- $this->url = $this->get_topic_url($server, $subject);
- if (!filter_var($this->url, FILTER_VALIDATE_URL)) {
- throw new TypeError('"'.$subject.'" is not property topic name.');
- }
- }
- /**
- * That function create new subject directly from server name and
- * subject name. It use empty authorization method by default, but it
- * could be changed. When server URL is invalid throws TypeError.
- *
- * @throws TypeError When server URL is invalid.
- *
- * @param string $server URL of the server.
- * @param string $subject Name of the subject.
- *
- * @return subject New created subject.
- */
- public static function create(string $server, string $subject): subject {
- if (!filter_var($server, FILTER_VALIDATE_URL)) {
- throw new TypeError('"'.$server.'" is not property URL.');
- }
- return new self($server, $subject, new empty_authorization());
- }
- /**
- * This return full subject url.
- *
- * This function return full URL to the subject. It require server
- * URL and subject name.
- *
- * @param string $server Server URL.
- * @param string $subject Subject to use.
- *
- * @return string Full subject URL.
- */
- private function get_topic_url(
- string $server,
- string $subject
- ): string {
- if ($server[strlen($server) - 1] === '/') {
- return $server.$subject;
- }
- return $server.'/'.$subject;
- }
- /**
- * This function generate new notification to publish in subject.
- *
- * This create new notification, to publish in that subject. Content
- * given as parameter is content of the notofication. Notification
- * could be infill with more data before send, that function not sending
- * notification, but only create it.
- *
- * @param string $content Content of the new notification.
- *
- * @return notification New notification to infill with data and send.
- */
- public function new_notification(string $content): notification {
- return new notification(
- $this->url,
- $content,
- $this->get_access_method()
- );
- }
- }
|