Updating Media dependent modules to versions compatible with core Media.
[yaffs-website] / web / modules / contrib / media_entity_instagram / src / Plugin / Validation / Constraint / InstagramEmbedCodeConstraintValidator.php
1 <?php
2
3 namespace Drupal\media_entity_instagram\Plugin\Validation\Constraint;
4
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;
9
10 /**
11  * Validates the InstagramEmbedCode constraint.
12  */
13 class InstagramEmbedCodeConstraintValidator extends ConstraintValidator {
14
15   /**
16    * {@inheritdoc}
17    */
18   public function validate($value, Constraint $constraint) {
19     $data = '';
20     if (is_string($value)) {
21       $data = $value;
22     }
23     elseif ($value instanceof FieldItemInterface) {
24       $class = get_class($value);
25       $property = $class::mainPropertyName();
26       if ($property) {
27         $data = $value->{$property};
28       }
29     }
30
31     if ($data) {
32       $matches = [];
33       foreach (Instagram::$validationRegexp as $pattern => $key) {
34         if (preg_match($pattern, $data, $item_matches)) {
35           $matches[] = $item_matches;
36         }
37       }
38       if (empty($matches)) {
39         $this->context->addViolation($constraint->message);
40       }
41     }
42
43   }
44
45 }