You are here

class VersionValidator in Markdown 8.2

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

@todo Move upstream to https://www.drupal.org/project/installable_plugins. @internal

Hierarchy

  • class \Drupal\markdown\Plugin\Validation\Constraint\VersionValidator extends \Symfony\Component\Validator\ConstraintValidator

Expanded class hierarchy of VersionValidator

File

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

Namespace

Drupal\markdown\Plugin\Validation\Constraint
View source
class VersionValidator extends ConstraintValidator {

  /**
   * Semver version parser.
   *
   * @var \Composer\Semver\VersionParser
   */
  private static $versionParser;

  /**
   * {@inheritdoc}
   */
  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));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
VersionValidator::$versionParser private static property Semver version parser.
VersionValidator::validate public function Checks if the passed value is valid.