Passed
Branch feature/refactoring (13cbf0)
by Alexey
03:46
created

Video::asArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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