Skip to content

Commit 6d88e58

Browse files
committed
Update for PHP7; Add Embed support
Updated for PHP v7 support. Added Discord Embed for PHP v7 and v7.3.
1 parent ccfff75 commit 6d88e58

4 files changed

+684
-61
lines changed

lib/DiscordEmbed-v7.0.php

+178
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
<?php
2+
3+
/**
4+
* Discord Embed is a part of DiscordWebhook library.
5+
*/
6+
final class DiscordEmbed
7+
{
8+
protected $title;
9+
protected $type = "rich";
10+
protected $description;
11+
protected $url;
12+
protected $timestamp;
13+
protected $color;
14+
protected $footer;
15+
protected $image;
16+
protected $thumbnail;
17+
protected $video;
18+
protected $provider;
19+
protected $author;
20+
protected $fields;
21+
22+
/**
23+
* Set the title and the url
24+
*
25+
* @param string $title
26+
* @param string $url
27+
* @return self|null
28+
*/
29+
function setTitle($title, $url = "") {
30+
$this->title = $title;
31+
$this->url = $url;
32+
33+
return $this;
34+
}
35+
36+
/**
37+
* Set description
38+
*
39+
* @param string $description
40+
* @return self
41+
*/
42+
function setDescription($description) {
43+
$this->description = $description;
44+
45+
return $this;
46+
}
47+
/**
48+
* Set timestamp of the embed
49+
*
50+
* @param [type] $timestamp
51+
* @return self
52+
*/
53+
function setTimestamp($timestamp) {
54+
$this->timestamp = $timestamp;
55+
56+
return $this;
57+
}
58+
/**
59+
* Set border color
60+
*
61+
* @param string $color
62+
* @return self
63+
*/
64+
function setColor($color) {
65+
$this->color = is_int($color) ? $color : hexdec($color);
66+
67+
return $this;
68+
}
69+
/**
70+
* Set url
71+
*
72+
* @param string $url
73+
* @return self
74+
*/
75+
function setUrl($url) {
76+
$this->url = $url;
77+
78+
return $this;
79+
}
80+
/**
81+
* Set footer
82+
*
83+
* @param string $text
84+
* @param string $icon_url
85+
* @return self
86+
*/
87+
function setFooter($text, $icon_url = "") {
88+
$this->footer = [
89+
"text" => $text,
90+
"icon_url" => $icon_url,
91+
];
92+
93+
return $this;
94+
}
95+
/**
96+
* Set image
97+
*
98+
* @param string $url
99+
* @return self
100+
*/
101+
function setImage($url) {
102+
$this->image = [
103+
"url" => $url,
104+
];
105+
106+
return $this;
107+
}
108+
/**
109+
* Set thumbnail
110+
*
111+
* @param string $url
112+
* @return self
113+
*/
114+
function setThumbnail($url) {
115+
$this->thumbnail = [
116+
"url" => $url,
117+
];
118+
119+
return $this;
120+
}
121+
/**
122+
* Set author
123+
*
124+
* @param string $name
125+
* @param string $url
126+
* @param string $icon_url
127+
* @return self
128+
*/
129+
function setAuthor($name, $url = "", $icon_url = "") {
130+
$this->author = [
131+
"name" => $name,
132+
"url" => $url,
133+
"icon_url" => $icon_url,
134+
];
135+
136+
return $this;
137+
}
138+
/**
139+
* Set field
140+
*
141+
* @param string $name
142+
* @param string $value
143+
* @param boolean $inline
144+
* @return self
145+
*/
146+
function setField($name, $value = "", $inline = false) {
147+
$this->fields[] = [
148+
'name' => $name,
149+
'value' => $value,
150+
'inline' => boolval($inline),
151+
];
152+
153+
return $this;
154+
}
155+
/**
156+
* Get fields as an array
157+
*
158+
* @return array
159+
*/
160+
function toArray() {
161+
return [
162+
'title' => $this->title,
163+
'type' => $this->type,
164+
'description' => $this->description,
165+
'url' => $this->url,
166+
'color' => $this->color,
167+
'footer' => $this->footer,
168+
'image' => $this->image,
169+
'thumbnail' => $this->thumbnail,
170+
'timestamp' => $this->timestamp,
171+
'author' => $this->author,
172+
'fields' => $this->fields
173+
];
174+
}
175+
176+
177+
}
178+
?>

