Completed
Push — master ( 8e2f23...0bdc89 )
by Maxime
18s queued 11s
created

YoutubeUrlValidator::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 6
c 2
b 0
f 0
dl 0
loc 10
rs 10
cc 3
nc 3
nop 2
1
<?php
2
3
/*
4
 * This file is part of Monsieur Biz' Rich Editor plugin for Sylius.
5
 *
6
 * (c) Monsieur Biz <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace MonsieurBiz\SyliusRichEditorPlugin\Validator\Constraints;
15
16
use Symfony\Component\Validator\Constraint;
17
use Symfony\Component\Validator\ConstraintValidator;
18
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
19
20
class YoutubeUrlValidator extends ConstraintValidator
21
{
22
    public const YOUTUBE_REGEX_VALIDATOR = '`^(?:https?://)?(?:www\.)?(?:youtu.be/|youtube\.com/(?:watch(?:/|/?\?(?:\S*&)?v=)|embed/))([\w\d]+)$`';
23
24
    public function validate($value, Constraint $constraint): void
25
    {
26
        if (!$constraint instanceof YoutubeUrl) {
27
            throw new UnexpectedTypeException($constraint, YoutubeUrl::class);
28
        }
29
30
        if (!preg_match(self::YOUTUBE_REGEX_VALIDATOR, (string) $value)) {
31
            $this->context->buildViolation($constraint->message)
32
                ->setParameter('{{ string }}', $value)
33
                ->addViolation()
34
            ;
35
        }
36
    }
37
}
38