set('Title', $title); } /** * That set notification priority. Priority could be from 1 to 5. * * @see https://docs.ntfy.sh/publish/#message-priority * * @param int Epriority Message priority. * * @return notification Self to chain loading. */ public function priority(int $priority): object { if ($priority < 1 || $priority > 5) { throw new TypeError('Priority must be between 1 and 5.'); } return $this->set('Priority', strval($priority)); } /** * That add new tag to notification * * @see https://docs.ntfy.sh/publish/#tags-emojis * @see https://docs.ntfy.sh/emojis/ * * @param string $tag New tag to add. * * @return notification Self to chain loading. */ public function add_tag(string $tag): object { $tag = trim($tag); if (strpos($tag, ' ') !== false) { throw new TypeError('Tags could not contain white chars.'); } if (strpos($tag, ',') !== false) { throw new TypeError('Tags could not contain ",".'); } array_push($this->tags, $tag); return $this; } /** * That return list of tags to use when request is processing. * * @return string Header with the tags. */ private function get_tags(): string { return join(',', $this->tags); } /** * That make notification body markdown formated. * * @see https://docs.ntfy.sh/publish/#markdown-formatting * * @return notification Self to chain loading. */ public function enable_markdown(): object { return $this->set('Markdown', 'yes'); } /** * That make notification delayed delivery. * * @see https://docs.ntfy.sh/publish/#scheduled-delivery * * @param string $when Time to show notification. * * @return notification Self to chain loading. */ public function delivery_time(string $when): object { return $this->set('At', $when); } /** * That send notification to given email. * * @see https://docs.ntfy.sh/publish/#__tabbed_27_3 * * @param string $email Email to send notification to. * * @return notification Self to chain loading. */ public function email(string $email): object { if (filter_var($email, FILTER_VALIDATE_EMAIL)) { throw new TypeError('Email "'.$email.'" is invalid.'); } return $this->set('Email', $email); } /** * That set link which would being open after click on notification. * * @see https://docs.ntfy.sh/publish/#click-action * * @param string $url Url to open after click. * * @return notification Self to chain loading. */ public function click_url(string $url): object { return $this->set('Click', $url); } /** * That add link to attachment. * * @see https://docs.ntfy.sh/publish/#attach-file-from-a-url * * @param string $title Title to set. * * @return notification Self to chain loading. */ public function attach_url(string $url): object { if (!filter_var($url, FILTER_VALIDATE_URL)) { throw new TypeError('"'.$url.'" is not valid URL.'); } return $this->set('Attach', $url); } /** * That set URL to notification icon. * * @see https://docs.ntfy.sh/publish/#icons * * @param string $url Url of the notification. * * @return notification Self to chain loading. */ public function icon_url(string $url): object { if (!filter_var($url, FILTER_VALIDATE_URL)) { throw new TypeError('"'.$url.'" is not valid URL.'); } return $this->set('Icon', $url); } /** * That would make notification phone call. * * @see https://docs.ntfy.sh/publish/#phone-calls * * @param string|true True when call would be make or phone number. * * @return notification Self to chain loading. */ public function call(string|true $phone_number): object { if ($phone_number === true) { return $this->set('Call', 'yes'); } $check = str_replace(' ', '', $phone_number); if ($check[0] === '+') { $check = substr($check, 1); } if (!is_numeric($phone_number)) { throw new TypeError('"'.$phone_number.'" is invalid number.'); } return $this->set('Call', $phone_number); } /** * That add name of the file, which would be send,, * * @see https://docs.ntfy.sh/publish/#attachments * * @param string $filename Name of the file to save. * * @return notification Self to chain loading. */ public function save_as(string $filename): object { if (basename($filename) !== $filename) { throw new TypeError('"'.$filename.'" is invalid filename.'); } return $this->set('Filename', $filename); } /** * That would disable sending notification via firebase. * * @see https://docs.ntfy.sh/publish/#disable-firebase * * @return notification Self to chain loading. */ public function disable_firebase(): object { return $this->set('Firebase', 'no'); } /** * That would disable notification cache on server side. * * @see https://docs.ntfy.sh/publish/#message-caching * * @return notification Self to chain loading. */ public function disable_cache(): object { return $this->set('Cache', 'no'); } /** * That would set iPhone Poll-ID. * * @param string $id Poll ID to set. * * @return notification Self to chain loading. */ public function poll_id(string $id): object { return $this->set('Poll-ID', $id); } /** * That would enable unified push delivery. * * @see https://docs.ntfy.sh/publish/#unifiedpush * * @return notification Self to chain loading */ public function enable_unified_push(): object { return $this->set('UnifiedPush', 'yes'); } /** * That add new action to the notification. * * @see https://docs.ntfy.sh/publish/#action-buttons * * @param string $action Action type. * @param string $label Label of the action button. * @param ?array $param List of the actions params. * * @return notification Self to chain loading. */ public function add_action( string $action, string $label, ?array $param = null ): object { $separator = ', '; $content = $action.$separator.$label; if ($param === null) { array_push($this->actions, $content); return $this; } foreach ($param as $key => $value) { $content .= $separator; $content .= $key.'='.$value; } array_push($this->actions, $content); return $this; } /** * That process all actions into header content. * * @return string Content of the actions header. */ private function get_actions(): string { return join('; ', $this->actions); } /** * Overwriting it is required to add headers with arrays. */ protected function prepare(): void { parent::prepare(); if (count($this->actions) > 0) { $this->set('Actions', $this->get_actions()); } if (count($this->tags) > 0) { $this->set('Tags', $this->get_tags()); } } }