lib/DiscordEmbed.php

+178
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
<?php
2+
3+
/**
4+
* Discord Embed is a part of DiscordWebhook library.
5+
*/
6+
final class DiscordEmbed
7+
{
8+
protected $title;
9+
protected $type = "rich";
10+
protected $description;
11+
protected $url;
12+
protected $timestamp;
13+
protected $color;
14+
protected $footer;
15+
protected $image;
16+
protected $thumbnail;
17+
protected $video;
18+
protected $provider;
19+
protected $author;
20+
protected $fields;
21+
22+
/**
23+
* Set the title and the url
24+
*
25+
* @param string $title
26+
* @param string $url
27+
* @return self|null
28+
*/
29+
function setTitle(string $title, string $url = ""): self {
30+
$this->title = $title;
31+
$this->url = $url;
32+
33+
return $this;
34+
}
35+
36+
/**
37+
* Set description
38+
*
39+
* @param string $description
40+
* @return self
41+
*/
42+
function setDescription(string $description): self {
43+
$this->description = $description;
44+
45+
return $this;
46+
}
47+
/**
48+
* Set timestamp of the embed
49+
*
50+
* @param [type] $timestamp
51+
* @return self
52+
*/
53+
function setTimestamp($timestamp): self {
54+
$this->timestamp = $timestamp;
55+
56+
return $this;
57+
}
58+
/**
59+
* Set border color
60+
*
61+
* @param string $color
62+
* @return self
63+
*/
64+
function setColor(string $color): self {
65+
$this->color = is_int($color) ? $color : hexdec($color);
66+
67+
return $this;
68+
}
69+
/**
70+
* Set url
71+
*
72+
* @param string $url
73+
* @return self
74+
*/
75+
function setUrl(string $url): self {
76+
$this->url = $url;
77+
78+
return $this;
79+
}
80+
/**
81+
* Set footer
82+
*
83+
* @param string $text
84+
* @param string $icon_url
85+
* @return self
86+
*/
87+
function setFooter(string $text, string $icon_url = ""): self {
88+
$this->footer = [
89+
"text" => $text,
90+
"icon_url" => $icon_url,
91+
];
92+
93+
return $this;
94+
}
95+
/**
96+
* Set image
97+
*
98+
* @param string $url
99+
* @return self
100+
*/
101+
function setImage(string $url): self {
102+
$this->image = [
103+
"url" => $url,
104+
];
105+
106+
return $this;
107+
}
108+
/**
109+
* Set thumbnail
110+
*
111+
* @param string $url
112+
* @return self
113+
*/
114+
function setThumbnail(string $url): self {
115+
$this->thumbnail = [
116+
"url" => $url,
117+
];
118+
119+
return $this;
120+
}
121+
/**
122+
* Set author
123+
*
124+
* @param string $name
125+
* @param string $url
126+
* @param string $icon_url
127+
* @return self
128+
*/
129+
function setAuthor(string $name, string $url = "", string $icon_url = ""): self {
130+
$this->author = [
131+
"name" => $name,
132+
"url" => $url,
133+
"icon_url" => $icon_url,
134+
];
135+
136+
return $this;
137+
}
138+
/**
139+
* Set field
140+
*
141+
* @param string $name
142+
* @param string $value
143+
* @param boolean $inline
144+
* @return self
145+
*/
146+
function setField(string $name, string $value = "", bool $inline = false): self {
147+
$this->fields[] = [
148+
'name' => $name,
149+
'value' => $value,
150+
'inline' => boolval($inline),
151+
];
152+
153+
return $this;
154+
}
155+
/**
156+
* Get fields as an array
157+
*
158+
* @return array
159+
*/
160+
function toArray(): array {
161+
return [
162+
'title' => $this->title,
163+
'type' => $this->type,
164+
'description' => $this->description,
165+
'url' => $this->url,
166+
'color' => $this->color,
167+
'footer' => $this->footer,
168+
'image' => $this->image,
169+
'thumbnail' => $this->thumbnail,
170+
'timestamp' => $this->timestamp,
171+
'author' => $this->author,
172+
'fields' => $this->fields
173+
];
174+
}
175+
176+
177+
}
178+
?>

0 commit comments

Comments
 (0)