Compare commits

..

No commits in common. 'c439307d81df21a35a6891e458981f954b3039f4' and 'fca165783eaa16e40d5ff19389bd94315934a45f' have entirely different histories.

@ -9,7 +9,6 @@ use Kartierung\Attribute\Id;
use Kartierung\Attribute\Table; use Kartierung\Attribute\Table;
use ReflectionClass; use ReflectionClass;
use ReflectionNamedType; use ReflectionNamedType;
use ReflectionParameter;
use ReflectionProperty; use ReflectionProperty;
/** /**
@ -50,6 +49,7 @@ readonly class EntityAnalyzer
$attrs = $refClass->getAttributes(Table::class); $attrs = $refClass->getAttributes(Table::class);
if (count($attrs) !== 1) { if (count($attrs) !== 1) {
//throw new InvalidEntity('Attribute #[Table] not found on entity ' . $refClass->name);
return $refClass->getShortName(); return $refClass->getShortName();
} }
@ -63,17 +63,9 @@ readonly class EntityAnalyzer
*/ */
private function fields(ReflectionClass $refClass): array private function fields(ReflectionClass $refClass): array
{ {
$ctor = $refClass->getConstructor(); $props = $refClass->getProperties();
if (!$ctor) {
throw new InvalidEntity(
"Entity $this->entity does not declare a constructor. Implement one accepting all of the "
. "entitie's values."
);
}
$params = $ctor->getParameters(); return array_map(fn(ReflectionProperty $prop) => $this->fieldFromProperty($prop), $props);
return array_map(fn(ReflectionParameter $param) => $this->fieldFromParameter($param), $params);
} }
/** /**
@ -85,61 +77,93 @@ readonly class EntityAnalyzer
$idFields = array_filter($fields, static fn(EntityField $f) => $f->isIdField); $idFields = array_filter($fields, static fn(EntityField $f) => $f->isIdField);
$nrIdFields = count($idFields); $nrIdFields = count($idFields);
if ($nrIdFields !== 1) { if ($nrIdFields === 0 || $nrIdFields > 1) {
throw new InvalidEntity( throw new InvalidEntity("Invalid number of id fields found: $nrIdFields, expected: 1");
"Invalid number of id fields on entity $this->entity found: $nrIdFields, expected: 1"
);
} }
return $idFields[array_key_first($idFields)]; return $idFields[array_key_first($idFields)];
} }
/** /**
* @param ReflectionProperty $prop
* @return EntityField<E> * @return EntityField<E>
*/ */
private function fieldFromParameter(ReflectionParameter $param): EntityField private function fieldFromProperty(ReflectionProperty $prop): EntityField
{ {
$attrs = $param->getAttributes(Column::class); $attrs = $prop->getAttributes(Column::class);
if (count($attrs) === 1) { if (count($attrs) === 1) {
/** @var Column $column */ /** @var string $name */
$column = $attrs[0]->newInstance(); $name = $attrs[0]->getArguments()[0];
$name = $column->name ?: $param->getName();
} else { } else {
$name = $param->getName(); $name = $prop->getName();
} }
$refClass = $param->getDeclaringClass(); $refClass = $prop->getDeclaringClass();
$readAccess = $this->readAccess($refClass, $param); $writeAccess = $this->writeAccess($refClass, $prop);
$readAccess = $this->readAccess($refClass, $prop);
return new EntityField( return new EntityField(
name: $param->name, name: $prop->name,
fqcn: $this->entity, fqcn: $this->entity,
columnName: $name, columnName: $name,
isIdField: count($param->getAttributes(Id::class)) > 0, isIdField: count($prop->getAttributes(Id::class)) > 0,
writeAccess: $writeAccess,
readAccess: $readAccess readAccess: $readAccess
); );
} }
/** /**
* @param ReflectionClass<E> $refClass * @param ReflectionClass<E> $refClass
* @param ReflectionProperty $prop
* @return FieldAccess * @return FieldAccess
*/ */
public function readAccess(ReflectionClass $refClass, ReflectionParameter $param): FieldAccess public function writeAccess(ReflectionClass $refClass, ReflectionProperty $prop): FieldAccess
{ {
$getter = 'get' . ucfirst($param->getName()); $wither = FieldAccess::witherName($prop->getName());
$setter = FieldAccess::setterName($prop->getName());
if ($refClass->hasMethod($setter)) {
$writeAccess = FieldAccess::GETSET;
} elseif ($refClass->hasMethod($wither)) {
$returnType = $refClass->getMethod($wither)->getReturnType();
if (!$returnType instanceof ReflectionNamedType || $returnType->getName() !== 'self') {
throw new InvalidEntity(
"Field $refClass->name::$prop->name defines wither writing method with invalid return type, " .
"expecting `self`."
);
}
$hasPublicProp = $refClass->hasProperty($param->getName()) $writeAccess = FieldAccess::WITHER;
&& $refClass->getProperty($param->getName())->isPublic(); } elseif ($prop->isPublic() && !$prop->isReadOnly()) {
$writeAccess = FieldAccess::PUBLIC;
} else {
throw new InvalidEntity(
"Field $refClass->name::$prop->name has no valid writing method. "
. "Implement $setter, $wither or make the field public."
);
}
return $writeAccess;
}
/**
* @param ReflectionClass<E> $refClass
* @param ReflectionProperty $prop
* @return FieldAccess
*/
public function readAccess(ReflectionClass $refClass, ReflectionProperty $prop): FieldAccess
{
$getter = 'get' . ucfirst($prop->getName());
if ($refClass->hasMethod($getter)) { if ($refClass->hasMethod($getter)) {
$readAccess = FieldAccess::GETTER; $readAccess = FieldAccess::GETSET;
} elseif ($hasPublicProp) { } elseif ($prop->isPublic()) {
$readAccess = FieldAccess::PUBLIC; $readAccess = FieldAccess::PUBLIC;
} else { } else {
throw new InvalidEntity( throw new InvalidEntity(
"Constructor parameter of entity $this->entity '$param->name' has no valid reading method. " "Field $refClass->name::$prop->name has no valid reading method. "
. "Implement $getter() or make the field public." . "Implement $getter or make the field public."
); );
} }

@ -17,6 +17,7 @@ readonly class EntityField
public string $fqcn, public string $fqcn,
public string $columnName, public string $columnName,
public bool $isIdField, public bool $isIdField,
public FieldAccess $writeAccess,
public FieldAccess $readAccess, public FieldAccess $readAccess,
) {} ) {}
} }

