You are here

class FractionConstraintValidator 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

Validates the FractionConstraint constraint.

Hierarchy

  • class \Drupal\fraction\Plugin\Validation\Constraint\FractionConstraintValidator extends \Symfony\Component\Validator\ConstraintValidator

Expanded class hierarchy of FractionConstraintValidator

File

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

Namespace

Drupal\fraction\Plugin\Validation\Constraint
View source
class FractionConstraintValidator extends ConstraintValidator {

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

}

Members

Namesort descending Modifiers Type Description Overrides
FractionConstraintValidator::validate public function Checks if the passed value is valid.