From f66ab8dae2f3d0418759f81a3a64dc9517a62449 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 14 Apr 2024 15:17:31 +0200 Subject: Adding upstream version 1.10.2. Signed-off-by: Daniel Baumann --- library/Director/CheckPlugin/Range.php | 101 +++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 library/Director/CheckPlugin/Range.php (limited to 'library/Director/CheckPlugin/Range.php') diff --git a/library/Director/CheckPlugin/Range.php b/library/Director/CheckPlugin/Range.php new file mode 100644 index 0000000..d7b582e --- /dev/null +++ b/library/Director/CheckPlugin/Range.php @@ -0,0 +1,101 @@ +start = $start; + $this->end = $end; + $this->mustBeWithinRange = $mustBeWithinRange; + } + + public function valueIsValid($value) + { + if ($this->valueIsWithinRange($value)) { + return $this->valueMustBeWithinRange(); + } else { + return ! $this->valueMustBeWithinRange(); + } + } + + public function valueIsWithinRange($value) + { + if ($this->start !== null && $value < $this->start) { + return false; + } + if ($this->end !== null && $value > $this->end) { + return false; + } + + return true; + } + + public function valueMustBeWithinRange() + { + return $this->mustBeWithinRange; + } + + /** + * @param $any + * @return static + */ + public static function wantRange($any) + { + if ($any instanceof static) { + return $any; + } else { + return static::parse($any); + } + } + + /** + * @param $string + * @return static + * @throws ConfigurationError + */ + public static function parse($string) + { + $string = str_replace(' ', '', $string); + $value = '[-+]?[\d\.]+'; + $valueRe = "$value(?:e$value)?"; + $regex = "/^(@)?($valueRe|~)(:$valueRe|~)?/"; + if (! preg_match($regex, $string, $match)) { + throw new ConfigurationError('Invalid range definition: %s', $string); + } + + $inside = $match[1] === '@'; + + if (strlen($match[3]) === 0) { + $start = 0; + $end = static::parseValue($match[2]); + } else { + $start = static::parseValue($match[2]); + $end = static::parseValue($match[3]); + } + $range = new static($start, $end, $inside); + + return $range; + } + + protected static function parseValue($value) + { + if ($value === '~') { + return null; + } else { + return $value; + } + } +} -- cgit v1.2.3