Passed
Push — master ( 07050b...36b6b2 )
by Paweł
03:00
created

Video::toArray()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 9
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 16
ccs 9
cts 9
cp 1
crap 7
rs 8.8333
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Wszetko Sitemap.
7
 *
8
 * (c) Paweł Kłopotek-Główczewski <[email protected]>
9
 *
10
 * This source file is subject to the MIT license that is bundled
11
 * with this source code in the file LICENSE.
12
 */
13
14
namespace Wszetko\Sitemap\Items;
15
16
use InvalidArgumentException as InvalidArgumentException;
17
use Wszetko\Sitemap\Traits\DateTime;
18
use Wszetko\Sitemap\Traits\IsAssoc;
19
20
/**
21
 * Class Video.
22
 *
23
 * @todo    : add support for tvshow tag
24
 *
25
 * @package Wszetko\Sitemap\Items
26
 *
27
 * @method setThumbnailLoc($thumbnail)
28
 * @method getThumbnailLoc()
29
 * @method setTitle($title)
30
 * @method getTitle()
31
 * @method setDescription($description)
32
 * @method getDescription()
33
 * @method setRating($rating)
34
 * @method getRating()
35
 * @method setViewCount($viewCount)
36
 * @method getViewCount()
37
 * @method setExpirationDate($expirationDate)
38
 * @method getExpirationDate()
39
 * @method setPublicationDate($publicationDate)
40
 * @method getPublicationDate()
41
 * @method setDuration($duration)
42
 * @method getDuration()
43
 * @method setGalleryLoc($galleryLoc)
44
 * @method getGalleryLoc()
45
 * @method setCategory($category)
46
 * @method getCategory()
47
 * @method setLive($live)
48
 * @method getLive()
49
 * @method setRequiresSubscription($subscription)
50
 * @method getRequiresSubscription()
51
 * @method setFamilyFriendly($familyFriendly)
52
 * @method getFamilyFriendly()
53
 * @method setContentLoc($contentLoc)
54
 * @method getContentLoc()
55
 * @method setPlayerLoc($player, $allow_embed = null, $autoplay = null)
56
 * @method getPlayerLoc()
57
 * @method addTag($tags)
58
 * @method setTag($tag)
59
 * @method getTag()
60
 * @method setUploader($uploader, $info = null)
61
 * @method getUploader()
62
 * @method setRestriction($countries, $relationship)
63
 * @method getRestriction()
64
 * @method setPlatform($platform, $relationship)
65
 * @method getPlatform()
66
 * @method setPrice($price, $currency, $type = null, $resolution = null)
67
 * @method addPrice($price, $currency, $type = null, $resolution = null)
68
 * @method getPrice()
69
 * @method setId($id, $type)
70
 * @method addId($id, $type)
71
 * @method getId()
72
 * @method setContentSegmentLoc($url, $duration = null)
73
 * @method addContentSegmentLoc($url, $duration = null)
74
 * @method getContentSegmentLoc()
75
 */
