|
|
@@ -2,36 +2,65 @@
|
|
|
|
|
|
namespace phpnotify;
|
|
|
|
|
|
-require_once('fetch.php');
|
|
|
-
|
|
|
-class notification {
|
|
|
- private array $tags;
|
|
|
- private array $actions;
|
|
|
- private fetch $request;
|
|
|
-
|
|
|
- public function __construct(string $location, string $content) {
|
|
|
- $this->tags = array();
|
|
|
- $this->actions = array();
|
|
|
-
|
|
|
- $this->request = new fetch($location);
|
|
|
- $this->request->set_method('POST');
|
|
|
- $this->request->send_raw($content, 'text/plain');
|
|
|
+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);
|
|
|
}
|
|
|
|
|
|
- public function set_title(string $title): object {
|
|
|
- $this->request->add_header('Title', $title);
|
|
|
- return $this;
|
|
|
- }
|
|
|
-
|
|
|
- public function set_priority(int $priority): object {
|
|
|
+ /**
|
|
|
+ * 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.');
|
|
|
}
|
|
|
|
|
|
- $this->request->add_header('Priority', strval($priority));
|
|
|
- return $this;
|
|
|
+ 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);
|
|
|
|
|
|
@@ -47,60 +76,115 @@ class notification {
|
|
|
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);
|
|
|
}
|
|
|
|
|
|
- public function set_markdown(): object {
|
|
|
- $this->request->add_header('Markdown', 'yes');
|
|
|
- return $this;
|
|
|
+ /**
|
|
|
+ * 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 {
|
|
|
- $this->request->add_header('At', $when);
|
|
|
- return $this;
|
|
|
+ return $this->set('At', $when);
|
|
|
}
|
|
|
|
|
|
- public function set_email(string $email): object {
|
|
|
+ /**
|
|
|
+ * 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.');
|
|
|
}
|
|
|
|
|
|
- $this->request->add_header('Email', $email);
|
|
|
- return $this;
|
|
|
+ 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 {
|
|
|
- if (!filter_var($url, FILTER_VALIDATE_URL)) {
|
|
|
- throw new TypeError('"'.$url.'" is not valid URL.');
|
|
|
- }
|
|
|
-
|
|
|
- $this->request->add_header('Action', $url);
|
|
|
- return $this;
|
|
|
+ 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.');
|
|
|
}
|
|
|
|
|
|
- $this->request->add_header('Attach', $url);
|
|
|
- return $this;
|
|
|
+ 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.');
|
|
|
}
|
|
|
|
|
|
- $this->request->add_header('Icon', $url);
|
|
|
- return $this;
|
|
|
+ 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) {
|
|
|
- $this->request->add_header('Call', 'yes');
|
|
|
- return $this;
|
|
|
+ return $this->set('Call', 'yes');
|
|
|
}
|
|
|
|
|
|
$check = str_replace(' ', '', $phone_number);
|
|
|
@@ -113,56 +197,81 @@ class notification {
|
|
|
throw new TypeError('"'.$phone_number.'" is invalid number.');
|
|
|
}
|
|
|
|
|
|
- $this->request->add_header('Call', $phone_number);
|
|
|
- return $this;
|
|
|
+ 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.');
|
|
|
}
|
|
|
|
|
|
- $this->request->add_header('Filename', $filename);
|
|
|
- return $this;
|
|
|
- }
|
|
|
-
|
|
|
- public function login(string $nick, string $password): object {
|
|
|
- $decoded = $nick.':'.$password;
|
|
|
- $encoded = base64_encode($decoded);
|
|
|
- $param = 'Basic '.$encoded;
|
|
|
-
|
|
|
- $this->request->add_header('Authorization', $param);
|
|
|
- return $this;
|
|
|
- }
|
|
|
-
|
|
|
- public function token(string $token): object {
|
|
|
- $token = trim($token);
|
|
|
- $param = 'Bearer '.$token;
|
|
|
-
|
|
|
- $this->request->add_header('Authorization', $param);
|
|
|
- return $this;
|
|
|
+ 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 {
|
|
|
- $this->request->add_header('Firebase', 'no');
|
|
|
- return $this;
|
|
|
+ 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 {
|
|
|
- $this->request->add_header('Cache', 'no');
|
|
|
- return $this;
|
|
|
+ return $this->set('Cache', 'no');
|
|
|
}
|
|
|
|
|
|
- public function set_poll_id(string $id): object {
|
|
|
- $this->request->add_header('Poll-ID', $id);
|
|
|
- return $this;
|
|
|
+ /**
|
|
|
+ * 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 {
|
|
|
- $this->request->add_header('UnifiedPush', 'yes');
|
|
|
- return $this;
|
|
|
+ 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,
|
|
|
@@ -184,41 +293,28 @@ class notification {
|
|
|
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);
|
|
|
}
|
|
|
|
|
|
- public function send(bool $throws = false): bool {
|
|
|
- $this->request->set_header('Actions', $this->get_actions());
|
|
|
- $this->request->set_header('Tags', $this->get_tags());
|
|
|
-
|
|
|
- try {
|
|
|
- $result = $this->request->request()->receive_array();
|
|
|
+ /**
|
|
|
+ * Overwriting it is required to add headers with arrays.
|
|
|
+ */
|
|
|
+ protected function prepare(): void {
|
|
|
+ parent::prepare();
|
|
|
|
|
|
- if (array_key_exists('id', $result)) {
|
|
|
- return true;
|
|
|
- }
|
|
|
+ if (count($this->actions) > 0) {
|
|
|
+ $this->set('Actions', $this->get_actions());
|
|
|
+ }
|
|
|
|
|
|
- if (count($result) === 0) {
|
|
|
- throw new RuntimeException('Can not fetch. General error.');
|
|
|
- }
|
|
|
-
|
|
|
- if (array_key_exists('error', $result)) {
|
|
|
- $error = (
|
|
|
- 'Can not publish: "'
|
|
|
- .$result['error']
|
|
|
- .'"'
|
|
|
- );
|
|
|
-
|
|
|
- throw new RuntimeException($error);
|
|
|
- }
|
|
|
- } catch (Exception $error) {
|
|
|
- if ($throws) {
|
|
|
- throw $error;
|
|
|
- }
|
|
|
-
|
|
|
- return false;
|
|
|
+ if (count($this->tags) > 0) {
|
|
|
+ $this->set('Tags', $this->get_tags());
|
|
|
}
|
|
|
}
|
|
|
-}
|
|
|
+}
|