1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Kerox\Messenger\Model\Message\Attachment\Template\Element; |
6
|
|
|
|
7
|
|
|
use Kerox\Messenger\Exception\InvalidTypeException; |
8
|
|
|
use Kerox\Messenger\Helper\ValidatorTrait; |
9
|
|
|
use Kerox\Messenger\Model\Common\Button\AbstractButton; |
10
|
|
|
|
11
|
|
|
class MediaElement implements \JsonSerializable |
12
|
|
|
{ |
13
|
|
|
use ValidatorTrait; |
14
|
|
|
|
15
|
|
|
public const TYPE_IMAGE = 'image'; |
16
|
|
|
public const TYPE_VIDEO = 'video'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $mediaType; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string|null |
25
|
|
|
*/ |
26
|
|
|
protected $attachmentId; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string|null |
30
|
|
|
*/ |
31
|
|
|
protected $url; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var \Kerox\Messenger\Model\Common\Button\AbstractButton[]|null |
35
|
|
|
*/ |
36
|
|
|
protected $buttons; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* MediaElement constructor. |
40
|
|
|
* |
41
|
|
|
* @throws \Kerox\Messenger\Exception\MessengerException |
42
|
|
|
*/ |
43
|
3 |
|
public function __construct(string $url, string $mediaType = self::TYPE_IMAGE) |
44
|
|
|
{ |
45
|
3 |
|
if ($this->isAttachmentId($url)) { |
46
|
|
|
$this->attachmentId = $url; |
47
|
|
|
} else { |
48
|
3 |
|
$this->isValidUrl($url); |
49
|
3 |
|
$this->url = $url; |
50
|
|
|
} |
51
|
|
|
|
52
|
3 |
|
$this->isValidMediaType($mediaType); |
53
|
2 |
|
$this->mediaType = $mediaType; |
54
|
2 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @throws \Kerox\Messenger\Exception\MessengerException |
58
|
|
|
* |
59
|
|
|
* @return \Kerox\Messenger\Model\Message\Attachment\Template\Element\MediaElement |
60
|
|
|
*/ |
61
|
3 |
|
public static function create(string $url, string $mediaType = self::TYPE_IMAGE): self |
62
|
|
|
{ |
63
|
3 |
|
return new self($url, $mediaType); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param \Kerox\Messenger\Model\Common\Button\AbstractButton[] $buttons |
68
|
|
|
* |
69
|
|
|
* @throws \Kerox\Messenger\Exception\MessengerException |
70
|
|
|
* |
71
|
|
|
* @return \Kerox\Messenger\Model\Message\Attachment\Template\Element\MediaElement |
72
|
|
|
*/ |
73
|
2 |
|
public function setButtons(array $buttons): self |
74
|
|
|
{ |
75
|
2 |
|
$this->isValidArray($buttons, 1); |
76
|
2 |
|
$this->isValidButtons($buttons, $this->getAllowedButtonsType()); |
77
|
|
|
|
78
|
1 |
|
$this->buttons = $buttons; |
79
|
|
|
|
80
|
1 |
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
protected function getAllowedButtonsType(): array |
84
|
|
|
{ |
85
|
|
|
return [ |
86
|
2 |
|
AbstractButton::TYPE_WEB_URL, |
87
|
|
|
]; |
88
|
|
|
} |
89
|
|
|
|
90
|
3 |
|
private function isAttachmentId(string $value): bool |
91
|
|
|
{ |
92
|
3 |
|
return (bool) preg_match('/^[\d]+$/', $value); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @throws \Kerox\Messenger\Exception\MessengerException |
97
|
|
|
*/ |
98
|
3 |
|
private function isValidMediaType(string $mediaType): void |
99
|
|
|
{ |
100
|
3 |
|
$allowedMediaType = $this->getAllowedMediaType(); |
101
|
3 |
|
if (!\in_array($mediaType, $allowedMediaType, true)) { |
102
|
1 |
|
throw new InvalidTypeException(sprintf('mediaType must be either "%s".', implode(', ', $allowedMediaType))); |
103
|
|
|
} |
104
|
2 |
|
} |
105
|
|
|
|
106
|
3 |
|
protected function getAllowedMediaType(): array |
107
|
|
|
{ |
108
|
|
|
return [ |
109
|
3 |
|
self::TYPE_IMAGE, |
110
|
3 |
|
self::TYPE_VIDEO, |
111
|
|
|
]; |
112
|
|
|
} |
113
|
|
|
|
114
|
1 |
|
public function toArray(): array |
115
|
|
|
{ |
116
|
|
|
$array = [ |
117
|
1 |
|
'media_type' => $this->mediaType, |
118
|
1 |
|
'attachment_id' => $this->attachmentId, |
119
|
1 |
|
'url' => $this->url, |
120
|
1 |
|
'buttons' => $this->buttons, |
121
|
|
|
]; |
122
|
|
|
|
123
|
1 |
|
return array_filter($array); |
124
|
|
|
} |
125
|
|
|
|
126
|
1 |
|
public function jsonSerialize(): array |
127
|
|
|
{ |
128
|
1 |
|
return $this->toArray(); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|