|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace BNETDocs\Libraries\Discord; |
| 4 | + |
| 5 | +use \BNETDocs\Libraries\Discord\EmbedAuthor; |
| 6 | +use \BNETDocs\Libraries\Discord\EmbedField; |
| 7 | +use \BNETDocs\Libraries\Discord\EmbedFooter; |
| 8 | +use \BNETDocs\Libraries\Discord\EmbedImage; |
| 9 | +use \BNETDocs\Libraries\Discord\EmbedProvider; |
| 10 | +use \BNETDocs\Libraries\Discord\EmbedThumbnail; |
| 11 | +use \BNETDocs\Libraries\Discord\EmbedVideo; |
| 12 | + |
| 13 | +use \DateTime; |
| 14 | +use \JsonSerializable; |
| 15 | +use \LengthException; |
| 16 | +use \OverflowException; |
| 17 | +use \SplObjectStorage; |
| 18 | + |
| 19 | +// <https://discordapp.com/developers/docs/resources/channel#embed-object> |
| 20 | + |
| 21 | +class Embed implements JsonSerializable { |
| 22 | + |
| 23 | + const MAX_DESCRIPTION = 2048; |
| 24 | + const MAX_FIELDS = 25; |
| 25 | + const MAX_TITLE = 256; |
| 26 | + |
| 27 | + protected $author; |
| 28 | + protected $color; |
| 29 | + protected $description; |
| 30 | + protected $fields; |
| 31 | + protected $footer; |
| 32 | + protected $image; |
| 33 | + protected $provider; |
| 34 | + protected $thumbnail; |
| 35 | + protected $timestamp; |
| 36 | + protected $title; |
| 37 | + protected $type; |
| 38 | + protected $url; |
| 39 | + protected $video; |
| 40 | + |
| 41 | + public function __construct() { |
| 42 | + $this->author = null; |
| 43 | + $this->color = -1; |
| 44 | + $this->description = ''; |
| 45 | + $this->fields = new SplObjectStorage(); |
| 46 | + $this->footer = null; |
| 47 | + $this->image = null; |
| 48 | + $this->provider = null; |
| 49 | + $this->thumbnail = null; |
| 50 | + $this->timestamp = null; |
| 51 | + $this->title = ''; |
| 52 | + $this->type = 'rich'; |
| 53 | + $this->url = ''; |
| 54 | + $this->video = null; |
| 55 | + } |
| 56 | + |
| 57 | + public function addField(EmbedField &$field_object) { |
| 58 | + if ($this->fields->count() >= self::MAX_FIELDS) { |
| 59 | + throw new OverflowException(sprintf( |
| 60 | + 'Discord forbids adding more than %d fields', |
| 61 | + self::MAX_FIELDS |
| 62 | + )); |
| 63 | + } |
| 64 | + |
| 65 | + $this->fields->attach($field_object); |
| 66 | + } |
| 67 | + |
| 68 | + public function fieldCount() { |
| 69 | + return $this->fields->count(); |
| 70 | + } |
| 71 | + |
| 72 | + public function hasField(EmbedField &$field_object) { |
| 73 | + return $this->fields->contains($field_object); |
| 74 | + } |
| 75 | + |
| 76 | + public function jsonSerialize() { |
| 77 | + // part of JsonSerializable interface |
| 78 | + $r = array(); |
| 79 | + |
| 80 | + if (!empty($this->description)) $r['description'] = $this->description; |
| 81 | + if (!empty($this->title)) $r['title'] = $this->title; |
| 82 | + if (!empty($this->type)) $r['type'] = $this->type; |
| 83 | + if (!empty($this->url)) $r['url'] = $this->url; |
| 84 | + if ($this->author) $r['author'] = $this->author; |
| 85 | + if ($this->color > -1) $r['color'] = $this->color; |
| 86 | + if ($this->fields->count() > 0) $r['fields'] = array(); |
| 87 | + if ($this->footer) $r['footer'] = $this->footer; |
| 88 | + if ($this->image) $r['image'] = $this->image; |
| 89 | + if ($this->provider) $r['provider'] = $this->provider; |
| 90 | + if ($this->thumbnail) $r['thumbnail'] = $this->thumbnail; |
| 91 | + if ($this->video) $r['video'] = $this->video; |
| 92 | + |
| 93 | + if ($this->timestamp) $r['timestamp'] = $this->timestamp->format( |
| 94 | + DateTime::ISO8601 |
| 95 | + ); |
| 96 | + |
| 97 | + foreach ($this->fields as $field_object) { |
| 98 | + $r['fields'][] = $field_object; |
| 99 | + } |
| 100 | + |
| 101 | + return $r; |
| 102 | + } |
| 103 | + |
| 104 | + public function removeAllFields() { |
| 105 | + $this->fields = new SplObjectStorage(); |
| 106 | + } |
| 107 | + |
| 108 | + public function removeField(EmbedField &$field_object) { |
| 109 | + $this->fields->detach($field_object); |
| 110 | + } |
| 111 | + |
| 112 | + public function setAuthor(EmbedAuthor &$author_object) { |
| 113 | + $this->author = $author_object; |
| 114 | + } |
| 115 | + |
| 116 | + public function setColor(int $color) { |
| 117 | + $this->color = $color; |
| 118 | + } |
| 119 | + |
| 120 | + public function setDescription(string $description) { |
| 121 | + if (strlen($description) > self::MAX_DESCRIPTION) { |
| 122 | + throw new LengthException(sprintf( |
| 123 | + 'Discord forbids description longer than %d characters', |
| 124 | + self::MAX_DESCRIPTION |
| 125 | + )); |
| 126 | + } |
| 127 | + |
| 128 | + $this->title = $title; |
| 129 | + } |
| 130 | + |
| 131 | + public function setFooter(EmbedFooter &$footer_object) { |
| 132 | + $this->footer = $footer_object; |
| 133 | + } |
| 134 | + |
| 135 | + public function setImage(EmbedImage &$image_object) { |
| 136 | + $this->image = $image_object; |
| 137 | + } |
| 138 | + |
| 139 | + public function setProvider(EmbedProvider &$provider_object) { |
| 140 | + $this->provider = $provider_object; |
| 141 | + } |
| 142 | + |
| 143 | + public function setThumbnail(EmbedThumbnail &$thumbnail_object) { |
| 144 | + $this->thumbnail = $thumbnail_object; |
| 145 | + } |
| 146 | + |
| 147 | + public function setTimestamp(DateTime $timestamp) { |
| 148 | + $this->timestamp = $timestamp; |
| 149 | + } |
| 150 | + |
| 151 | + public function setTitle(string $title) { |
| 152 | + if (strlen($title) > self::MAX_TITLE) { |
| 153 | + throw new LengthException(sprintf( |
| 154 | + 'Discord forbids title longer than %d characters', |
| 155 | + self::MAX_TITLE |
| 156 | + )); |
| 157 | + } |
| 158 | + |
| 159 | + $this->title = $title; |
| 160 | + } |
| 161 | + |
| 162 | + public function setType(string $type) { |
| 163 | + $this->type = $type; |
| 164 | + } |
| 165 | + |
| 166 | + public function setUrl(string $url) { |
| 167 | + $this->url = $url; |
| 168 | + } |
| 169 | + |
| 170 | + public function setVideo(EmbedVideo &$video_object) { |
| 171 | + $this->video = $video_object; |
| 172 | + } |
| 173 | + |
| 174 | +} |
0 commit comments