Updating Media dependent modules to versions compatible with core Media.
[yaffs-website] / web / modules / contrib / media_entity_instagram / src / Plugin / Validation / Constraint / InstagramEmbedCodeConstraintValidator.php
index 1cd9516525881843dc5fe1241d20871b3bf305f8..367b7f3043ee467742de3370c73510d8d346cf2a 100644 (file)
@@ -2,8 +2,8 @@
 
 namespace Drupal\media_entity_instagram\Plugin\Validation\Constraint;
 
-use Drupal\media_entity\EmbedCodeValueTrait;
-use Drupal\media_entity_instagram\Plugin\MediaEntity\Type\Instagram;
+use Drupal\Core\Field\FieldItemInterface;
+use Drupal\media_entity_instagram\Plugin\media\Source\Instagram;
 use Symfony\Component\Validator\Constraint;
 use Symfony\Component\Validator\ConstraintValidator;
 
@@ -12,27 +12,34 @@ use Symfony\Component\Validator\ConstraintValidator;
  */
 class InstagramEmbedCodeConstraintValidator extends ConstraintValidator {
 
-  use EmbedCodeValueTrait;
-
   /**
    * {@inheritdoc}
    */
   public function validate($value, Constraint $constraint) {
-    $value = $this->getEmbedCode($value);
-    if (!isset($value)) {
-      return;
+    $data = '';
+    if (is_string($value)) {
+      $data = $value;
     }
-
-    $matches = [];
-    foreach (Instagram::$validationRegexp as $pattern => $key) {
-      if (preg_match($pattern, $value, $item_matches)) {
-        $matches[] = $item_matches;
+    elseif ($value instanceof FieldItemInterface) {
+      $class = get_class($value);
+      $property = $class::mainPropertyName();
+      if ($property) {
+        $data = $value->{$property};
       }
     }
 
-    if (empty($matches)) {
-      $this->context->addViolation($constraint->message);
+    if ($data) {
+      $matches = [];
+      foreach (Instagram::$validationRegexp as $pattern => $key) {
+        if (preg_match($pattern, $data, $item_matches)) {
+          $matches[] = $item_matches;
+        }
+      }
+      if (empty($matches)) {
+        $this->context->addViolation($constraint->message);
+      }
     }
+
   }
 
 }