notification.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. namespace phpnotify;
  3. use \TypeError as TypeError;
  4. use \RuntimeException as RuntimeException;
  5. /**
  6. * It add more personalization options to the notifications.
  7. *
  8. * @see https://docs.ntfy.sh/publish/#list-of-all-parameters
  9. */
  10. class notification extends notification_base{
  11. /**
  12. * This store tags, which would be send.
  13. * @var array
  14. */
  15. private array $tags = [];
  16. /**
  17. * That store actions for the notification.
  18. * @var array
  19. */
  20. private array $actions = [];
  21. /**
  22. * That set notification title.
  23. *
  24. * @param string $title Title to set.
  25. *
  26. * @return notification Self to chain loading.
  27. */
  28. public function title(string $title): object {
  29. return $this->set('Title', $title);
  30. }
  31. /**
  32. * That set notification priority. Priority could be from 1 to 5.
  33. *
  34. * @see https://docs.ntfy.sh/publish/#message-priority
  35. *
  36. * @param int Epriority Message priority.
  37. *
  38. * @return notification Self to chain loading.
  39. */
  40. public function priority(int $priority): object {
  41. if ($priority < 1 || $priority > 5) {
  42. throw new TypeError('Priority must be between 1 and 5.');
  43. }
  44. return $this->set('Priority', strval($priority));
  45. }
  46. /**
  47. * That add new tag to notification
  48. *
  49. * @see https://docs.ntfy.sh/publish/#tags-emojis
  50. * @see https://docs.ntfy.sh/emojis/
  51. *
  52. * @param string $tag New tag to add.
  53. *
  54. * @return notification Self to chain loading.
  55. */
  56. public function add_tag(string $tag): object {
  57. $tag = trim($tag);
  58. if (strpos($tag, ' ') !== false) {
  59. throw new TypeError('Tags could not contain white chars.');
  60. }
  61. if (strpos($tag, ',') !== false) {
  62. throw new TypeError('Tags could not contain ",".');
  63. }
  64. array_push($this->tags, $tag);
  65. return $this;
  66. }
  67. /**
  68. * That return list of tags to use when request is processing.
  69. *
  70. * @return string Header with the tags.
  71. */
  72. private function get_tags(): string {
  73. return join(',', $this->tags);
  74. }
  75. /**
  76. * That make notification body markdown formated.
  77. *
  78. * @see https://docs.ntfy.sh/publish/#markdown-formatting
  79. *
  80. * @return notification Self to chain loading.
  81. */
  82. public function enable_markdown(): object {
  83. return $this->set('Markdown', 'yes');
  84. }
  85. /**
  86. * That make notification delayed delivery.
  87. *
  88. * @see https://docs.ntfy.sh/publish/#scheduled-delivery
  89. *
  90. * @param string $when Time to show notification.
  91. *
  92. * @return notification Self to chain loading.
  93. */
  94. public function delivery_time(string $when): object {
  95. return $this->set('At', $when);
  96. }
  97. /**
  98. * That send notification to given email.
  99. *
  100. * @see https://docs.ntfy.sh/publish/#__tabbed_27_3
  101. *
  102. * @param string $email Email to send notification to.
  103. *
  104. * @return notification Self to chain loading.
  105. */
  106. public function email(string $email): object {
  107. if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
  108. throw new TypeError('Email "'.$email.'" is invalid.');
  109. }
  110. return $this->set('Email', $email);
  111. }
  112. /**
  113. * That set link which would being open after click on notification.
  114. *
  115. * @see https://docs.ntfy.sh/publish/#click-action
  116. *
  117. * @param string $url Url to open after click.
  118. *
  119. * @return notification Self to chain loading.
  120. */
  121. public function click_url(string $url): object {
  122. return $this->set('Click', $url);
  123. }
  124. /**
  125. * That add link to attachment.
  126. *
  127. * @see https://docs.ntfy.sh/publish/#attach-file-from-a-url
  128. *
  129. * @param string $title Title to set.
  130. *
  131. * @return notification Self to chain loading.
  132. */
  133. public function attach_url(string $url): object {
  134. if (!filter_var($url, FILTER_VALIDATE_URL)) {
  135. throw new TypeError('"'.$url.'" is not valid URL.');
  136. }
  137. return $this->set('Attach', $url);
  138. }
  139. /**
  140. * That set URL to notification icon.
  141. *
  142. * @see https://docs.ntfy.sh/publish/#icons
  143. *
  144. * @param string $url Url of the notification.
  145. *
  146. * @return notification Self to chain loading.
  147. */
  148. public function icon_url(string $url): object {
  149. if (!filter_var($url, FILTER_VALIDATE_URL)) {
  150. throw new TypeError('"'.$url.'" is not valid URL.');
  151. }
  152. return $this->set('Icon', $url);
  153. }
  154. /**
  155. * That would make notification phone call.
  156. *
  157. * @see https://docs.ntfy.sh/publish/#phone-calls
  158. *
  159. * @param string|true True when call would be make or phone number.
  160. *
  161. * @return notification Self to chain loading.
  162. */
  163. public function call(string|true $phone_number): object {
  164. if ($phone_number === true) {
  165. return $this->set('Call', 'yes');
  166. }
  167. $check = str_replace(' ', '', $phone_number);
  168. if ($check[0] === '+') {
  169. $check = substr($check, 1);
  170. }
  171. if (!is_numeric($phone_number)) {
  172. throw new TypeError('"'.$phone_number.'" is invalid number.');
  173. }
  174. return $this->set('Call', $phone_number);
  175. }
  176. /**
  177. * That add name of the file, which would be send,,
  178. *
  179. * @see https://docs.ntfy.sh/publish/#attachments
  180. *
  181. * @param string $filename Name of the file to save.
  182. *
  183. * @return notification Self to chain loading.
  184. */
  185. public function save_as(string $filename): object {
  186. if (basename($filename) !== $filename) {
  187. throw new TypeError('"'.$filename.'" is invalid filename.');
  188. }
  189. return $this->set('Filename', $filename);
  190. }
  191. /**
  192. * That would disable sending notification via firebase.
  193. *
  194. * @see https://docs.ntfy.sh/publish/#disable-firebase
  195. *
  196. * @return notification Self to chain loading.
  197. */
  198. public function disable_firebase(): object {
  199. return $this->set('Firebase', 'no');
  200. }
  201. /**
  202. * That would disable notification cache on server side.
  203. *
  204. * @see https://docs.ntfy.sh/publish/#message-caching
  205. *
  206. * @return notification Self to chain loading.
  207. */
  208. public function disable_cache(): object {
  209. return $this->set('Cache', 'no');
  210. }
  211. /**
  212. * That would set iPhone Poll-ID.
  213. *
  214. * @param string $id Poll ID to set.
  215. *
  216. * @return notification Self to chain loading.
  217. */
  218. public function poll_id(string $id): object {
  219. return $this->set('Poll-ID', $id);
  220. }
  221. /**
  222. * That would enable unified push delivery.
  223. *
  224. * @see https://docs.ntfy.sh/publish/#unifiedpush
  225. *
  226. * @return notification Self to chain loading
  227. */
  228. public function enable_unified_push(): object {
  229. return $this->set('UnifiedPush', 'yes');
  230. }
  231. /**
  232. * That add new action to the notification.
  233. *
  234. * @see https://docs.ntfy.sh/publish/#action-buttons
  235. *
  236. * @param string $action Action type.
  237. * @param string $label Label of the action button.
  238. * @param ?array<str, str> $param List of the actions params.
  239. *
  240. * @return notification Self to chain loading.
  241. */
  242. public function add_action(
  243. string $action,
  244. string $label,
  245. ?array $param = null
  246. ): object {
  247. $separator = ', ';
  248. $content = $action.$separator.$label;
  249. if ($param === null) {
  250. array_push($this->actions, $content);
  251. return $this;
  252. }
  253. foreach ($param as $key => $value) {
  254. $content .= $separator;
  255. $content .= $key.'='.$value;
  256. }
  257. array_push($this->actions, $content);
  258. return $this;
  259. }
  260. /**
  261. * That process all actions into header content.
  262. *
  263. * @return string Content of the actions header.
  264. */
  265. private function get_actions(): string {
  266. return join('; ', $this->actions);
  267. }
  268. /**
  269. * Overwriting it is required to add headers with arrays.
  270. */
  271. protected function prepare(): void {
  272. parent::prepare();
  273. if (count($this->actions) > 0) {
  274. $this->set('Actions', $this->get_actions());
  275. }
  276. if (count($this->tags) > 0) {
  277. $this->set('Tags', $this->get_tags());
  278. }
  279. }
  280. }