respect-validation/library/Rules/VideoUrl.php

95 lines
2.5 KiB
PHP
Raw Normal View History

2015-09-15 20:35:17 +02:00
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
2015-09-15 20:35:17 +02:00
*/
declare(strict_types=1);
2015-09-15 20:35:17 +02:00
namespace Respect\Validation\Rules;
use Respect\Validation\Exceptions\ComponentException;
use function array_keys;
use function is_string;
use function mb_strtolower;
use function preg_match;
use function sprintf;
2015-09-15 20:35:17 +02:00
/**
* Validates if the input is a video URL value.
*
* @author Danilo Correa <danilosilva87@gmail.com>
* @author Emmerson Siqueira <emmersonsiqueira@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
* @author Ricardo Gobbo <ricardo@clicknow.com.br>
*/
final class VideoUrl extends AbstractRule
2015-09-15 20:35:17 +02:00
{
private const SERVICES = [
// phpcs:disable Generic.Files.LineLength.TooLong
2015-09-15 20:35:17 +02:00
'youtube' => '@^https?://(www\.)?(?:youtube\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^\"&?/]{11})@i',
'vimeo' => '@^https?://(www\.)?(player\.)?(vimeo\.com/)((channels/[A-z]+/)|(groups/[A-z]+/videos/)|(video/))?([0-9]+)@i',
'twitch' => '@^https?://(((www\.)?twitch\.tv/videos/[0-9]+)|clips\.twitch\.tv/[a-zA-Z]+)$@i',
// phpcs:enable Generic.Files.LineLength.TooLong
2015-10-18 03:44:47 +02:00
];
/**
* @var string|null
*/
private $service;
2015-09-15 20:35:17 +02:00
/**
* Create a new instance VideoUrl.
2015-09-15 20:35:17 +02:00
*
* @throws ComponentException when the given service is not supported
2015-09-15 20:35:17 +02:00
*/
public function __construct(?string $service = null)
2015-09-15 20:35:17 +02:00
{
if ($service !== null && !$this->isSupportedService($service)) {
2015-09-15 20:35:17 +02:00
throw new ComponentException(sprintf('"%s" is not a recognized video service.', $service));
}
$this->service = $service;
}
/**
* {@inheritDoc}
2015-09-15 20:35:17 +02:00
*/
public function validate($input): bool
2015-09-15 20:35:17 +02:00
{
if (!is_string($input)) {
return false;
2015-09-15 20:35:17 +02:00
}
if ($this->service !== null) {
return $this->isValid($this->service, $input);
}
foreach (array_keys(self::SERVICES) as $service) {
if (!$this->isValid($service, $input)) {
2015-09-15 20:35:17 +02:00
continue;
}
return true;
}
return false;
}
private function isSupportedService(string $service): bool
{
return isset(self::SERVICES[mb_strtolower($service)]);
}
private function isValid(string $service, string $input): bool
{
return preg_match(self::SERVICES[mb_strtolower($service)], $input) > 0;
}
2015-09-15 20:35:17 +02:00
}