@ -6,6 +6,17 @@ namespace Kartierung\Analyze;
enum FieldAccess enum FieldAccess
{ {
case GETTER; case GETSET;
case PUBLIC; case PUBLIC;
case WITHER;
public static function setterName(string $fieldName): string
{
return 'set' . ucfirst($fieldName);
}
public static function witherName(string $fieldName): string
{
return 'with' . ucfirst($fieldName);
}
} }

@ -6,7 +6,7 @@ namespace Kartierung\Attribute;
use Attribute; use Attribute;
#[Attribute(Attribute::TARGET_PARAMETER)] #[Attribute(Attribute::TARGET_PROPERTY)]
readonly class Column readonly class Column
{ {
public function __construct( public function __construct(

@ -26,15 +26,21 @@ readonly class DataToEntityMapper
*/ */
public function toEntity(array $row): object public function toEntity(array $row): object
{ {
$params = []; /** @var E $entity */
$entity = new $this->entityResult->classFqcn();
foreach ($this->entityResult->fields as $field) { foreach ($this->entityResult->fields as $field) {
/** @var object $dbValue */ /** @var object $dbValue */
$dbValue = $row[$field->columnName]; $dbValue = $row[$field->columnName];
$params[] = $dbValue;
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),
};
} }
/** @var E */ return $entity;
return new $this->entityResult->classFqcn(...$params);
} }
/** /**

@ -5,10 +5,8 @@ declare(strict_types=1);
namespace Kartierung\Analyze; namespace Kartierung\Analyze;
use Kartierung\EntityWithoutId; use Kartierung\EntityWithoutId;
use Kartierung\EntityWithoutTableAttribute;
use Kartierung\EntityWithTwoIds; use Kartierung\EntityWithTwoIds;
use Kartierung\FullEntity; use Kartierung\FullEntity;
use Kartierung\PropertyPropagatedEntity;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
class EntityAnalyzerTest extends TestCase class EntityAnalyzerTest extends TestCase
@ -35,13 +33,25 @@ class EntityAnalyzerTest extends TestCase
$result = (new EntityAnalyzer($entity))->analyze(); $result = (new EntityAnalyzer($entity))->analyze();
// Then // Then
$this->assertContainsEquals(
new EntityField(
name: 'stringField',
fqcn: $entity,
columnName: 'stringField',
isIdField: false,
writeAccess: FieldAccess::WITHER,
readAccess: FieldAccess::PUBLIC
),
$result->fields
);
$this->assertContainsEquals( $this->assertContainsEquals(
new EntityField( new EntityField(
name: 'intField', name: 'intField',
fqcn: $entity, fqcn: $entity,
columnName: 'int-field', columnName: 'int-field',
isIdField: false, isIdField: false,
readAccess: FieldAccess::GETTER writeAccess: FieldAccess::PUBLIC,
readAccess: FieldAccess::GETSET
), ),
$result->fields $result->fields
); );
@ -51,6 +61,7 @@ class EntityAnalyzerTest extends TestCase
fqcn: $entity, fqcn: $entity,
columnName: 'renamed-id-field', columnName: 'renamed-id-field',
isIdField: true, isIdField: true,
writeAccess: FieldAccess::GETSET,
readAccess: FieldAccess::PUBLIC readAccess: FieldAccess::PUBLIC
), ),
$result->fields $result->fields
@ -72,6 +83,7 @@ class EntityAnalyzerTest extends TestCase
fqcn: $entity, fqcn: $entity,
columnName: 'renamed-id-field', columnName: 'renamed-id-field',
isIdField: true, isIdField: true,
writeAccess: FieldAccess::GETSET,
readAccess: FieldAccess::PUBLIC readAccess: FieldAccess::PUBLIC
), ),
$result->idField $result->idField
@ -101,16 +113,4 @@ class EntityAnalyzerTest extends TestCase
// When // When
(new EntityAnalyzer($entity))->analyze(); (new EntityAnalyzer($entity))->analyze();
} }
public function testTableAttributeIsOptional(): void
{
// Given
$entity = EntityWithoutTableAttribute::class;
// When
$result = (new EntityAnalyzer($entity))->analyze();
// Then
$this->assertEquals("EntityWithoutTableAttribute", $result->tableName);
}
} }

@ -1,15 +0,0 @@
<?php
declare(strict_types=1);
namespace Kartierung;
use Kartierung\Attribute\Id;
class EntityWithoutTableAttribute
{
public function __construct(
#[Id]
public $id
) {}
}

@ -11,13 +11,12 @@ use Kartierung\Attribute\Table;
#[Table("simple-table")] #[Table("simple-table")]
class FullEntity class FullEntity
{ {
public function __construct( public string $stringField;
#[Column('int-field')] #[Column('int-field')]
public int $intField, public int $intField;
#[Id] #[Id]
#[Column('renamed-id-field')] #[Column('renamed-id-field')]
public int $idField public int $idField;
) {}
public function setIdField(int $idField): void public function setIdField(int $idField): void
{ {
@ -28,4 +27,10 @@ class FullEntity
{ {
return $this->intField; return $this->intField;
} }
public function withStringField(string $stringField): self
{
$this->stringField = $stringField;
return $this;
}
} }

@ -45,39 +45,4 @@ class ResolutionStrategyFactoryTest extends TestCase
// Then // Then
$this->assertInstanceOf($strategyClass, $function); $this->assertInstanceOf($strategyClass, $function);
} }
public static function userDefinedPrecedenceProvider(): array
{
return [
['R', 'save', 'UPDATE', '', null, [], UserDefinedQuery::class],
['R', 'delete', 'DELETE', '', null, [], UserDefinedQuery::class],
['R', 'findById', 'SELECT', '', null, [], UserDefinedQuery::class],
['R', 'findAll', 'SELECT', '', null, [], UserDefinedQuery::class],
];
}
#[DataProvider('userDefinedPrecedenceProvider')]
public function testUserDefinedQueryHasAlwaysPrecedence(
string $repName,
string $name,
?string $query,
string $returnType,
?string $listType,
array $params,
string $strategyClass
): void
{
// When
$function = (new ResolutionStrategyFactory())->forRepositoryMethod(
$repName,
$name,
$query,
$returnType,
$listType,
$params
);
// Then
$this->assertInstanceOf($strategyClass, $function);
}
} }

@ -8,11 +8,10 @@ use Kartierung\Attribute\Id;
class SimpleEntity class SimpleEntity
{ {
public function __construct( public string $stringcol;
#[Id] #[Id]
public int $intcol, public int $intcol;
public string $stringcol
) {}
public function withStringCol(string $value): self public function withStringCol(string $value): self
{ {

@ -16,14 +16,23 @@ class DateToEntityMapperTest extends TestCase
// Given // Given
$mapper = new DataToEntityMapper( $mapper = new DataToEntityMapper(
entityResult: new EntityResult( entityResult: new EntityResult(
classFqcn: EntityWithAllReadMechanics::class, classFqcn: EntityWithThreeAccessMethods::class,
tableName: '', tableName: '',
fields: [ fields: [
new EntityField(
name: 'witherVal',
fqcn: 'int',
columnName: 'wither_val',
isIdField: false,
writeAccess: FieldAccess::WITHER,
readAccess: FieldAccess::WITHER
),
new EntityField( new EntityField(
name: 'publicVal', name: 'publicVal',
fqcn: 'int', fqcn: 'int',
columnName: 'public_val', columnName: 'public_val',
isIdField: false, isIdField: false,
writeAccess: FieldAccess::PUBLIC,
readAccess: FieldAccess::PUBLIC readAccess: FieldAccess::PUBLIC
), ),
new EntityField( new EntityField(
@ -31,7 +40,8 @@ class DateToEntityMapperTest extends TestCase
fqcn: 'int', fqcn: 'int',
columnName: 'setter_val', columnName: 'setter_val',
isIdField: false, isIdField: false,
readAccess: FieldAccess::GETTER writeAccess: FieldAccess::GETSET,
readAccess: FieldAccess::GETSET
) )
], ],
idField: new EntityField( idField: new EntityField(
@ -39,12 +49,14 @@ class DateToEntityMapperTest extends TestCase
fqcn: '', fqcn: '',
columnName: '', columnName: '',
isIdField: false, isIdField: false,
readAccess: FieldAccess::GETTER writeAccess: FieldAccess::GETSET,
readAccess: FieldAccess::GETSET
) )
) )
); );
$row = [ $row = [
'wither_val' => 1,
'public_val' => 2, 'public_val' => 2,
'setter_val' => 3 'setter_val' => 3
]; ];
@ -53,7 +65,8 @@ class DateToEntityMapperTest extends TestCase
$entity = $mapper->toEntity($row); $entity = $mapper->toEntity($row);
// Then // Then
$this->assertInstanceOf(EntityWithAllReadMechanics::class, $entity); $this->assertInstanceOf(EntityWithThreeAccessMethods::class, $entity);
$this->assertEquals(1, $entity->getWitherVal());
$this->assertEquals(2, $entity->publicVal); $this->assertEquals(2, $entity->publicVal);
$this->assertEquals(3, $entity->getSetterVal()); $this->assertEquals(3, $entity->getSetterVal());
} }

@ -1,18 +0,0 @@
<?php
declare(strict_types=1);
namespace Kartierung\Sql;
class EntityWithAllReadMechanics
{
public function __construct(
public int $publicVal,
private int $setterVal
) {}
public function getSetterVal(): int
{
return $this->setterVal;
}
}

@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace Kartierung\Sql;
class EntityWithThreeAccessMethods
{
public int $publicVal;
private int $setterVal;
private int $witherVal;
public function getSetterVal(): int
{
return $this->setterVal;
}
public function setSetterVal(int $value): void
{
$this->setterVal = $value;
}
public function withWitherVal(int $value): self
{
$this->witherVal = $value;
return $this;
}
public function getWitherVal(): int
{
return $this->witherVal;
}
}
Loading…
Cancel
Save