poll getMyVotes(), improved poll error handling
This commit is contained in:
parent
c2434cfedc
commit
ddeb77da95
@ -481,9 +481,10 @@ class APnutI
|
|||||||
return $polls;
|
return $polls;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getPollFromResponse(array $res): Poll
|
private function getPollFromEndpoint(string $endpoint, array $args = []): Poll
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
|
$res = $this->get($endpoint, $args);
|
||||||
return new Poll($res, $this);
|
return new Poll($res, $this);
|
||||||
} catch (NotSupportedPollException $e) {
|
} catch (NotSupportedPollException $e) {
|
||||||
$this->logger->error('Poll not supported: '.json_encode($res));
|
$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
|
public function getPollFromToken(int $poll_id, ?string $poll_token = null): Poll
|
||||||
{
|
{
|
||||||
$poll_token_query = empty($poll_token) ? '' : '?poll_token=' . $poll_token;
|
$poll_token_query = empty($poll_token) ? '' : '?poll_token=' . $poll_token;
|
||||||
try {
|
return $this->getPollFromEndpoint('/polls/' . $poll_id . $poll_token_query);
|
||||||
$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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getPoll(int $poll_id, ?string $poll_token = null): Poll
|
public function getPoll(int $poll_id, ?string $poll_token = null): Poll
|
||||||
@ -527,13 +522,7 @@ class APnutI
|
|||||||
'include_html' => false,
|
'include_html' => false,
|
||||||
'include_post_raw' => true
|
'include_post_raw' => true
|
||||||
];
|
];
|
||||||
try {
|
return $this->getPollFromEndpoint('/posts/' . $post_id, $arg);
|
||||||
$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();
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
$this->logger->debug('Poll token seems to be an actual poll token');
|
$this->logger->debug('Poll token seems to be an actual poll token');
|
||||||
return $this->getPollFromToken($poll_id, $poll_token);
|
return $this->getPollFromToken($poll_id, $poll_token);
|
||||||
|
@ -12,6 +12,7 @@ class Poll
|
|||||||
public \DateTime $created_at;
|
public \DateTime $created_at;
|
||||||
public \DateTime $closed_at;
|
public \DateTime $closed_at;
|
||||||
public int $id = 0;
|
public int $id = 0;
|
||||||
|
public int $max_options = 0;
|
||||||
public bool $is_anonymous = false;
|
public bool $is_anonymous = false;
|
||||||
public bool $is_public = false;
|
public bool $is_public = false;
|
||||||
public array $options = [];
|
public array $options = [];
|
||||||
@ -77,6 +78,7 @@ class Poll
|
|||||||
$this->closed_at = new \DateTime($data['closed_at']);
|
$this->closed_at = new \DateTime($data['closed_at']);
|
||||||
$this->id = (int)$data['id'];
|
$this->id = (int)$data['id'];
|
||||||
$this->is_anonymous = array_key_exists('is_anonymous', $data) ? (bool)$data['is_anonymous'] : false;
|
$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;
|
$this->is_public = array_key_exists('is_public', $data) ? (bool)$data['is_public'] : false;
|
||||||
foreach ($data['options'] as $option) {
|
foreach ($data['options'] as $option) {
|
||||||
$this->options[] = new PollOption($option);
|
$this->options[] = new PollOption($option);
|
||||||
@ -117,6 +119,16 @@ class Poll
|
|||||||
return $optns;
|
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
|
public static function isValidPoll(string $type): bool
|
||||||
{
|
{
|
||||||
return $type === Poll::$notice_type || in_array($type, Poll::$poll_types);
|
return $type === Poll::$notice_type || in_array($type, Poll::$poll_types);
|
||||||
|
Loading…
Reference in New Issue
Block a user