3 namespace Drupal\media_entity_instagram\Plugin\Validation\Constraint;
5 use Drupal\Core\Field\FieldItemInterface;
6 use Drupal\media_entity_instagram\Plugin\media\Source\Instagram;
7 use Symfony\Component\Validator\Constraint;
8 use Symfony\Component\Validator\ConstraintValidator;
11 * Validates the InstagramEmbedCode constraint.
13 class InstagramEmbedCodeConstraintValidator extends ConstraintValidator {
18 public function validate($value, Constraint $constraint) {
20 if (is_string($value)) {
23 elseif ($value instanceof FieldItemInterface) {
24 $class = get_class($value);
25 $property = $class::mainPropertyName();
27 $data = $value->{$property};
33 foreach (Instagram::$validationRegexp as $pattern => $key) {
34 if (preg_match($pattern, $data, $item_matches)) {
35 $matches[] = $item_matches;
38 if (empty($matches)) {
39 $this->context->addViolation($constraint->message);