preparations for SaveEntity
parent
7783b245df
commit
c0dd69eac0
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kartierung\Repository;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
class InvalidCall extends RuntimeException
|
||||
{
|
||||
public function __construct(string $message = "")
|
||||
{
|
||||
parent::__construct($message);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kartierung\Repository\ResolutionStrategy;
|
||||
|
||||
use Kartierung\Analyze\Result\EntityField;
|
||||
use Kartierung\Analyze\Result\EntityResult;
|
||||
use Kartierung\Analyze\Result\FieldAccess;
|
||||
use Kartierung\Analyze\Result\Type\ArrayType;
|
||||
use Kartierung\Analyze\Result\Type\ClassType;
|
||||
|
||||
class EntityToDataMapper
|
||||
{
|
||||
/**
|
||||
* @template E of object
|
||||
* @param EntityResult<E> $entityResult
|
||||
* @param E $entity
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function byColumnName(EntityResult $entityResult, object $entity): array
|
||||
{
|
||||
$result = [];
|
||||
|
||||
foreach ($entityResult->fields as $field) {
|
||||
$type = $field->type;
|
||||
$value = $this->fieldFromEntity($field, $entity);
|
||||
|
||||
if ($type instanceof ArrayType) {
|
||||
/** @var array<object> $value */
|
||||
$result[$field->columnName] = array_map(
|
||||
fn(object $listValue) => $this->byColumnName($type->entity, $listValue),
|
||||
$value
|
||||
);
|
||||
} elseif ($type instanceof ClassType) {
|
||||
/** @var object $value */
|
||||
$result[$field->columnName] = $this->byColumnName($type->entity, $value);
|
||||
} else {
|
||||
$result[$field->columnName] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function fieldFromEntity(EntityField $field, object $entity): mixed
|
||||
{
|
||||
return match ($field->readAccess) {
|
||||
FieldAccess::PUBLIC => $entity->{$field->name},
|
||||
FieldAccess::GETTER => $entity->{'get' . ucfirst($field->name)}()
|
||||
};
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kartierung\Repository\ResolutionStrategy;
|
||||
|
||||
use Kartierung\Analyze\Result\EntityField;
|
||||
use Kartierung\Analyze\Result\EntityResult;
|
||||
use Kartierung\Analyze\Result\FieldAccess;
|
||||
use Kartierung\Analyze\Result\Type\PrimitiveType;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class EntityToDataMapperTest extends TestCase
|
||||
{
|
||||
public function testMapsByColumnName(): void
|
||||
{
|
||||
// Given
|
||||
$entity = new SimpleEntity(1, "2");
|
||||
$entityResult = new EntityResult(
|
||||
classFqcn: '',
|
||||
tableName: '',
|
||||
fields: [
|
||||
new EntityField(
|
||||
name: 'intcol',
|
||||
type: PrimitiveType::INTEGER,
|
||||
columnName: 'intcol',
|
||||
isIdField: true,
|
||||
readAccess: FieldAccess::PUBLIC
|
||||
),
|
||||
new EntityField(
|
||||
name: 'stringcol',
|
||||
type: PrimitiveType::INTEGER,
|
||||
columnName: 'stringcol',
|
||||
isIdField: true,
|
||||
readAccess: FieldAccess::PUBLIC
|
||||
)
|
||||
],
|
||||
idField: new EntityField(
|
||||
name: '',
|
||||
type: PrimitiveType::INTEGER,
|
||||
columnName: '',
|
||||
isIdField: true,
|
||||
readAccess: FieldAccess::PUBLIC
|
||||
)
|
||||
);
|
||||
|
||||
// When
|
||||
$result = (new EntityToDataMapper())->byColumnName(
|
||||
$entityResult, $entity
|
||||
);
|
||||
|
||||
// Then
|
||||
$this->assertEquals(
|
||||
[
|
||||
'intcol' => 1,
|
||||
'stringcol' => 2
|
||||
],
|
||||
$result
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue