Passed
Branch develop (2fd4b5)
by Alexey
01:46
created

Video::getThumb()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * @author   Ne-Lexa
6
 * @license  MIT
7
 * @link     https://github.com/Ne-Lexa/google-play-scraper
8
 */
9
10
namespace Nelexa\GPlay\Model;
11
12
/**
13
 * Contains promo video data.
14
 *
15
 * @see \Nelexa\GPlay\GPlayApps::getApps() Returns detailed information about
16
 *     many android packages.
17
 * @see \Nelexa\GPlay\GPlayApps::getAppInLocales() Returns detailed information
18
 *     about an application from the Google Play store for an array of locales.
19
 * @see \Nelexa\GPlay\GPlayApps::getAppInAvailableLocales() Returns detailed
20
 *     information about the application in all available locales.
21
 */
22
class Video implements \JsonSerializable
23
{
24
    use JsonSerializableTrait;
25
26
    /** @var string Video thumbnail url. */
27
    private $imageUrl;
28
29
    /** @var string Video url. */
30
    private $videoUrl;
31
32
    /**
33
     * Creates an object with information about the promo video for Android application.
34
     *
35
     * @param string $imageUrl Video thumbnail url.
36
     * @param string $videoUrl Video url.
37
     */
38 3
    public function __construct(string $imageUrl, string $videoUrl)
39
    {
40 3
        $this->imageUrl = $imageUrl;
41 3
        $this->videoUrl = $videoUrl;
42 3
    }
43
44
    /**
45
     * Returns the video thumbnail url.
46
     *
47
     * @return string Image url.
48
     */
49 1
    public function getImageUrl(): string
50
    {
51 1
        return $this->imageUrl;
52
    }
53
54
    /**
55
     * Returns the video url.
56
     *
57
     * @return string Video url.
58
     */
59 1
    public function getVideoUrl(): string
60
    {
61 1
        return $this->videoUrl;
62
    }
63
64
    /**
65
     * Returns a YouTube id.
66
     *
67
     * @return string|null YouTube ID or `null` if the video is not from YouTube.
68
     */
69 1
    public function getYoutubeId(): ?string
70
    {
71 1
        if (preg_match('~^.*(?:(?:youtu\.be\/|v\/|vi\/|u\/\w\/|embed\/)|(?:(?:watch)?\?v(?:i)?=|\&v(?:i)?=))([^#\&\?]*).*~', $this->videoUrl, $match)) {
72 1
            return $match[1];
73
        }
74
        return null;
75
    }
76
77
    /**
78
     * Returns class properties as an array.
79
     *
80
     * @return array Class properties as an array.
81
     */
82
    public function asArray(): array
83
    {
84
        return [
85
            'thumbUrl' => $this->imageUrl,
86
            'videoUrl' => $this->videoUrl,
87
        ];
88
    }
89
}
90