-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor MessageBuilder parts to be reusable for EditMessageBuilder
- Loading branch information
Showing
13 changed files
with
459 additions
and
285 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Exan\Dhp\Rest\Helpers\Channel; | ||
|
||
use Exan\Dhp\Rest\Helpers\Channel\Message\AddAttachment; | ||
use Exan\Dhp\Rest\Helpers\Channel\Message\AddComponent; | ||
use Exan\Dhp\Rest\Helpers\Channel\Message\AddEmbed; | ||
use Exan\Dhp\Rest\Helpers\Channel\Message\AddFile; | ||
use Exan\Dhp\Rest\Helpers\Channel\Message\AllowMentions; | ||
use Exan\Dhp\Rest\Helpers\Channel\Message\MultipartMessage; | ||
use Exan\Dhp\Rest\Helpers\Channel\Message\SetContent; | ||
use Exan\Dhp\Rest\Helpers\Channel\Message\SetFlags; | ||
use Exan\Dhp\Rest\Helpers\MultipartCapable; | ||
|
||
class EditMessageBuilder implements MultipartCapable | ||
{ | ||
use AddAttachment; | ||
use AddComponent; | ||
use AddEmbed; | ||
use AddFile; | ||
use AllowMentions; | ||
use SetContent; | ||
use SetFlags; | ||
use MultipartMessage; | ||
|
||
private $data = []; | ||
|
||
private $files = []; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace Exan\Dhp\Rest\Helpers\Channel\Message; | ||
|
||
use Exan\Dhp\Rest\Helpers\Channel\AttachmentBuilder; | ||
|
||
trait AddAttachment | ||
{ | ||
/** | ||
* @see https://discord.com/developers/docs/resources/channel#attachment-object | ||
*/ | ||
public function addAttachment(AttachmentBuilder $attachment): self | ||
{ | ||
if (!isset($this->data['attachments'])) { | ||
$this->data['attachments'] = []; | ||
} | ||
|
||
$this->data['attachments'][] = $attachment->get(); | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace Exan\Dhp\Rest\Helpers\Channel\Message; | ||
|
||
use Exan\Dhp\Rest\Helpers\Channel\ComponentBuilder; | ||
|
||
trait AddComponent | ||
{ | ||
/** | ||
* @see https://discord.com/developers/docs/interactions/message-components#component-object | ||
*/ | ||
public function addComponent(ComponentBuilder $component): self | ||
{ | ||
if (!isset($this->data['components'])) { | ||
$this->data['components'] = []; | ||
} | ||
|
||
$this->data['components'][] = $component->get(); | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace Exan\Dhp\Rest\Helpers\Channel\Message; | ||
|
||
use Exan\Dhp\Rest\Helpers\Channel\EmbedBuilder; | ||
|
||
trait AddEmbed | ||
{ | ||
/** | ||
* Deduplicated by url | ||
* Up to 6000 characters across all text fields | ||
* Up to 25 fields total | ||
* @see https://discord.com/developers/docs/resources/channel#embed-object | ||
*/ | ||
public function addEmbed(EmbedBuilder $embed): self | ||
{ | ||
if (!isset($this->data['embeds'])) { | ||
$this->data['embeds'] = []; | ||
} | ||
|
||
$this->data['embeds'][] = $embed->get(); | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace Exan\Dhp\Rest\Helpers\Channel\Message; | ||
|
||
trait AddFile | ||
{ | ||
/** | ||
* @var string $contentType | ||
* Content-Type header to be used for this file. | ||
* If not provided, this is guessed based on file extension. | ||
* @see https://discord.com/developers/docs/reference#uploading-files | ||
*/ | ||
public function addFile(string $fileName, string $content, ?string $contentType = null): self | ||
{ | ||
$file = [ | ||
'name' => $fileName, | ||
'content' => $content, | ||
]; | ||
|
||
$this->files[] = &$file; | ||
|
||
if (!is_null($contentType)) { | ||
$file['type'] = $contentType; | ||
|
||
return $this; | ||
} | ||
|
||
$fileInfo = pathinfo($fileName); | ||
if (empty($fileInfo['extension'])) { | ||
return $this; | ||
} | ||
|
||
$type = (new \Mimey\MimeTypes())->getMimeType($fileInfo['extension']); | ||
|
||
if (!is_null($type)) { | ||
$file['type'] = $type; | ||
} | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Exan\Dhp\Rest\Helpers\Channel\Message; | ||
|
||
use Exan\Dhp\Rest\Helpers\Channel\AllowedMentionsBuilder; | ||
|
||
trait AllowMentions | ||
{ | ||
/** | ||
* @see https://discord.com/developers/docs/resources/channel#allowed-mentions-object | ||
*/ | ||
public function allowMentions(AllowedMentionsBuilder $allowedMentions): self | ||
{ | ||
$this->data['allowed_mentions'] = $allowedMentions->get(); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Disallow all mentions | ||
*/ | ||
public function noMentions(): self | ||
{ | ||
return $this->allowMentions(new AllowedMentionsBuilder()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace Exan\Dhp\Rest\Helpers\Channel\Message; | ||
|
||
use Exan\Dhp\Parts\Multipart; | ||
use Exan\Dhp\Parts\MultipartField; | ||
|
||
trait MultipartMessage | ||
{ | ||
public function getMultipart(): Multipart | ||
{ | ||
$fields = array_map(function ($fileData, int $index) { | ||
$headers = isset($fileData['type']) | ||
? ['Content-Type' => $fileData['type']] | ||
: []; | ||
|
||
return new MultipartField( | ||
'files[' . $index . ']', | ||
$fileData['content'], | ||
$fileData['name'], | ||
$headers | ||
); | ||
}, $this->files, array_keys($this->files)); | ||
|
||
$fields[] = new MultipartField( | ||
'payload_json', | ||
json_encode($this->get()), | ||
null, | ||
['Content-Type' => 'application/json'] | ||
); | ||
|
||
return new Multipart($fields); | ||
} | ||
|
||
public function requiresMultipart(): bool | ||
{ | ||
return $this->files !== []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Exan\Dhp\Rest\Helpers\Channel\Message; | ||
|
||
trait SetContent | ||
{ | ||
/** | ||
* @var string $content Up to 2000 characters | ||
*/ | ||
public function setContent(string $content): self | ||
{ | ||
$this->data['content'] = $content; | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace Exan\Dhp\Rest\Helpers\Channel\Message; | ||
|
||
trait SetFlags | ||
{ | ||
public function setFlags(int $flags): self | ||
{ | ||
$this->data['flags'] = $flags; | ||
return $this; | ||
} | ||
} |
Oops, something went wrong.