76
class Video extends Extension
77
{
78
    use DateTime;
79
    use IsAssoc;
80
81
    /**
82
     * Name of Namescapce.
83
     */
84
    public const NAMESPACE_NAME = 'video';
85
86
    /**
87
     * Namespace URL.
88
     */
89
    public const NAMESPACE_URL = 'http://www.google.com/schemas/sitemap-video/1.1';
90
91
    /**
92
     * Element name.
93
     */
94
    public const ELEMENT_NAME = 'video';
95
96
    /**
97
     * URL pointing to an image thumbnail.
98
     *
99
     * @var \Wszetko\Sitemap\Items\DataTypes\URLType
100
     */
101
    protected $thumbnailLoc;
102
103
    /**
104
     * Title of the video, max 100 characters.
105
     *
106
     * @var \Wszetko\Sitemap\Items\DataTypes\StringType
107
     */
108
    protected $title;
109
110
    /**
111
     * Description of the video, max 2048 characters.
112
     *
113
     * @var \Wszetko\Sitemap\Items\DataTypes\StringType
114
     */
115
    protected $description;
116
117
    /**
118
     * URL pointing to the actual media file (mp4).
119
     *
120
     * @var \Wszetko\Sitemap\Items\DataTypes\URLType
121
     */
122
    protected $contentLoc;
123
124
    /**
125
     * URL pointing to the player file (normally a SWF).
126
     *
127
     * @attribute allow_embed
128
     * @attributeDataType \Wszetko\Sitemap\Items\DataTypes\YesNoType
129
     * @attribute autoplay
130
     * @attributeDataType \Wszetko\Sitemap\Items\DataTypes\StringType
131
     *
132
     * @var \Wszetko\Sitemap\Items\DataTypes\URLType
133
     */
134
    protected $playerLoc;
135
136
    /**
137
     * @dataType \Wszetko\Sitemap\Items\DataTypes\URLType
138
     * @attribute duration
139
     * @attributeDataType \Wszetko\Sitemap\Items\DataTypes\IntegerType
140
     *
141
     * @var \Wszetko\Sitemap\Items\DataTypes\ArrayType
142
     */
143
    protected $contentSegmentLoc;
144
145
    /**
146
     * Indicates whether the video is live.
147
     *
148
     * @var \Wszetko\Sitemap\Items\DataTypes\YesNoType
149
     */
150
    protected $live;
151
152
    /**
153
     * Duration of the video in seconds.
154
     *
155
     * @var \Wszetko\Sitemap\Items\DataTypes\IntegerType
156
     */
157
    protected $duration;
158
159
    /**
160
     * String of space delimited platform values.
161
     * Allowed values are web, mobile, and tv.
162
     *
163
     * @attribute relationship
164
     * @attributeDataType \Wszetko\Sitemap\Items\DataTypes\StringType
165
     *
166
     * @var \Wszetko\Sitemap\Items\DataTypes\StringType
167
     */
168
    protected $platform;
169
170
    /**
171
     * Does the video require a subscription?
172
     *
173
     * @var \Wszetko\Sitemap\Items\DataTypes\YesNoType
174
     */
175
    protected $requiresSubscription;
176
177
    /**
178
     * @dataType \Wszetko\Sitemap\Items\DataTypes\FloatType
179
     * @attribute currency
180
     * @attributeDataType \Wszetko\Sitemap\Items\DataTypes\StringType
181
     * @attribute type
182
     * @attributeDataType \Wszetko\Sitemap\Items\DataTypes\StringType
183
     * @attribute resolution
184
     * @attributeDataType \Wszetko\Sitemap\Items\DataTypes\StringType
185
     *
186
     * @var \Wszetko\Sitemap\Items\DataTypes\ArrayType
187
     */
188
    protected $price;
189
190
    /**
191
     * Link to gallery of which this video appears in.
192
     *
193
     * @var \Wszetko\Sitemap\Items\DataTypes\URLType
194
     */
195
    protected $galleryLoc;
196
197
    /**
198
     * A space-delimited list of countries where the video may or may not be played.
199
     *
200
     * @attribute relationship
201
     * @attributeDataType \Wszetko\Sitemap\Items\DataTypes\StringType
202
     *
203
     * @var \Wszetko\Sitemap\Items\DataTypes\StringType
204
     */
205
    protected $restriction;
206
207
    /**
208
     * A tag associated with the video.
209
     *
210
     * @dataType \Wszetko\Sitemap\Items\DataTypes\StringType
211
     *
212
     * @var \Wszetko\Sitemap\Items\DataTypes\ArrayType
213
     */
214
    protected $tag;
215
216
    /**
217
     * The video's category. For example, cooking.
218
     *
219
     * @var \Wszetko\Sitemap\Items\DataTypes\StringType
220
     */
221
    protected $category;
222
223
    /**
224
     * No if the video should be available only to users with SafeSearch turned off.
225
     *
226
     * @var \Wszetko\Sitemap\Items\DataTypes\YesNoType
227
     */
228
    protected $familyFriendly;
229
230
    /**
231
     * The date the video was first published.
232
     *
233
     * @var \Wszetko\Sitemap\Items\DataTypes\DateTimeType
234
     */
235
    protected $publicationDate;
236
237
    /**
238
     * The number of times the video has been viewed.
239
     *
240
     * @var \Wszetko\Sitemap\Items\DataTypes\IntegerType
241
     */
242
    protected $viewCount;
243
244
    /**
245
     * The video uploader's name. Only one <video:uploader> is allowed per video.
246
     *
247
     * @attribute info
248
     * @attributeDataType \Wszetko\Sitemap\Items\DataTypes\URLType
249
     *
250
     * @var \Wszetko\Sitemap\Items\DataTypes\StringType
251
     */
252
    protected $uploader;
253
254
    /**
255
     * The rating of the video. Allowed values are float numbers in the range 0.0 to 5.0.
256
     *
257
     * @var \Wszetko\Sitemap\Items\DataTypes\FloatType
258
     */
259
    protected $rating;
260
261
    /**
262
     * The date after which the video will no longer be available.
263
     *
264
     * @var \Wszetko\Sitemap\Items\DataTypes\DateTimeType
265
     */
266
    protected $expirationDate;
267
268
    /**
269
     * An unambiguous identifier for the video within a given identification context.
270
     *
271
     * @dataType \Wszetko\Sitemap\Items\DataTypes\StringType
272
     * @attribute type
273
     * @attributeDataType \Wszetko\Sitemap\Items\DataTypes\StringType
274
     *
275
     * @var \Wszetko\Sitemap\Items\DataTypes\ArrayType
276
     */
277
    protected $id;
278
279
    /**
280
     * Video constructor.
281
     *
282
     * @param string $thumbnailLoc
283
     * @param string $title
284
     * @param string $description
285
     *
286
     * @throws InvalidArgumentException
287
     * @throws \ReflectionException
288
     * @throws \Error
289
     */
290 312
    public function __construct($thumbnailLoc, $title, $description)
291
    {
292 312
        parent::__construct();
293 312
        $this->setUpValues();
294 312
        $this->setThumbnailLoc($thumbnailLoc);
295 310
        $this->setTitle($title);
296 308
        $this->setDescription($description);
297 306
    }
298
299
    /**
300
     * @return array
301
     *
302
     * @throws \InvalidArgumentException
303
     * @throws \Error
304
     */
305 6
    public function toArray(): array
306
    {
307
        if (
308 6
            (null === $this->getContentLoc() ||
309 6
                '' === $this->getContentLoc()) &&
310 2
            (null === $this->getPlayerLoc() ||
311 6
                '' === $this->getPlayerLoc())
312
        ) {
313 2
            throw new InvalidArgumentException('Nor content_loc or player_loc parameter is set.');
314
        }
315
316 6
        if (null === $this->getPlayerLoc() && null !== $this->getContentSegmentLoc()) {
317 2
            throw new InvalidArgumentException('Parameter player_loc should be set if content_segment_loc is defined.');
318
        }
319
320 4
        return parent::toArray();
321
    }
322
323 312
    private function setUpValues(): void
324
    {
325 312
        $this->thumbnailLoc
326 312
            ->setRequired(true)
327
        ;
328 312
        $this->title
329 312
            ->setRequired(true)
330 312
            ->setMinLength(1)
331 312
            ->setMaxLength(100)
332
        ;
333 312
        $this->description
334 312
            ->setRequired(true)
335 312
            ->setMinLength(1)
336 312
            ->setMaxLength(2048)
337
        ;
338 312
        $this->rating
339 312
            ->setMinValue(0)
340 312
            ->setMaxValue(5)
341 312
            ->setPrecision(1)
342
        ;
343 312
        $this->viewCount
344 312
            ->setMinValue(0)
345
        ;
346 312
        $this->duration
347 312
            ->setMinValue(0)
348 312
            ->setMaxValue(28800)
349
        ;
350 312
        $this->restriction
351 312
            ->setConversion('upper')
352 312
            ->setValueRegex("/^([A-Z]{2}( +[A-Z]{2})*)?$/")
353
        ;
354
        /** @var \Wszetko\Sitemap\Items\DataTypes\StringType $restrictionRelationship */
355 312
        $restrictionRelationship = $this->restriction->getAttribute('relationship');
356
        $restrictionRelationship
357 312
            ->setConversion('lower')
358 312
            ->setAllowedValues('allow, deny')
359
        ;
360 312
        $this->platform
361 312
            ->setConversion('lower')
362 312
            ->setValueRegex("/^((web|mobile|tv)( (web|mobile|tv))*)?/")
363
        ;
364
        /** @var \Wszetko\Sitemap\Items\DataTypes\StringType $platformRelationship */
365 312
        $platformRelationship = $this->platform->getAttribute('relationship');
366
        $platformRelationship
367 312
            ->setConversion('lower')
368 312
            ->setAllowedValues('allow, deny')
369
        ;
370
        /** @var \Wszetko\Sitemap\Items\DataTypes\FloatType $priceType */
371 312
        $priceType = $this->price->getBaseDataType();
372
        $priceType
373 312
            ->setMinValue(0)
374 312
            ->setPrecision(2)
375
        ;
376
        /** @var \Wszetko\Sitemap\Items\DataTypes\StringType $priceCurrency */
377 312
        $priceCurrency = $priceType->getAttribute('currency');
378
        $priceCurrency
379 312
            ->setConversion('upper')
380 312
            ->setRequired(true)
381 312
            ->setValueRegex("/^([A-Z]{3})$/")
382
        ;
383
        /** @var \Wszetko\Sitemap\Items\DataTypes\StringType $priceType */
384 312
        $priceType = $this->price->getBaseDataType()->getAttribute('type');
385
        $priceType
386 312
            ->setConversion('lower')
387 312
            ->setAllowedValues('rent, purchase')
388
        ;
389
        /** @var \Wszetko\Sitemap\Items\DataTypes\StringType $priceResolution */
390 312
        $priceResolution = $this->price->getBaseDataType()->getAttribute('resolution');
391
        $priceResolution
392 312
            ->setConversion('upper')
393 312
            ->setAllowedValues('SD, HD')
394
        ;
395 312
        $this->tag
396 312
            ->setMaxElements(32)
397
        ;
398 312
        $this->category
399 312
            ->setMaxLength(256)
400
        ;
401
        /** @var \Wszetko\Sitemap\Items\DataTypes\StringType $idType */
402 312
        $idType = $this->id->getBaseDataType()->getAttribute('type');
403 312
        $idType->setRequired(true);
404 312
        $idType->setAllowedValues(['tms:series', 'tms:program', 'rovi:series', 'rovi:program', 'freebase', 'url']);
405
        /** @var \Wszetko\Sitemap\Items\DataTypes\IntegerType $contentSegmentLocDuration */
406 312
        $contentSegmentLocDuration = $this->contentSegmentLoc->getBaseDataType()->getAttribute('duration');
407
        $contentSegmentLocDuration
408 312
            ->setMinValue(0)
409 312
            ->setMaxValue(28800)
410
        ;
411 312
    }
412
}
413