You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
2.0 KiB
PHP
80 lines
2.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Kartierung\Sql;
|
|
|
|
use Kartierung\Analyze\EntityField;
|
|
use Kartierung\Analyze\EntityResult;
|
|
use Kartierung\Analyze\FieldAccess;
|
|
|
|
/**
|
|
* @template E of object
|
|
*/
|
|
readonly class DataToEntityMapper
|
|
{
|
|
/**
|
|
* @param EntityResult<E> $entityResult
|
|
*/
|
|
public function __construct(
|
|
private EntityResult $entityResult
|
|
) {}
|
|
|
|
/**
|
|
* @param array<string, mixed> $row
|
|
* @return E
|
|
*/
|
|
public function toEntity(array $row): object
|
|
{
|
|
/** @var E $entity */
|
|
$entity = new $this->entityResult->classFqcn();
|
|
|
|
foreach ($this->entityResult->fields as $field) {
|
|
/** @var object $dbValue */
|
|
$dbValue = $row[$field->columnName];
|
|
|
|
match ($field->writeAccess) {
|
|
FieldAccess::GETSET => $this->getsetWrite($entity, $field, $dbValue),
|
|
FieldAccess::PUBLIC => $this->publicWrite($entity, $field, $dbValue),
|
|
FieldAccess::WITHER => $entity = $this->witherWrite($entity, $field, $dbValue),
|
|
};
|
|
}
|
|
|
|
return $entity;
|
|
}
|
|
|
|
/**
|
|
* @param E $entity
|
|
* @param EntityField<object> $field
|
|
* @param object $value
|
|
*/
|
|
private function getsetWrite(object $entity, EntityField $field, mixed $value): void
|
|
{
|
|
$setter = FieldAccess::setterName($field->name);
|
|
$entity->$setter($value);
|
|
}
|
|
|
|
/**
|
|
* @param E $entity
|
|
* @param EntityField<object> $field
|
|
* @param object $value
|
|
*/
|
|
private function publicWrite(object $entity, EntityField $field, mixed $value): void
|
|
{
|
|
$entity->{$field->name} = $value;
|
|
}
|
|
|
|
/**
|
|
* @param E $entity
|
|
* @param EntityField<object> $field
|
|
* @param object $value
|
|
* @return E
|
|
*/
|
|
private function witherWrite(object $entity, EntityField $field, mixed $value): object
|
|
{
|
|
$wither = FieldAccess::witherName($field->name);
|
|
/** @var E */
|
|
return $entity->$wither($value);
|
|
}
|
|
}
|