tags = array(); $this->actions = array(); $this->request = new fetch($location); $this->request->set_method('POST'); $this->request->send_raw($content, 'text/plain'); } public function set_title(string $title): object { $this->request->add_header('Title', $title); return $this; } public function set_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; } 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; } private function get_tags(): string { return join(',', $this->tags); } public function set_markdown(): object { $this->request->add_header('Markdown', 'yes'); return $this; } public function delivery_time(string $when): object { $this->request->add_header('At', $when); return $this; } public function set_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; } 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; } 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; } 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; } public function call(string|true $phone_number): object { if ($phone_number === true) { $this->request->add_header('Call', 'yes'); return $this; } $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.'); } $this->request->add_header('Call', $phone_number); return $this; } 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; } public function disable_firebase(): object { $this->request->add_header('Firebase', 'no'); return $this; } public function disable_cache(): object { $this->request->add_header('Cache', 'no'); return $this; } public function set_poll_id(string $id): object { $this->request->add_header('Poll-ID', $id); return $this; } public function enable_unified_push(): object { $this->request->add_header('UnifiedPush', 'yes'); return $this; } 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; } 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(); if (array_key_exists('id', $result)) { return true; } 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; } } }