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 mb_strtolower; |
18
|
|
|
use function preg_match; |
19
|
|
|
use function sprintf; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Validates if the input is a video URL value: |
23
|
|
|
* |
24
|
|
|
* @author Danilo Correa <[email protected]> |
25
|
|
|
* @author Henrique Moody <[email protected]> |
26
|
|
|
* @author Ricardo Gobbo <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
final class VideoUrl extends AbstractRule |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
public $service; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $serviceKey; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
private $services = [ |
44
|
|
|
'youtube' => '@^https?://(www\.)?(?:youtube\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^\"&?/]{11})@i', |
45
|
|
|
'vimeo' => '@^https?://(www\.)?(player\.)?(vimeo\.com/)((channels/[A-z]+/)|(groups/[A-z]+/videos/)|(video/))?([0-9]+)@i', |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Create a new instance VideoUrl. |
50
|
|
|
* |
51
|
|
|
* @param string $service |
52
|
|
|
*/ |
53
|
1 |
|
public function __construct(string $service = null) |
54
|
|
|
{ |
55
|
1 |
|
$serviceKey = mb_strtolower((string) $service); |
56
|
1 |
|
if (null !== $service && !isset($this->services[$serviceKey])) { |
57
|
|
|
throw new ComponentException(sprintf('"%s" is not a recognized video service.', $service)); |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
$this->service = $service; |
61
|
1 |
|
$this->serviceKey = $serviceKey; |
62
|
1 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
22 |
|
public function validate($input): bool |
68
|
|
|
{ |
69
|
22 |
|
if (isset($this->services[$this->serviceKey])) { |
70
|
8 |
|
return preg_match($this->services[$this->serviceKey], (string) $input) > 0; |
71
|
|
|
} |
72
|
|
|
|
73
|
15 |
|
foreach ($this->services as $pattern) { |
74
|
15 |
|
if (0 === preg_match($pattern, (string) $input)) { |
75
|
12 |
|
continue; |
76
|
|
|
} |
77
|
|
|
|
78
|
6 |
|
return true; |
79
|
|
|
} |
80
|
|
|
|
81
|
10 |
|
return false; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|