Mike42\Escpos\Printer::validateIntegerMulti PHP Method

validateIntegerMulti() protected static method

Throw an exception if the argument given is not an integer within one of the specified ranges
protected static validateIntegerMulti ( integer $test, array $ranges, string $source, string $argument = "Argument" )
$test integer the input to test
$ranges array array of two-item min/max ranges.
$source string the name of the function calling this
$argument string the name of the invalid parameter
    protected static function validateIntegerMulti($test, array $ranges, $source, $argument = "Argument")
    {
        if (!is_integer($test)) {
            throw new InvalidArgumentException("{$argument} given to {$source} must be a number, but '{$test}' was given.");
        }
        $match = false;
        foreach ($ranges as $range) {
            $match |= $test >= $range[0] && $test <= $range[1];
        }
        if (!$match) {
            // Put together a good error "range 1-2 or 4-6"
            $rangeStr = "range ";
            for ($i = 0; $i < count($ranges); $i++) {
                $rangeStr .= $ranges[$i][0] . "-" . $ranges[$i][1];
                if ($i == count($ranges) - 1) {
                    continue;
                } elseif ($i == count($ranges) - 2) {
                    $rangeStr .= " or ";
                } else {
                    $rangeStr .= ", ";
                }
            }
            throw new InvalidArgumentException("{$argument} given to {$source} must be in {$rangeStr}, but {$test} was given.");
        }
    }