1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: Ricardo Fiorani |
5
|
|
|
* Date: 29/08/2015 |
6
|
|
|
* Time: 19:37. |
7
|
|
|
*/ |
8
|
|
|
namespace RicardoFiorani\Adapter; |
9
|
|
|
|
10
|
|
|
use RicardoFiorani\Exception\NotEmbeddableException; |
11
|
|
|
use RicardoFiorani\Renderer\EmbedRendererInterface; |
12
|
|
|
|
13
|
|
|
abstract class AbstractServiceAdapter implements VideoAdapterInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
public $rawUrl; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
public $videoId; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
public $pattern; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var EmbedRendererInterface |
32
|
|
|
*/ |
33
|
|
|
public $renderer; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* AbstractVideoAdapter constructor. |
37
|
|
|
* |
38
|
|
|
* @param string $url |
39
|
|
|
* @param string $pattern |
40
|
|
|
* @param EmbedRendererInterface $renderer |
41
|
|
|
*/ |
42
|
|
|
public function __construct($url, $pattern, EmbedRendererInterface $renderer) |
43
|
|
|
{ |
44
|
|
|
$this->rawUrl = $url; |
45
|
|
|
$this->pattern = $pattern; |
46
|
|
|
$this->renderer = $renderer; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Returns the input URL. |
51
|
|
|
* |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
|
|
public function getRawUrl() |
55
|
|
|
{ |
56
|
|
|
return $this->rawUrl; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $rawUrl |
61
|
|
|
*/ |
62
|
|
|
public function setRawUrl($rawUrl) |
63
|
|
|
{ |
64
|
|
|
$this->rawUrl = $rawUrl; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
public function getVideoId() |
71
|
|
|
{ |
72
|
|
|
return $this->videoId; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $videoId |
77
|
|
|
*/ |
78
|
|
|
public function setVideoId($videoId) |
79
|
|
|
{ |
80
|
|
|
$this->videoId = $videoId; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
public function getPattern() |
87
|
|
|
{ |
88
|
|
|
return $this->pattern; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param string $pattern |
93
|
|
|
*/ |
94
|
|
|
public function setPattern($pattern) |
95
|
|
|
{ |
96
|
|
|
$this->pattern = $pattern; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return EmbedRendererInterface |
101
|
|
|
*/ |
102
|
|
|
public function getRenderer() |
103
|
|
|
{ |
104
|
|
|
return $this->renderer; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param EmbedRendererInterface $renderer |
109
|
|
|
*/ |
110
|
|
|
public function setRenderer($renderer) |
111
|
|
|
{ |
112
|
|
|
$this->renderer = $renderer; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param int $width |
117
|
|
|
* @param int $height |
118
|
|
|
* @param bool $forceAutoplay |
119
|
|
|
* @param bool $forceSecure |
120
|
|
|
* |
121
|
|
|
* @return string |
122
|
|
|
* @throws NotEmbeddableException |
123
|
|
|
*/ |
124
|
|
|
public function getEmbedCode($width, $height, $forceAutoplay = false, $forceSecure = false) |
125
|
|
|
{ |
126
|
|
|
if (false == $this->isEmbeddable()) { |
|
|
|
|
127
|
|
|
throw new NotEmbeddableException(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $this->getRenderer()->renderVideoEmbedCode( |
131
|
|
|
$this->getEmbedUrl($forceAutoplay, $forceSecure), |
132
|
|
|
$width, |
133
|
|
|
$height |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Switches the protocol scheme between http and https in case you want to force https |
139
|
|
|
* |
140
|
|
|
* @param bool|false $forceSecure |
141
|
|
|
* @return string |
|
|
|
|
142
|
|
|
*/ |
143
|
|
|
public function getScheme($forceSecure = false) |
144
|
|
|
{ |
145
|
|
|
if ($forceSecure) { |
146
|
|
|
return 'https'; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return parse_url($this->rawUrl, PHP_URL_SCHEME); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.