mirror of
https://github.com/Icinga/icingaweb2-module-director.git
synced 2025-07-29 00:34:05 +02:00
DbObject: provide boolean properties...
...as IcingaObject did, move convertion logic to DbDataFormatter
This commit is contained in:
parent
bbf85f052b
commit
2ade848f4d
@ -6,7 +6,7 @@ use InvalidArgumentException;
|
|||||||
|
|
||||||
class DbDataFormatter
|
class DbDataFormatter
|
||||||
{
|
{
|
||||||
public static function normalizeBoolean($value)
|
public static function normalizeBoolean($value): ?string
|
||||||
{
|
{
|
||||||
if ($value === 'y' || $value === '1' || $value === true || $value === 1) {
|
if ($value === 'y' || $value === '1' || $value === true || $value === 1) {
|
||||||
return 'y';
|
return 'y';
|
||||||
@ -23,4 +23,16 @@ class DbDataFormatter
|
|||||||
var_export($value, 1)
|
var_export($value, 1)
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function booleanForDbValue($value): ?bool
|
||||||
|
{
|
||||||
|
if ($value === 'y') {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if ($value === 'n') {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $value; // let this fail elsewhere, if not null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -80,6 +80,9 @@ abstract class DbObject
|
|||||||
|
|
||||||
protected $binaryProperties = [];
|
protected $binaryProperties = [];
|
||||||
|
|
||||||
|
/* key/value!! */
|
||||||
|
protected $booleans = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Filled with object instances when prefetchAll is used
|
* Filled with object instances when prefetchAll is used
|
||||||
*/
|
*/
|
||||||
@ -346,6 +349,10 @@ abstract class DbObject
|
|||||||
return $this->$func($value);
|
return $this->$func($value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($this->propertyIsBoolean($key)) {
|
||||||
|
$value = DbDataFormatter::normalizeBoolean($value);
|
||||||
|
}
|
||||||
|
|
||||||
if (! $this->hasProperty($key)) {
|
if (! $this->hasProperty($key)) {
|
||||||
throw new InvalidArgumentException(sprintf(
|
throw new InvalidArgumentException(sprintf(
|
||||||
'Trying to set invalid key "%s"',
|
'Trying to set invalid key "%s"',
|
||||||
@ -878,6 +885,11 @@ abstract class DbObject
|
|||||||
return in_array($column, $this->binaryProperties) || $this->getUuidColumn() === $column;
|
return in_array($column, $this->binaryProperties) || $this->getUuidColumn() === $column;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function propertyIsBoolean($property)
|
||||||
|
{
|
||||||
|
return array_key_exists($property, $this->booleans);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Store object to database
|
* Store object to database
|
||||||
*
|
*
|
||||||
|
@ -63,9 +63,6 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
|
|||||||
|
|
||||||
protected $type;
|
protected $type;
|
||||||
|
|
||||||
/* key/value!! */
|
|
||||||
protected $booleans = [];
|
|
||||||
|
|
||||||
// Property suffixed with _id must exist
|
// Property suffixed with _id must exist
|
||||||
protected $relations = [
|
protected $relations = [
|
||||||
// property => PropertyClass
|
// property => PropertyClass
|
||||||
@ -142,11 +139,6 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
|
|||||||
return $this->connection;
|
return $this->connection;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function propertyIsBoolean($property)
|
|
||||||
{
|
|
||||||
return array_key_exists($property, $this->booleans);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function propertyIsInterval($property)
|
public function propertyIsInterval($property)
|
||||||
{
|
{
|
||||||
return array_key_exists($property, $this->intervalProperties);
|
return array_key_exists($property, $this->intervalProperties);
|
||||||
@ -771,10 +763,6 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->propertyIsBoolean($key)) {
|
|
||||||
return parent::set($key, DbDataFormatter::normalizeBoolean($value));
|
|
||||||
}
|
|
||||||
|
|
||||||
// e.g. zone_id
|
// e.g. zone_id
|
||||||
if ($this->propertyIsRelation($key)) {
|
if ($this->propertyIsRelation($key)) {
|
||||||
return $this->setRelation($key, $value);
|
return $this->setRelation($key, $value);
|
||||||
@ -2894,7 +2882,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
|
|||||||
// TODO: Do not ship null properties based on flag?
|
// TODO: Do not ship null properties based on flag?
|
||||||
if (!$skipDefaults || $this->differsFromDefaultValue($k, $v)) {
|
if (!$skipDefaults || $this->differsFromDefaultValue($k, $v)) {
|
||||||
if ($k === 'disabled' || $this->propertyIsBoolean($k)) {
|
if ($k === 'disabled' || $this->propertyIsBoolean($k)) {
|
||||||
$props[$k] = $this->booleanForDbValue($v);
|
$props[$k] = DbDataFormatter::booleanForDbValue($v);
|
||||||
} else {
|
} else {
|
||||||
$props[$k] = $v;
|
$props[$k] = $v;
|
||||||
}
|
}
|
||||||
@ -3005,18 +2993,6 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
|
|||||||
return (object) $props;
|
return (object) $props;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function booleanForDbValue($value)
|
|
||||||
{
|
|
||||||
if ($value === 'y') {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if ($value === 'n') {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $value; // let this fail elsewhere, if not null
|
|
||||||
}
|
|
||||||
|
|
||||||
public function listImportNames()
|
public function listImportNames()
|
||||||
{
|
{
|
||||||
if ($this->gotImports()) {
|
if ($this->gotImports()) {
|
||||||
@ -3161,7 +3137,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
|
|||||||
|
|
||||||
if ($this->differsFromDefaultValue($k, $v)) {
|
if ($this->differsFromDefaultValue($k, $v)) {
|
||||||
if ($k === 'disabled' || $this->propertyIsBoolean($k)) {
|
if ($k === 'disabled' || $this->propertyIsBoolean($k)) {
|
||||||
$props[$k] = $this->booleanForDbValue($v);
|
$props[$k] = DbDataFormatter::booleanForDbValue($v);
|
||||||
} else {
|
} else {
|
||||||
$props[$k] = $v;
|
$props[$k] = $v;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user