Skip to content

Commit bd46faa

Browse files
authored
Merge pull request #11 from sebbmeyer/feature/custom-card
Adding custom card
2 parents 90cacd6 + 53d53d8 commit bd46faa

File tree

2 files changed

+275
-1
lines changed

2 files changed

+275
-1
lines changed

README.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,20 @@ $connector->send($card);
2626

2727
### Custom card
2828

29-
You can create your own cards for every purpose you need, just extend the **AbstractCard** class and implement the `getMessage()` function. This is an example of a [Laravel Forge deployment card](https://github.com/sebbmeyer/laravel-teams-connector)
29+
You can use the provided **CustomCard** class and add a color, facts, images, an activity, actions or a summary to it.
30+
31+
```php
32+
// create a custom card
33+
$card = new \Sebbmyr\Teams\Cards\CustomCard('Package update', 'A custom card class was added to the package.');
34+
// add information
35+
$card->setColor('01BC36')
36+
->addFactsText('Supported PHP versions',['<= 5.4.0','7.x'])
37+
->addFactsText('Unsupported PHP versions',['Before Version 5.4'])
38+
->addAction('Visit Github repository','https://github.com/sebbmeyer/php-microsoft-teams-connector')
39+
->addFacts('Facts Section',['Fact Name 1' => 'Fact Value 1','Fact Name 2' => 'Fact Value 2']);
40+
```
41+
42+
Or you can create your own cards for every purpose you need, just extend the **AbstractCard** class and implement the `getMessage()` function. This is an example of a [Laravel Forge deployment card](https://github.com/sebbmeyer/laravel-teams-connector)
3043

3144
```php
3245
\\ Sebbmyr\LaravelTeams\Cards\ForgeCard.php

src/Cards/CustomCard.php

+261
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,261 @@
1+
<?php
2+
3+
namespace Sebbmyr\Teams\Cards;
4+
5+
use Sebbmyr\Teams\TeamsConnectorInterface;
6+
7+
/**
8+
* Sebbmyr\Teams\Cards\CustomCard
9+
*/
10+
class CustomCard implements TeamsConnectorInterface
11+
{
12+
/**
13+
* Theme color
14+
* @var string
15+
*/
16+
private $color;
17+
18+
/**
19+
* Summary
20+
* @var string
21+
*/
22+
private $summary;
23+
24+
/**
25+
* Title
26+
* @var string
27+
*/
28+
private $title;
29+
30+
/**
31+
* Generic text
32+
* @var string
33+
*/
34+
private $text;
35+
36+
/**
37+
* Action Buttons
38+
* @var array
39+
*/
40+
private $potentialAction;
41+
42+
/**
43+
* Sections
44+
* @var array
45+
*/
46+
private $sections;
47+
48+
/**
49+
* CustomCard constructor.
50+
* @param null $title
51+
* @param null $text
52+
*/
53+
public function __construct($title = null, $text = null)
54+
{
55+
$this->title = $title ?: '';
56+
$this->text = $text ?: '';
57+
}
58+
59+
/**
60+
* Formats data for API call
61+
*/
62+
public function getMessage()
63+
{
64+
$message = [
65+
"@type" => "MessageCard",
66+
"@context" => "http://schema.org/extensions",
67+
];
68+
if (isset($this->summary)) {
69+
$message['summary'] = $this->summary;
70+
}
71+
if (isset($this->title)) {
72+
$message['title'] = $this->title;
73+
}
74+
if (isset($this->text)) {
75+
$message['text'] = $this->text;
76+
}
77+
if (isset($this->color)) {
78+
$message['themeColor'] = $this->color;
79+
}
80+
81+
if (isset($this->sections)) {
82+
$message['sections'] = $this->sections;
83+
}
84+
if (isset($this->potentialAction)) {
85+
$message['potentialAction'] = $this->potentialAction;
86+
}
87+
88+
return $message;
89+
}
90+
91+
/**
92+
* Sets Card Title
93+
*
94+
* @param string $title
95+
* @return CustomCard
96+
*/
97+
public function setTitle(string $title)
98+
{
99+
$this->title = $title;
100+
101+
return $this;
102+
}
103+
104+
/**
105+
* Sets Card Text
106+
*
107+
* @param string $text
108+
* @return CustomCard
109+
*/
110+
public function setText(string $text)
111+
{
112+
$this->text = $text;
113+
114+
return $this;
115+
}
116+
117+
/**
118+
* Sets Card Summary
119+
*
120+
* @param string $summary
121+
* @return CustomCard
122+
*/
123+
public function setSummary(string $summary)
124+
{
125+
$this->summary = $summary;
126+
127+
return $this;
128+
}
129+
130+
/**
131+
* Sets Card Color
132+
*
133+
* @param string $color
134+
* @return CustomCard
135+
*/
136+
public function setColor(string $color)
137+
{
138+
$this->color = $color;
139+
140+
return $this;
141+
}
142+
143+
/**
144+
* Adds activity section to card
145+
*
146+
* @param string $text
147+
* @param string|null $title
148+
* @param string|null $image
149+
* @return CustomCard
150+
*/
151+
public function addActivity(string $text, string $title = null, string $image = null)
152+
{
153+
$activity = ['activityTitle' => $title];
154+
if ($text !== null) {
155+
$activity['activityText'] = $text;
156+
}
157+
if ($image !== null) {
158+
$activity['activityImage'] = $image;
159+
}
160+
$this->sections[] = $activity;
161+
162+
return $this;
163+
}
164+
165+
/**
166+
* Adds text section to card
167+
*
168+
* @param string $title
169+
* @param array|null $array
170+
* @return CustomCard
171+
*/
172+
public function addFactsText(string $title, array $array = null)
173+
{
174+
$section = ['title' => $title];
175+
if (!is_null($array)) {
176+
$facts = [];
177+
foreach ($array as $arr) {
178+
$facts[] = ['name' => $arr];
179+
}
180+
$section['facts'] = $facts;
181+
}
182+
$this->sections[] = $section;
183+
184+
return $this;
185+
}
186+
187+
188+
/**
189+
* Adds facts section to card
190+
*
191+
* @param string $title
192+
* @param array $array
193+
* @return CustomCard
194+
*/
195+
public function addFacts(string $title, array $array)
196+
{
197+
$section = ['title' => $title];
198+
$facts = [];
199+
foreach ($array as $name => $value) {
200+
$facts[] = ['name' => $name, 'value' => $value];
201+
}
202+
$section['facts'] = $facts;
203+
$this->sections[] = $section;
204+
205+
return $this;
206+
}
207+
208+
209+
/**
210+
* Adds single image to card
211+
*
212+
* @param string $title
213+
* @param string $image
214+
* @return CustomCard
215+
*/
216+
public function addImage(string $title, string $image)
217+
{
218+
$this->addImages($title, [$image]);
219+
220+
return $this;
221+
}
222+
223+
/**
224+
* Adds images to card
225+
*
226+
* @param string $title
227+
* @param array $images
228+
* @return CustomCard
229+
*/
230+
public function addImages(string $title, array $images)
231+
{
232+
$section = ['title' => $title];
233+
$sectionImages = [];
234+
foreach ($images as $image) {
235+
$sectionImages[] = ['image' => $image];
236+
}
237+
$section['images'] = $sectionImages;
238+
$this->sections[] = $section;
239+
240+
return $this;
241+
}
242+
243+
/**
244+
* Adds action button to card
245+
*
246+
* @param string $text
247+
* @param string $url
248+
* @return CustomCard
249+
*/
250+
public function addAction(string $text, string $url)
251+
{
252+
$this->potentialAction[] = [
253+
'@context' => 'http://schema.org',
254+
'@type' => 'ViewAction',
255+
'name' => $text,
256+
'target' => [$url],
257+
];
258+
259+
return $this;
260+
}
261+
}

0 commit comments

Comments
 (0)