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
|
||||
{
|
||||
public static function normalizeBoolean($value)
|
||||
public static function normalizeBoolean($value): ?string
|
||||
{
|
||||
if ($value === 'y' || $value === '1' || $value === true || $value === 1) {
|
||||
return 'y';
|
||||
|
@ -23,4 +23,16 @@ class DbDataFormatter
|
|||
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 = [];
|
||||
|
||||
/* key/value!! */
|
||||
protected $booleans = [];
|
||||
|
||||
/**
|
||||
* Filled with object instances when prefetchAll is used
|
||||
*/
|
||||
|
@ -346,6 +349,10 @@ abstract class DbObject
|
|||
return $this->$func($value);
|
||||
}
|
||||
|
||||
if ($this->propertyIsBoolean($key)) {
|
||||
$value = DbDataFormatter::normalizeBoolean($value);
|
||||
}
|
||||
|
||||
if (! $this->hasProperty($key)) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'Trying to set invalid key "%s"',
|
||||
|
@ -878,6 +885,11 @@ abstract class DbObject
|
|||
return in_array($column, $this->binaryProperties) || $this->getUuidColumn() === $column;
|
||||
}
|
||||
|
||||
public function propertyIsBoolean($property)
|
||||
{
|
||||
return array_key_exists($property, $this->booleans);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store object to database
|
||||
*
|
||||
|
|
|
@ -63,9 +63,6 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
|
|||
|
||||
protected $type;
|
||||
|
||||
/* key/value!! */
|
||||
protected $booleans = [];
|
||||
|
||||
// Property suffixed with _id must exist
|
||||
protected $relations = [
|
||||
// property => PropertyClass
|
||||
|
@ -142,11 +139,6 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
|
|||
return $this->connection;
|
||||
}
|
||||
|
||||
public function propertyIsBoolean($property)
|
||||
{
|
||||
return array_key_exists($property, $this->booleans);
|
||||
}
|
||||
|
||||
public function propertyIsInterval($property)
|
||||
{
|
||||
return array_key_exists($property, $this->intervalProperties);
|
||||
|
@ -771,10 +763,6 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
|
|||
return $this;
|
||||
}
|
||||
|
||||
if ($this->propertyIsBoolean($key)) {
|
||||
return parent::set($key, DbDataFormatter::normalizeBoolean($value));
|
||||
}
|
||||
|
||||
// e.g. zone_id
|
||||
if ($this->propertyIsRelation($key)) {
|
||||
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?
|
||||
if (!$skipDefaults || $this->differsFromDefaultValue($k, $v)) {
|
||||
if ($k === 'disabled' || $this->propertyIsBoolean($k)) {
|
||||
$props[$k] = $this->booleanForDbValue($v);
|
||||
$props[$k] = DbDataFormatter::booleanForDbValue($v);
|
||||
} else {
|
||||
$props[$k] = $v;
|
||||
}
|
||||
|
@ -3005,18 +2993,6 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
|
|||
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()
|
||||
{
|
||||
if ($this->gotImports()) {
|
||||
|
@ -3161,7 +3137,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
|
|||
|
||||
if ($this->differsFromDefaultValue($k, $v)) {
|
||||
if ($k === 'disabled' || $this->propertyIsBoolean($k)) {
|
||||
$props[$k] = $this->booleanForDbValue($v);
|
||||
$props[$k] = DbDataFormatter::booleanForDbValue($v);
|
||||
} else {
|
||||
$props[$k] = $v;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue