Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. DivisibleByValidator.php

class DivisibleByValidator

Validates that values are a multiple of the given number.

@author Colin O'Dell <colinodell@gmail.com>

Hierarchy

  • class \Symfony\Component\Validator\ConstraintValidator implements \Symfony\Component\Validator\ConstraintValidatorInterface
    • class \Symfony\Component\Validator\Constraints\AbstractComparisonValidator extends \Symfony\Component\Validator\ConstraintValidator
      • class \Symfony\Component\Validator\Constraints\DivisibleByValidator extends \Symfony\Component\Validator\Constraints\AbstractComparisonValidator

Expanded class hierarchy of DivisibleByValidator

File

vendor/symfony/validator/Constraints/DivisibleByValidator.php, line 21

Namespace

Symfony\Component\Validator\Constraints
View source
class DivisibleByValidator extends AbstractComparisonValidator {
    protected function compareValues(mixed $value1, mixed $value2) : bool {
        if (!is_numeric($value1)) {
            throw new UnexpectedValueException($value1, 'numeric');
        }
        if (!is_numeric($value2)) {
            throw new UnexpectedValueException($value2, 'numeric');
        }
        if (!($value2 = abs($value2))) {
            return false;
        }
        if (\is_int($value1 = abs($value1)) && \is_int($value2)) {
            return 0 === $value1 % $value2;
        }
        if (!($remainder = fmod($value1, $value2))) {
            return true;
        }
        if (\is_float($value2) && \INF !== $value2) {
            $quotient = $value1 / $value2;
            $rounded = round($quotient);
            return \sprintf('%.12e', $quotient) === \sprintf('%.12e', $rounded);
        }
        return \sprintf('%.12e', $value2) === \sprintf('%.12e', $remainder);
    }
    protected function getErrorCode() : ?string {
        return DivisibleBy::NOT_DIVISIBLE_BY;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
AbstractComparisonValidator::getPropertyAccessor private function
AbstractComparisonValidator::validate public function Checks if the passed value is valid. Overrides ConstraintValidatorInterface::validate
AbstractComparisonValidator::__construct public function
ConstraintValidator::$context protected property
ConstraintValidator::formatTypeOf protected function Returns a string representation of the type of the value.
ConstraintValidator::formatValue protected function Returns a string representation of the value.
ConstraintValidator::formatValues protected function Returns a string representation of a list of values.
ConstraintValidator::initialize public function Initializes the constraint validator. Overrides ConstraintValidatorInterface::initialize
ConstraintValidator::OBJECT_TO_STRING public constant Whether to cast objects with a &quot;__toString()&quot; method to strings.
ConstraintValidator::PRETTY_DATE public constant Whether to format {@link \DateTime} objects, either with the {@link \IntlDateFormatter}
(if it is available) or as RFC-3339 dates (&quot;Y-m-d H:i:s&quot;).
DivisibleByValidator::compareValues protected function Compares the two given values to find if their relationship is valid. Overrides AbstractComparisonValidator::compareValues
DivisibleByValidator::getErrorCode protected function Returns the error code used if the comparison fails. Overrides AbstractComparisonValidator::getErrorCode
RSS feed
Powered by Drupal