Version 1
[yaffs-website] / web / modules / contrib / entityqueue / src / Plugin / Validation / Constraint / QueueSizeConstraintValidator.php
diff --git a/web/modules/contrib/entityqueue/src/Plugin/Validation/Constraint/QueueSizeConstraintValidator.php b/web/modules/contrib/entityqueue/src/Plugin/Validation/Constraint/QueueSizeConstraintValidator.php
new file mode 100644 (file)
index 0000000..049d32a
--- /dev/null
@@ -0,0 +1,39 @@
+<?php
+
+namespace Drupal\entityqueue\Plugin\Validation\Constraint;
+
+use Symfony\Component\Validator\Constraint;
+use Symfony\Component\Validator\ConstraintValidator;
+
+/**
+ * Validates the QueueSize constraint.
+ */
+class QueueSizeConstraintValidator extends ConstraintValidator {
+
+  /**
+   * {@inheritdoc}
+   */
+  public function validate($entity, Constraint $constraint) {
+    $number_of_items = count($entity->items);
+
+    /** @var \Drupal\entityqueue\EntityQueueInterface $queue */
+    $queue = $entity->getQueue();
+    $min_size = $queue->getMinimumSize();
+    $max_size = $queue->getMaximumSize();
+    $act_as_queue = $queue->getActAsQueue();
+
+    // Do not allow less items than the minimum size.
+    if ($min_size && $number_of_items < $min_size) {
+      $this->context->buildViolation($constraint->messageMinSize, ['%min_size' => $min_size])
+        ->addViolation();
+    }
+    // Do not allow more items than the maximum size if the queue is not
+    // configured to act a simple list.
+    elseif (!$act_as_queue && $max_size && $number_of_items > $max_size) {
+      $this->context->buildViolation($constraint->messageMaxSize, ['%max_size' => $max_size])
+        ->addViolation();
+    }
+
+  }
+
+}