summaryrefslogtreecommitdiffstats
path: root/library/Director/CheckPlugin/Range.php
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-14 13:17:31 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-14 13:17:31 +0000
commitf66ab8dae2f3d0418759f81a3a64dc9517a62449 (patch)
treefbff2135e7013f196b891bbde54618eb050e4aaf /library/Director/CheckPlugin/Range.php
parentInitial commit. (diff)
downloadicingaweb2-module-director-f66ab8dae2f3d0418759f81a3a64dc9517a62449.tar.xz
icingaweb2-module-director-f66ab8dae2f3d0418759f81a3a64dc9517a62449.zip
Adding upstream version 1.10.2.upstream/1.10.2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'library/Director/CheckPlugin/Range.php')
-rw-r--r--library/Director/CheckPlugin/Range.php101
1 files changed, 101 insertions, 0 deletions
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 @@
+<?php
+
+namespace Icinga\Module\Director\CheckPlugin;
+
+use Icinga\Exception\ConfigurationError;
+
+class Range
+{
+ /** @var float|null */
+ protected $start = 0;
+
+ /** @var float|null */
+ protected $end = null;
+
+ /** @var bool */
+ protected $mustBeWithinRange = true;
+
+ public function __construct($start = 0, $end = null, $mustBeWithinRange = true)
+ {
+ $this->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;
+ }
+ }
+}