You are here

public function VersionValidator::validate in Markdown 8.2

File

src/Plugin/Validation/Constraint/VersionValidator.php, line 29

Class

VersionValidator
Validates that a field is unique for the given entity type.

Namespace

Drupal\markdown\Plugin\Validation\Constraint

Code

public function validate($version, Constraint $constraint) {

  /** @var \Drupal\markdown\Plugin\Validation\Constraint\Version $constraint */
  $semverConstraints = $constraint->value;
  $named = isset($constraint->name);
  $message = $named ? $constraint->namedMessage : $constraint->message;
  $params = [
    '@name' => $named ? Markup::create($constraint->name) : 'Unknown',
    '@constraints' => $semverConstraints ? Markup::create($semverConstraints) : '',
    '@version' => $version ? Markup::create($version) : '',
  ];
  $validated = FALSE;
  try {
    if (!empty($version)) {
      if (!empty($semverConstraints)) {
        $validated = Semver::satisfies($version, $semverConstraints);
      }
      else {
        if (!self::$versionParser) {
          self::$versionParser = new VersionParser();
        }
        $validated = !!self::$versionParser
          ->normalize($version);
      }
    }
  } catch (\UnexpectedValueException $exception) {
    $message = $exception
      ->getMessage();
  }
  if (!$validated) {

    // Passing an already translated message allows markup to be preserved
    // when it passes to the theme system.
    $this->context
      ->addViolation(t($message, $params));
  }
}