1 | <?php |
||
2 | /** |
||
3 | * @link https://dukt.net/videos/ |
||
4 | * @copyright Copyright (c) Dukt |
||
5 | * @license https://github.com/dukt/videos/blob/v2/LICENSE.md |
||
6 | */ |
||
7 | |||
8 | namespace dukt\videos\models; |
||
9 | |||
10 | use Craft; |
||
11 | use craft\base\Model; |
||
12 | use dukt\videos\base\Gateway; |
||
13 | use dukt\videos\helpers\VideosHelper; |
||
14 | use dukt\videos\Plugin as Videos; |
||
15 | use Twig_Markup; |
||
16 | |||
17 | /** |
||
18 | * Video model class. |
||
19 | * |
||
20 | * @author Dukt <[email protected]> |
||
21 | * @since 2.0 |
||
22 | * |
||
23 | * @property string $duration |
||
24 | * @property \dukt\videos\base\Gateway|null $gateway |
||
25 | */ |
||
26 | class Video extends Model |
||
27 | { |
||
28 | // Properties |
||
29 | // ========================================================================= |
||
30 | |||
31 | /** |
||
32 | * @var int|null The ID of the video |
||
33 | */ |
||
34 | public $id; |
||
35 | |||
36 | /** |
||
37 | * @var mixed|null The raw response object |
||
38 | */ |
||
39 | public $raw; |
||
40 | |||
41 | /** |
||
42 | * @var string|null The URL of the video |
||
43 | */ |
||
44 | public $url; |
||
45 | |||
46 | /** |
||
47 | * @var string|null The gateway’s handle |
||
48 | */ |
||
49 | public $gatewayHandle; |
||
50 | |||
51 | /** |
||
52 | * @var string|null The gateway’s name |
||
53 | */ |
||
54 | public $gatewayName; |
||
55 | |||
56 | /** |
||
57 | * @var \DateTime|null The date the video was uploaded |
||
58 | */ |
||
59 | public $date; |
||
60 | |||
61 | /** |
||
62 | * @var int|null The number of times the video has been played |
||
63 | */ |
||
64 | public $plays; |
||
65 | |||
66 | /** |
||
67 | * @var int|null Duration of the video in seconds |
||
68 | */ |
||
69 | public $durationSeconds; |
||
70 | |||
71 | /** |
||
72 | * @var int|null Duration of the video in ISO 8601 format |
||
73 | */ |
||
74 | public $duration8601; |
||
75 | |||
76 | /** |
||
77 | * @var string|null The author’s name |
||
78 | */ |
||
79 | public $authorName; |
||
80 | |||
81 | /** |
||
82 | * @var string|null The author’s URL |
||
83 | */ |
||
84 | public $authorUrl; |
||
85 | |||
86 | /** |
||
87 | * @var string|null The author’s username |
||
88 | */ |
||
89 | public $authorUsername; |
||
90 | |||
91 | /** |
||
92 | * @var string|null The thumbnail’s source |
||
93 | */ |
||
94 | public $thumbnailSource; |
||
95 | |||
96 | /** |
||
97 | * @var string|null The thumbnail’s large source |
||
98 | * @deprecated in 2.1. Use [[\dukt\videos\models\Video::$thumbnailSource]] instead. |
||
99 | */ |
||
100 | public $thumbnailLargeSource; |
||
101 | |||
102 | /** |
||
103 | * @var string|null The video’s title |
||
104 | */ |
||
105 | public $title; |
||
106 | |||
107 | /** |
||
108 | * @var string|null The video’s description |
||
109 | */ |
||
110 | public $description; |
||
111 | |||
112 | /** |
||
113 | * @var bool Is this video private? |
||
114 | */ |
||
115 | public $private = false; |
||
116 | |||
117 | /** |
||
118 | * @var int|null The video’s width |
||
119 | */ |
||
120 | public $width; |
||
121 | |||
122 | /** |
||
123 | * @var int|null The video’s height |
||
124 | */ |
||
125 | public $height; |
||
126 | |||
127 | /** |
||
128 | * @var Gateway|null Gateway |
||
129 | */ |
||
130 | private $_gateway; |
||
131 | |||
132 | // Public Methods |
||
133 | // ========================================================================= |
||
134 | |||
135 | /** |
||
136 | * Get the video’s duration. |
||
137 | * |
||
138 | * @return string |
||
139 | */ |
||
140 | public function getDuration(): string |
||
141 | { |
||
142 | return VideosHelper::getDuration($this->durationSeconds); |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Get the video’s embed. |
||
147 | * |
||
148 | * @param array $opts |
||
149 | * |
||
150 | * @return Twig_Markup |
||
151 | * @throws \yii\base\InvalidConfigException |
||
152 | */ |
||
153 | public function getEmbed(array $opts = []): Twig_Markup |
||
154 | { |
||
155 | $embed = $this->getGateway()->getEmbedHtml($this->id, $opts); |
||
156 | $charset = Craft::$app->getView()->getTwig()->getCharset(); |
||
157 | |||
158 | return new Twig_Markup($embed, $charset); |
||
0 ignored issues
–
show
Deprecated Code
introduced
by
![]() |
|||
159 | } |
||
160 | |||
161 | /** |
||
162 | * Get the video’s embed URL. |
||
163 | * |
||
164 | * @param array $opts |
||
165 | * |
||
166 | * @return null|string |
||
167 | * @throws \yii\base\InvalidConfigException |
||
168 | */ |
||
169 | public function getEmbedUrl(array $opts = []): ?string |
||
170 | { |
||
171 | $gateway = $this->getGateway(); |
||
172 | |||
173 | if (!$gateway instanceof \dukt\videos\base\Gateway) { |
||
174 | return null; |
||
175 | } |
||
176 | |||
177 | return $gateway->getEmbedUrl($this->id, $opts); |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Get the video’s gateway. |
||
182 | * |
||
183 | * @return Gateway|null |
||
184 | * @throws \yii\base\InvalidConfigException |
||
185 | */ |
||
186 | public function getGateway(): ?\dukt\videos\base\Gateway |
||
187 | { |
||
188 | if (!$this->_gateway instanceof \dukt\videos\base\Gateway) { |
||
189 | $this->_gateway = Videos::$plugin->getGateways()->getGateway($this->gatewayHandle); |
||
190 | } |
||
191 | |||
192 | return $this->_gateway; |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * Get the video’s thumbnail. |
||
197 | * |
||
198 | * |
||
199 | * @return null|string |
||
200 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
201 | * @throws \craft\errors\ImageException |
||
202 | * @throws \yii\base\Exception |
||
203 | * @throws \yii\base\InvalidConfigException |
||
204 | */ |
||
205 | public function getThumbnail(int $size = 300): ?string |
||
206 | { |
||
207 | return VideosHelper::getVideoThumbnail($this->gatewayHandle, $this->id, $size); |
||
208 | } |
||
209 | } |
||
210 |