Video::getImageUrl()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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