You are here

public function FractionConstraintValidator::validate in Fraction 8

Same name and namespace in other branches
  1. 2.x src/Plugin/Validation/Constraint/FractionConstraintValidator.php \Drupal\fraction\Plugin\Validation\Constraint\FractionConstraintValidator::validate()

File

src/Plugin/Validation/Constraint/FractionConstraintValidator.php, line 16

Class

FractionConstraintValidator
Validates the FractionConstraint constraint.

Namespace

Drupal\fraction\Plugin\Validation\Constraint

Code

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

  /** @var \Drupal\fraction\Plugin\Field\FieldType\FractionItem $fraction */
  $fractionItem = $items;
  $values = $fractionItem
    ->getValue();
  if (empty($values['denominator']) && !empty($values['numerator'])) {
    $this->context
      ->addViolation($constraint->denominatorNotZero);
    return;
  }
  if (!empty($values['denominator']) && ((string) $values['denominator'] <= '0' || (string) $values['denominator'] > '2147483647')) {
    $this->context
      ->addViolation($constraint->denominatorOutOfRange);
    return;
  }
  if (!empty($values['numerator']) && ((string) $values['numerator'] < '-9223372036854775808' || (string) $values['numerator'] > '9223372036854775807')) {
    $this->context
      ->addViolation($constraint->numeratorOutOfRange);
    return;
  }
  if ((string) $values['denominator'] > '1000000000') {
    $this->context
      ->addViolation($constraint->maxNumberOfDecimals);
    return;
  }
}