You are here

public function PinEmbedCodeConstraintValidator::validate in Media entity Pinterest 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Validation/Constraint/PinEmbedCodeConstraintValidator.php \Drupal\media_entity_pinterest\Plugin\Validation\Constraint\PinEmbedCodeConstraintValidator::validate()

File

src/Plugin/Validation/Constraint/PinEmbedCodeConstraintValidator.php, line 20

Class

PinEmbedCodeConstraintValidator
Validates the PinEmbedCode constraint.

Namespace

Drupal\media_entity_pinterest\Plugin\Validation\Constraint

Code

public function validate($value, Constraint $constraint) {
  $value = $this
    ->getEmbedCode($value);
  if (!isset($value)) {
    return;
  }
  $matches = [];
  foreach (Pinterest::$validationRegexp as $pattern => $key) {

    // URLs will sometimes have urlencoding, so we decode for safety.
    if (preg_match($pattern, urldecode($value), $item_matches)) {
      $matches[] = $item_matches;
    }
  }
  if (empty($matches)) {
    $this->context
      ->addViolation($constraint->message);
  }
}