Passed
Pull Request — master (#83)
by Romain
03:05
created

MediaElement::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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