summaryrefslogtreecommitdiffstats
path: root/library/Director/CustomVariable/CustomVariableNumber.php
diff options
context:
space:
mode:
Diffstat (limited to 'library/Director/CustomVariable/CustomVariableNumber.php')
-rw-r--r--library/Director/CustomVariable/CustomVariableNumber.php73
1 files changed, 73 insertions, 0 deletions
diff --git a/library/Director/CustomVariable/CustomVariableNumber.php b/library/Director/CustomVariable/CustomVariableNumber.php
new file mode 100644
index 0000000..62838a9
--- /dev/null
+++ b/library/Director/CustomVariable/CustomVariableNumber.php
@@ -0,0 +1,73 @@
+<?php
+
+namespace Icinga\Module\Director\CustomVariable;
+
+use Icinga\Exception\ProgrammingError;
+
+class CustomVariableNumber extends CustomVariable
+{
+ // Hint: 'F' is intentional, this MUST NOT respect locales
+ const PRECISION = '%.9F';
+
+ public function equals(CustomVariable $var)
+ {
+ if (! $var instanceof CustomVariableNumber) {
+ return false;
+ }
+
+ $cur = $this->getValue();
+ $new = $var->getValue();
+
+ // Be tolerant when comparing floats:
+ if (is_float($cur) || is_float($new)) {
+ return sprintf(self::PRECISION, $cur)
+ === sprintf(self::PRECISION, $new);
+ }
+
+ return $cur === $new;
+ }
+
+ public function getDbFormat()
+ {
+ return 'json';
+ }
+
+ public function getDbValue()
+ {
+ return json_encode($this->getValue());
+ }
+
+ public function getValue()
+ {
+ return $this->value;
+ }
+
+ public function setValue($value)
+ {
+ if (! is_int($value) && ! is_float($value)) {
+ throw new ProgrammingError(
+ 'Expected a number, got %s',
+ var_export($value, 1)
+ );
+ }
+
+ $this->value = $value;
+ $this->deleted = false;
+
+ return $this;
+ }
+
+ public function toConfigString($renderExpressions = false)
+ {
+ if (is_int($this->value)) {
+ return (string) $this->value;
+ } else {
+ return sprintf(self::PRECISION, $this->value);
+ }
+ }
+
+ public function toLegacyConfigString()
+ {
+ return $this->toConfigString();
+ }
+}