|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Kerox\Messenger\Model\Message\Attachment\Template; |
|
6
|
|
|
|
|
7
|
|
|
use Kerox\Messenger\Exception\InvalidClassException; |
|
8
|
|
|
use Kerox\Messenger\Model\Message\Attachment\AbstractTemplate; |
|
9
|
|
|
use Kerox\Messenger\Model\Message\Attachment\Template\Element\MediaElement; |
|
10
|
|
|
|
|
11
|
|
|
class MediaTemplate extends AbstractTemplate |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var \Kerox\Messenger\Model\Message\Attachment\Template\Element\MediaElement[] |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $elements; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* MediaTemplate constructor. |
|
20
|
|
|
* |
|
21
|
|
|
* @throws \Kerox\Messenger\Exception\MessengerException |
|
22
|
|
|
*/ |
|
23
|
1 |
|
public function __construct(array $elements) |
|
24
|
|
|
{ |
|
25
|
1 |
|
parent::__construct(); |
|
26
|
|
|
|
|
27
|
1 |
|
$this->isValidElements($elements); |
|
28
|
|
|
|
|
29
|
1 |
|
$this->elements = $elements; |
|
30
|
1 |
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
*@throws \Kerox\Messenger\Exception\MessengerException |
|
34
|
|
|
* |
|
35
|
|
|
* @return \Kerox\Messenger\Model\Message\Attachment\Template\MediaTemplate |
|
36
|
|
|
*/ |
|
37
|
1 |
|
public static function create(array $elements): self |
|
38
|
|
|
{ |
|
39
|
1 |
|
return new self($elements); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @throws \Kerox\Messenger\Exception\MessengerException |
|
44
|
|
|
*/ |
|
45
|
1 |
|
private function isValidElements(array $elements): void |
|
46
|
|
|
{ |
|
47
|
1 |
|
$this->isValidArray($elements, 1, 1); |
|
48
|
1 |
|
foreach ($elements as $element) { |
|
49
|
1 |
|
if (!$element instanceof MediaElement) { |
|
50
|
|
|
throw new InvalidClassException(sprintf('Array can only contain instance of %s.', MediaElement::class)); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
1 |
|
} |
|
54
|
|
|
|
|
55
|
1 |
|
public function toArray(): array |
|
56
|
|
|
{ |
|
57
|
1 |
|
$array = parent::toArray(); |
|
58
|
|
|
$array += [ |
|
59
|
|
|
'payload' => [ |
|
60
|
1 |
|
'template_type' => AbstractTemplate::TYPE_MEDIA, |
|
61
|
1 |
|
'elements' => $this->elements, |
|
62
|
|
|
], |
|
63
|
|
|
]; |
|
64
|
|
|
|
|
65
|
1 |
|
return $this->arrayFilter($array); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|