From ddeb77da951bea412240d7554e16b8029959a95e Mon Sep 17 00:00:00 2001 From: aymm Date: Sun, 28 Mar 2021 13:15:04 +0200 Subject: [PATCH] poll getMyVotes(), improved poll error handling --- src/APnutI.php | 19 ++++--------------- src/Entities/Poll.php | 12 ++++++++++++ 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/APnutI.php b/src/APnutI.php index d16f540..1c7ec23 100644 --- a/src/APnutI.php +++ b/src/APnutI.php @@ -481,9 +481,10 @@ class APnutI return $polls; } - private function getPollFromResponse(array $res): Poll + private function getPollFromEndpoint(string $endpoint, array $args = []): Poll { try { + $res = $this->get($endpoint, $args); return new Poll($res, $this); } catch (NotSupportedPollException $e) { $this->logger->error('Poll not supported: '.json_encode($res)); @@ -500,13 +501,7 @@ class APnutI public function getPollFromToken(int $poll_id, ?string $poll_token = null): Poll { $poll_token_query = empty($poll_token) ? '' : '?poll_token=' . $poll_token; - try { - $res = $this->get('/polls/' . $poll_id . $poll_token_query); - return $this->getPollFromResponse($res); - } catch (NotAuthorizedException $nauth) { - $this->logger->error('Not authorized when fetching poll'); - throw new PollAccessRestrictedException(); - } + return $this->getPollFromEndpoint('/polls/' . $poll_id . $poll_token_query); } public function getPoll(int $poll_id, ?string $poll_token = null): Poll @@ -527,13 +522,7 @@ class APnutI 'include_html' => false, 'include_post_raw' => true ]; - try { - $res = $this->get('/posts/' . $post_id, $args); - return $this->getPollFromResponse($res); - } catch (NotAuthorizedException $nauth) { - $this->logger->error('Not authorized when fetching poll'); - throw new PollAccessRestrictedException(); - } + return $this->getPollFromEndpoint('/posts/' . $post_id, $arg); } else { $this->logger->debug('Poll token seems to be an actual poll token'); return $this->getPollFromToken($poll_id, $poll_token); diff --git a/src/Entities/Poll.php b/src/Entities/Poll.php index 4121464..8ca51d3 100644 --- a/src/Entities/Poll.php +++ b/src/Entities/Poll.php @@ -12,6 +12,7 @@ class Poll public \DateTime $created_at; public \DateTime $closed_at; public int $id = 0; + public int $max_options = 0; public bool $is_anonymous = false; public bool $is_public = false; public array $options = []; @@ -77,6 +78,7 @@ class Poll $this->closed_at = new \DateTime($data['closed_at']); $this->id = (int)$data['id']; $this->is_anonymous = array_key_exists('is_anonymous', $data) ? (bool)$data['is_anonymous'] : false; + $this->max_options = array_key_exists('max_options', $data) ? (int)$data['max_options'] : 1; $this->is_public = array_key_exists('is_public', $data) ? (bool)$data['is_public'] : false; foreach ($data['options'] as $option) { $this->options[] = new PollOption($option); @@ -117,6 +119,16 @@ class Poll return $optns; } + public function getMyVotes(): array { + $optns = []; + foreach ($this->options as $option) { + if ($option->is_your_response) { + $optns[] = $option; + } + } + return $optns; + } + public static function isValidPoll(string $type): bool { return $type === Poll::$notice_type || in_array($type, Poll::$poll_types);