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

View File

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