2015-10-28 22:27:24 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\CustomVariable;
|
|
|
|
|
|
|
|
use Icinga\Exception\ProgrammingError;
|
|
|
|
|
|
|
|
class CustomVariableNumber extends CustomVariable
|
|
|
|
{
|
2016-10-06 18:18:34 +02:00
|
|
|
// Hint: 'F' is intentional, this MUST NOT respect locales
|
|
|
|
const PRECISION = '%.9F';
|
|
|
|
|
2015-10-28 22:27:24 +01:00
|
|
|
public function equals(CustomVariable $var)
|
|
|
|
{
|
2016-02-23 09:02:51 +01:00
|
|
|
if (! $var instanceof CustomVariableNumber) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-06 18:18:34 +02:00
|
|
|
$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);
|
2016-02-23 09:09:47 +01:00
|
|
|
}
|
2016-10-06 18:18:34 +02:00
|
|
|
|
|
|
|
return $cur === $new;
|
2015-10-28 22:27:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getDbFormat()
|
|
|
|
{
|
|
|
|
return 'json';
|
|
|
|
}
|
|
|
|
|
2016-02-23 10:15:39 +01:00
|
|
|
public function getDbValue()
|
|
|
|
{
|
|
|
|
return json_encode($this->getValue());
|
|
|
|
}
|
|
|
|
|
2015-10-28 22:27:24 +01:00
|
|
|
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;
|
2016-09-09 00:40:07 +02:00
|
|
|
$this->deleted = false;
|
2015-10-28 22:27:24 +01:00
|
|
|
|
|
|
|
return $this;
|
2016-02-26 11:58:37 +01:00
|
|
|
}
|
2015-10-28 22:27:24 +01:00
|
|
|
|
|
|
|
public function toConfigString()
|
|
|
|
{
|
2015-11-06 09:33:42 +01:00
|
|
|
if (is_int($this->value)) {
|
|
|
|
return (string) $this->value;
|
|
|
|
} else {
|
2016-10-06 18:18:34 +02:00
|
|
|
return sprintf(self::PRECISION, $this->value);
|
2015-11-06 09:33:42 +01:00
|
|
|
}
|
2015-10-28 22:27:24 +01:00
|
|
|
}
|
|
|
|
}
|