|
|
|
@ -5,14 +5,20 @@ declare(strict_types=1);
|
|
|
|
namespace Kartierung\Analyze;
|
|
|
|
namespace Kartierung\Analyze;
|
|
|
|
|
|
|
|
|
|
|
|
use Kartierung\Analyze\Exception\InvalidEntity;
|
|
|
|
use Kartierung\Analyze\Exception\InvalidEntity;
|
|
|
|
|
|
|
|
use Kartierung\Analyze\Exception\InvalidRepository;
|
|
|
|
|
|
|
|
use Kartierung\Analyze\Result\ClassType;
|
|
|
|
use Kartierung\Analyze\Result\EntityField;
|
|
|
|
use Kartierung\Analyze\Result\EntityField;
|
|
|
|
use Kartierung\Analyze\Result\EntityResult;
|
|
|
|
use Kartierung\Analyze\Result\EntityResult;
|
|
|
|
use Kartierung\Analyze\Result\FieldAccess;
|
|
|
|
use Kartierung\Analyze\Result\FieldAccess;
|
|
|
|
|
|
|
|
use Kartierung\Analyze\Result\PrimitiveType;
|
|
|
|
use Kartierung\Attribute\Column;
|
|
|
|
use Kartierung\Attribute\Column;
|
|
|
|
use Kartierung\Attribute\Id;
|
|
|
|
use Kartierung\Attribute\Id;
|
|
|
|
use Kartierung\Attribute\Table;
|
|
|
|
use Kartierung\Attribute\Table;
|
|
|
|
use ReflectionClass;
|
|
|
|
use ReflectionClass;
|
|
|
|
|
|
|
|
use ReflectionIntersectionType;
|
|
|
|
|
|
|
|
use ReflectionNamedType;
|
|
|
|
use ReflectionParameter;
|
|
|
|
use ReflectionParameter;
|
|
|
|
|
|
|
|
use ReflectionUnionType;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @template E of object
|
|
|
|
* @template E of object
|
|
|
|
@ -61,7 +67,7 @@ readonly class EntityAnalyzer
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @param ReflectionClass<E> $refClass
|
|
|
|
* @param ReflectionClass<E> $refClass
|
|
|
|
* @return list<EntityField<E>>
|
|
|
|
* @return list<EntityField>
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private function fields(ReflectionClass $refClass): array
|
|
|
|
private function fields(ReflectionClass $refClass): array
|
|
|
|
{
|
|
|
|
{
|
|
|
|
@ -79,8 +85,8 @@ readonly class EntityAnalyzer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @param list<EntityField<E>> $fields
|
|
|
|
* @param list<EntityField> $fields
|
|
|
|
* @return EntityField<E>
|
|
|
|
* @return EntityField
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private function idField(array $fields): EntityField
|
|
|
|
private function idField(array $fields): EntityField
|
|
|
|
{
|
|
|
|
{
|
|
|
|
@ -97,7 +103,7 @@ readonly class EntityAnalyzer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @return EntityField<E>
|
|
|
|
* @return EntityField
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private function fieldFromParameter(ReflectionParameter $param): EntityField
|
|
|
|
private function fieldFromParameter(ReflectionParameter $param): EntityField
|
|
|
|
{
|
|
|
|
{
|
|
|
|
@ -117,18 +123,47 @@ readonly class EntityAnalyzer
|
|
|
|
|
|
|
|
|
|
|
|
return new EntityField(
|
|
|
|
return new EntityField(
|
|
|
|
name: $param->name,
|
|
|
|
name: $param->name,
|
|
|
|
fqcn: $this->entity,
|
|
|
|
type: $this->parameterType($param),
|
|
|
|
columnName: $name,
|
|
|
|
columnName: $name,
|
|
|
|
isIdField: count($param->getAttributes(Id::class)) > 0,
|
|
|
|
isIdField: count($param->getAttributes(Id::class)) > 0,
|
|
|
|
readAccess: $readAccess
|
|
|
|
readAccess: $readAccess
|
|
|
|
);
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private function parameterType(ReflectionParameter $param): PrimitiveType|ClassType
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (!$param->hasType()) {
|
|
|
|
|
|
|
|
throw new InvalidEntity(
|
|
|
|
|
|
|
|
"Entity parameter $this->entity::$param->name has no type declaration. Add one."
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$type = $param->getType();
|
|
|
|
|
|
|
|
if (!$type instanceof ReflectionNamedType) {
|
|
|
|
|
|
|
|
throw new InvalidRepository(
|
|
|
|
|
|
|
|
"Entity parameter $this->entity::$param->name has an invalid return type. "
|
|
|
|
|
|
|
|
. 'Simple type expected. Unions, Intersections or no type are not simple.'
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// workaround for phpstan
|
|
|
|
|
|
|
|
/** @var class-string<object> $classType */
|
|
|
|
|
|
|
|
$classType = $type->getName();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return match ($type->getName()) {
|
|
|
|
|
|
|
|
"string" => PrimitiveType::STRING,
|
|
|
|
|
|
|
|
"bool" => PrimitiveType::BOOLEAN,
|
|
|
|
|
|
|
|
"int" => PrimitiveType::INTEGER,
|
|
|
|
|
|
|
|
"float" => PrimitiveType::FLOAT,
|
|
|
|
|
|
|
|
default => new ClassType($classType)
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @param ReflectionClass<E> $refClass
|
|
|
|
* @param ReflectionClass<E> $refClass
|
|
|
|
* @return FieldAccess
|
|
|
|
* @return FieldAccess
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function readAccess(ReflectionClass $refClass, ReflectionParameter $param): FieldAccess
|
|
|
|
private function readAccess(ReflectionClass $refClass, ReflectionParameter $param): FieldAccess
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$getter = 'get' . ucfirst($param->getName());
|
|
|
|
$getter = 'get' . ucfirst($param->getName());
|
|
|
|
|
|
|
|
|
|
|
|
|