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
|
|
|
]; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Create a new instance VideoUrl. |
47
|
|
|
* |
48
|
|
|
* @param string|null $service |
49
|
|
|
* |
50
|
|
|
* @throws ComponentException when the given service is not supported |
51
|
|
|
*/ |
52
|
1 |
|
public function __construct(string $service = null) |
53
|
|
|
{ |
54
|
1 |
|
if (null !== $service && !$this->isSupportedService($service)) { |
55
|
|
|
throw new ComponentException(sprintf('"%s" is not a recognized video service.', $service)); |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
$this->service = $service; |
59
|
1 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
22 |
|
public function validate($input): bool |
65
|
|
|
{ |
66
|
22 |
|
if (!is_string($input)) { |
67
|
|
|
return false; |
68
|
|
|
} |
69
|
|
|
|
70
|
22 |
|
if (null !== $this->service) { |
71
|
8 |
|
return $this->isValid($this->service, $input); |
72
|
|
|
} |
73
|
|
|
|
74
|
15 |
|
foreach (array_keys(self::SERVICES) as $service) { |
75
|
15 |
|
if (!$this->isValid($service, $input)) { |
76
|
12 |
|
continue; |
77
|
|
|
} |
78
|
|
|
|
79
|
6 |
|
return true; |
80
|
|
|
} |
81
|
|
|
|
82
|
10 |
|
return false; |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
private function isSupportedService(string $service): bool |
86
|
|
|
{ |
87
|
1 |
|
return isset(self::SERVICES[mb_strtolower($service)]); |
88
|
|
|
} |
89
|
|
|
|
90
|
22 |
|
private function isValid(string $service, string $input): bool |
91
|
|
|
{ |
92
|
22 |
|
return preg_match(self::SERVICES[mb_strtolower($service)], $input) > 0; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|