DbObject: allow arrays in fromDbRow, handle errors

This commit is contained in:
Thomas Gelf 2021-10-05 23:27:07 +02:00
parent 6a68a2c3d8
commit 2994403aa8
1 changed files with 12 additions and 4 deletions

View File

@ -3,6 +3,7 @@
namespace Icinga\Module\Director\Data\Db;
use Icinga\Exception\NotFoundError;
use Icinga\Module\Director\Data\InvalidDataException;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Exception\DuplicateKeyException;
use InvalidArgumentException;
@ -696,15 +697,22 @@ abstract class DbObject
}
/**
* @param object $row
* @param object|array $row
* @param Db $db
* @return self
*/
public static function fromDbRow($row, Db $db)
{
return (new static())
->setConnection($db)
->setDbProperties($row);
$self = (new static())->setConnection($db);
if (is_object($row)) {
return $self->setDbProperties((array) $row);
}
if (is_array($row)) {
return $self->setDbProperties($row);
}
throw new InvalidDataException('array or object', $row);
}
protected function setDbProperties($properties)