You are here

class GranularityStringConstraintValidator in Duration Field 3.0.x

Same name and namespace in other branches
  1. 8.2 src/Plugin/Validation/Constraint/GranularityStringConstraintValidator.php \Drupal\duration_field\Plugin\Validation\Constraint\GranularityStringConstraintValidator

Validates the granularity_string constraint.

Hierarchy

Expanded class hierarchy of GranularityStringConstraintValidator

File

src/Plugin/Validation/Constraint/GranularityStringConstraintValidator.php, line 11

Namespace

Drupal\duration_field\Plugin\Validation\Constraint
View source
class GranularityStringConstraintValidator extends ConstraintValidator {

  /**
   * {@inheritdoc}
   */
  public function validate($items, Constraint $constraint) {
    if (is_array($items)) {
      foreach ($items as $item) {
        if (!$this
          ->isGranularityString($item)) {

          // The value is not an integer, so a violation, aka error, is applied.
          // The type of violation applied comes from the constraint description
          // in step 1.
          $this->context
            ->addViolation($constraint->notValidGranularityString, [
            '%value' => (string) $item,
          ]);
        }
      }
    }
    else {
      if (!$this
        ->isGranularityString($items)) {
        $this->context
          ->addViolation($constraint->notValidGranularityString, [
          '%value' => (string) $items,
        ]);
      }
    }
  }

  /**
   * Check if a string is a valid granularity string.
   *
   * @param mixed $value
   *   The item to check as a granularity string.
   *
   * @return bool
   *   TRUE if the given value is a valid granularity string. FALSE if it is
   *   not.
   */
  private function isGranularityString($value) {
    if (is_string($value)) {
      return preg_match(GraularityStringInterface::GRANULARITY_STRING_PATTERN, $value);
    }
    return FALSE;
  }

}

Members