Version 1
[yaffs-website] / web / core / modules / link / src / Plugin / Validation / Constraint / LinkNotExistingInternalConstraintValidator.php
1 <?php
2
3 namespace Drupal\link\Plugin\Validation\Constraint;
4
5 use Symfony\Component\Routing\Exception\InvalidParameterException;
6 use Symfony\Component\Routing\Exception\MissingMandatoryParametersException;
7 use Symfony\Component\Routing\Exception\RouteNotFoundException;
8 use Symfony\Component\Validator\Constraint;
9 use Symfony\Component\Validator\ConstraintValidator;
10
11 /**
12  * Validates the LinkNotExistingInternal constraint.
13  */
14 class LinkNotExistingInternalConstraintValidator extends ConstraintValidator {
15
16   /**
17    * {@inheritdoc}
18    */
19   public function validate($value, Constraint $constraint) {
20     if (isset($value)) {
21       try {
22         /** @var \Drupal\Core\Url $url */
23         $url = $value->getUrl();
24       }
25       // If the URL is malformed this constraint cannot check further.
26       catch (\InvalidArgumentException $e) {
27         return;
28       }
29
30       if ($url->isRouted()) {
31         $allowed = TRUE;
32         try {
33           $url->toString();
34         }
35         // The following exceptions are all possible during URL generation, and
36         // should be considered as disallowed URLs.
37         catch (RouteNotFoundException $e) {
38           $allowed = FALSE;
39         }
40         catch (InvalidParameterException $e) {
41           $allowed = FALSE;
42         }
43         catch (MissingMandatoryParametersException $e) {
44           $allowed = FALSE;
45         }
46         if (!$allowed) {
47           $this->context->addViolation($constraint->message, ['@uri' => $value->uri]);
48         }
49       }
50     }
51   }
52
53 }