| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 | <?phpnamespace phpnotify;use \TypeError as TypeError;use \RuntimeException as RuntimeException;/** * It add more personalization options to the notifications. * * @see https://docs.ntfy.sh/publish/#list-of-all-parameters */class notification extends notification_base{    /**     * This store tags, which would be send.     * @var array     */    private array $tags = [];    /**     * That store actions for the notification.     * @var array      */    private array $actions = [];    /**     * That set notification title.     *      * @param string $title Title to set.     *      * @return notification Self to chain loading.     */    public function title(string $title): object {        return $this->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<str, str> $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());        }    }}
 |