Skip to content

Commit

Permalink
🚧 Create a repository for poll answers
Browse files Browse the repository at this point in the history
🎨 Add docblock for event fillable attributes
🎨 Improve return types
🎨 Rename `Poll::end` to `Poll::expire` for parity with the Discord API
  • Loading branch information
Log1x committed Jul 21, 2024
1 parent 09f486e commit 15fb3ce
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 37 deletions.
64 changes: 27 additions & 37 deletions src/Discord/Parts/Channel/Poll.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Discord\Parts\Channel\Poll\PollMedia;
use Discord\Parts\Channel\Poll\PollResults;
use Discord\Parts\Part;
use Discord\Repository\Channel\PollAnswerRepository;
use React\Promise\ExtendedPromiseInterface;

/**
Expand All @@ -27,15 +28,15 @@
*
* @since 10.0.0
*
* @property ?PollMedia $question The question of the poll. Only text is supported.
* @property Collection|PollAnswer[] $answers Each of the answers available in the poll.
* @property Carbon|null $expiry The time when the poll ends.
* @property bool $allow_multiselect Whether a user can select multiple answers.
* @property int $layout_type The layout type of the poll.
* @property PollResults|null $results The results of the poll.
* @property PollMedia $question The question of the poll. Only text is supported.
* @property PollAnswerRepository $answers Each of the answers available in the poll.
* @property Carbon $expiry The time when the poll ends.
* @property bool $allow_multiselect Whether a user can select multiple answers.
* @property int $layout_type The layout type of the poll.
* @property PollResults|null $results The results of the poll.
*
* @property string $channel_id The ID of the channel the poll is in.
* @property string $message_id The ID of the message the poll is in.
* @property string $channel_id The ID of the channel the poll is in.
* @property string $message_id The ID of the message the poll is in.
*/
class Poll extends Part
{
Expand All @@ -44,15 +45,23 @@ class Poll extends Part
*/
protected $fillable = [
'question',
'answers',
'expiry',
'allow_multiselect',
'layout_type',
'results',

// events
'channel_id',
'message_id',
];

/**
* {@inheritdoc}
*/
protected $repositories = [
'answers' => PollAnswerRepository::class,
];

/**
* Returns the question attribute.
*
Expand All @@ -63,37 +72,15 @@ protected function getQuestionAttribute(): PollMedia
return $this->factory->part(PollMedia::class, (array) $this->attributes['question'], true);
}

/**
* Returns the answers attribute.
*
* @return Collection|PollAnswer[]
*/
protected function getAnswersAttribute(): Collection
{
$answers = Collection::for(PollAnswer::class);

foreach ($this->attributes['answers'] ?? [] as $answer) {
$part = $this->factory->part(PollAnswer::class, (array) $answer, true);

$answers->pushItem($part);
}

return $answers;
}

/**
* Return the expiry attribute.
*
* @return Carbon|null
* @return Carbon
*
* @throws \Exception
*/
protected function getExpiryAttribute(): ?Carbon
protected function getExpiryAttribute(): Carbon
{
if (! isset($this->attributes['expiry'])) {
return null;
}

return Carbon::parse($this->attributes['expiry']);
}

Expand All @@ -112,14 +99,17 @@ protected function getResultsAttribute(): ?PollResults
}

/**
* End the poll.
* Expire the poll.
*
* @link https://discord.com/developers/docs/resources/poll#end-poll
*
* @return ExtendedPromiseInterface
* @return ExtendedPromiseInterface<Message>
*/
public function end(): ExtendedPromiseInterface
public function expire(): ExtendedPromiseInterface
{
return $this->http->post(Endpoint::bind(Endpoint::CHANNEL_POLL_EXPIRE, $this->channel_id, $this->message_id));
return $this->http->post(Endpoint::bind(Endpoint::CHANNEL_POLL_EXPIRE, $this->channel_id, $this->message_id))
->then(function ($response) {
return $this->factory->create(Message::class, (array) $response, true);
});
}
}
47 changes: 47 additions & 0 deletions src/Discord/Repository/Channel/PollAnswerRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/*
* This file is a part of the DiscordPHP project.
*
* Copyright (c) 2015-present David Cole <david.cole1340@gmail.com>
*
* This file is subject to the MIT license that is bundled
* with this source code in the LICENSE.md file.
*/

namespace Discord\Repository\Channel;

use Discord\Http\Endpoint;
use Discord\Parts\Channel\Poll\PollAnswer;
use Discord\Repository\AbstractRepository;
use React\Promise\ExtendedPromiseInterface;

/**
* Contains poll answers on a poll in a message.
*
* @see PollAnswer
* @see \Discord\Parts\Channel\Poll
* @see \Discord\Parts\Channel\Message
*
* @since 10.0.0
*
* @method PollAnswer|null get(string $discrim, $key)
* @method PollAnswer|null pull(string|int $key, $default = null)
* @method PollAnswer|null first()
* @method PollAnswer|null last()
* @method PollAnswer|null find(callable $callback)
*/
class PollAnswerRepository extends AbstractRepository
{
/**
* {@inheritDoc}
*/
protected $endpoints = [
'get' => Endpoint::MESSAGE_POLL_ANSWER,
];

/**
* {@inheritDoc}
*/
protected $class = PollAnswer::class;
}

0 comments on commit 15fb3ce

Please sign in to comment.