edit docblock + add factory support

This commit is contained in:
Dennis Eichhorn 2023-01-26 21:39:53 +01:00
parent 778a8f02eb
commit 6e42fe49a1
2 changed files with 24 additions and 10 deletions

View File

@ -85,7 +85,7 @@ class DataMapperFactory
* *
* Relation is defined in current mapper * Relation is defined in current mapper
* *
* @var array<string, array{mapper:string, external:string, by?:string, column?:string, conditional?:bool}> * @var array<string, array{mapper:class-string, external:string, by?:string, column?:string, conditional?:bool}>
* @since 1.0.0 * @since 1.0.0
*/ */
public const OWNS_ONE = []; public const OWNS_ONE = [];
@ -93,7 +93,7 @@ class DataMapperFactory
/** /**
* Belongs to. * Belongs to.
* *
* @var array<string, array{mapper:string, external:string, column?:string, by?:string}> * @var array<string, array{mapper:class-string, external:string, column?:string, by?:string}>
* @since 1.0.0 * @since 1.0.0
*/ */
public const BELONGS_TO = []; public const BELONGS_TO = [];
@ -109,7 +109,7 @@ class DataMapperFactory
/** /**
* Parent column. * Parent column.
* *
* @var string * @var class-string
* @since 1.0.0 * @since 1.0.0
*/ */
public const PARENT = ''; public const PARENT = '';
@ -117,11 +117,19 @@ class DataMapperFactory
/** /**
* Model to use by the mapper. * Model to use by the mapper.
* *
* @var string * @var class-string
* @since 1.0.0 * @since 1.0.0
*/ */
public const MODEL = ''; public const MODEL = '';
/**
* Model factory to use by the mapper.
*
* @var class-string
* @since 1.0.0
*/
public const FACTORY = '';
/** /**
* Database connection. * Database connection.
* *
@ -387,15 +395,21 @@ class DataMapperFactory
/** /**
* Create the empty base model * Create the empty base model
* *
* @param null|array $data Data to use for initialization
*
* @return object * @return object
* *
* @since 1.0.0 * @since 1.0.0
*/ */
public static function createBaseModel() : object public static function createBaseModel(array $data = null) : object
{ {
$class = empty(static::MODEL) ? \substr(static::class, 0, -6) : static::MODEL; if (empty(static::FACTORY)) {
$class = empty(static::MODEL) ? \substr(static::class, 0, -6) : static::MODEL;
return new $class(); return new $class();
}
return static::FACTORY::createWith($data);
} }
/** /**

View File

@ -187,7 +187,7 @@ final class ReadMapper extends DataMapperAbstract
foreach ($dbData as $row) { foreach ($dbData as $row) {
$value = $row[$this->mapper::PRIMARYFIELD . '_d' . $this->depth]; $value = $row[$this->mapper::PRIMARYFIELD . '_d' . $this->depth];
$obj[$value] = $this->mapper::createBaseModel(); $obj[$value] = $this->mapper::createBaseModel($row);
$obj[$value] = $this->populateAbstract($row, $obj[$value]); $obj[$value] = $this->populateAbstract($row, $obj[$value]);
$this->loadHasManyRelations($obj[$value]); $this->loadHasManyRelations($obj[$value]);
@ -743,7 +743,7 @@ final class ReadMapper extends DataMapperAbstract
$ownsOneMapper = $this->createRelationMapper($mapper::get($this->db), $member); $ownsOneMapper = $this->createRelationMapper($mapper::get($this->db), $member);
$ownsOneMapper->depth = $this->depth + 1; $ownsOneMapper->depth = $this->depth + 1;
return $ownsOneMapper->populateAbstract($result, $mapper::createBaseModel()); return $ownsOneMapper->populateAbstract($result, $mapper::createBaseModel($result));
} }
/** /**
@ -804,7 +804,7 @@ final class ReadMapper extends DataMapperAbstract
$belongsToMapper = $this->createRelationMapper($mapper::get($this->db), $member); $belongsToMapper = $this->createRelationMapper($mapper::get($this->db), $member);
$belongsToMapper->depth = $this->depth + 1; $belongsToMapper->depth = $this->depth + 1;
return $belongsToMapper->populateAbstract($result, $mapper::createBaseModel()); return $belongsToMapper->populateAbstract($result, $mapper::createBaseModel($result));
} }
/** /**