1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Respect/Validation. |
5
|
|
|
* |
6
|
|
|
* (c) Alexandre Gomes Gaigalas <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the "LICENSE.md" |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Respect\Validation\Rules; |
15
|
|
|
|
16
|
|
|
use Respect\Validation\Exceptions\ComponentException; |
17
|
|
|
use function array_keys; |
18
|
|
|
use function is_string; |
19
|
|
|
use function mb_strtolower; |
20
|
|
|
use function preg_match; |
21
|
|
|
use function sprintf; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Validates if the input is a video URL value. |
25
|
|
|
* |
26
|
|
|
* @author Danilo Correa <[email protected]> |
27
|
|
|
* @author Henrique Moody <[email protected]> |
28
|
|
|
* @author Ricardo Gobbo <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
final class VideoUrl extends AbstractRule |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $service; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
private const SERVICES = [ |
41
|
|
|
'youtube' => '@^https?://(www\.)?(?:youtube\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^\"&?/]{11})@i', |
42
|
|
|
'vimeo' => '@^https?://(www\.)?(player\.)?(vimeo\.com/)((channels/[A-z]+/)|(groups/[A-z]+/videos/)|(video/))?([0-9]+)@i', |
43
|
|
|
'twitch' => '@^https?://(((www\.)?twitch\.tv/videos/[0-9]+)|clips\.twitch\.tv/[a-zA-Z]+)$@i', |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Create a new instance VideoUrl. |
48
|
|
|
* |
49
|
|
|
* @param string|null $service |
50
|
|
|
* |
51
|
|
|
* @throws ComponentException when the given service is not supported |
52
|
|
|
*/ |
53
|
1 |
|
public function __construct(string $service = null) |
54
|
|
|
{ |
55
|
1 |
|
if (null !== $service && !$this->isSupportedService($service)) { |
56
|
|
|
throw new ComponentException(sprintf('"%s" is not a recognized video service.', $service)); |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
$this->service = $service; |
60
|
1 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
29 |
|
public function validate($input): bool |
66
|
|
|
{ |
67
|
29 |
|
if (!is_string($input)) { |
68
|
|
|
return false; |
69
|
|
|
} |
70
|
|
|
|
71
|
29 |
|
if (null !== $this->service) { |
72
|
10 |
|
return $this->isValid($this->service, $input); |
73
|
|
|
} |
74
|
|
|
|
75
|
20 |
|
foreach (array_keys(self::SERVICES) as $service) { |
76
|
20 |
|
if (!$this->isValid($service, $input)) { |
77
|
17 |
|
continue; |
78
|
|
|
} |
79
|
|
|
|
80
|
6 |
|
return true; |
81
|
|
|
} |
82
|
|
|
|
83
|
15 |
|
return false; |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
private function isSupportedService(string $service): bool |
87
|
|
|
{ |
88
|
1 |
|
return isset(self::SERVICES[mb_strtolower($service)]); |
89
|
|
|
} |
90
|
|
|
|
91
|
29 |
|
private function isValid(string $service, string $input): bool |
92
|
|
|
{ |
93
|
29 |
|
return preg_match(self::SERVICES[mb_strtolower($service)], $input) > 0; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|