|
|
|
@ -5,19 +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\Type\ArrayType;
|
|
|
|
use Kartierung\Analyze\Result\PrimitiveType;
|
|
|
|
use Kartierung\Analyze\Result\Type\ClassType;
|
|
|
|
|
|
|
|
use Kartierung\Analyze\Result\Type\PrimitiveType;
|
|
|
|
|
|
|
|
use Kartierung\Attribute\Column;
|
|
|
|
use Kartierung\Attribute\Column;
|
|
|
|
use Kartierung\Attribute\Id;
|
|
|
|
use Kartierung\Attribute\Id;
|
|
|
|
use Kartierung\Attribute\ListOf;
|
|
|
|
|
|
|
|
use Kartierung\Attribute\Table;
|
|
|
|
use Kartierung\Attribute\Table;
|
|
|
|
use ReflectionClass;
|
|
|
|
use ReflectionClass;
|
|
|
|
|
|
|
|
use ReflectionIntersectionType;
|
|
|
|
use ReflectionNamedType;
|
|
|
|
use ReflectionNamedType;
|
|
|
|
use ReflectionParameter;
|
|
|
|
use ReflectionParameter;
|
|
|
|
|
|
|
|
use ReflectionUnionType;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @template E of object
|
|
|
|
* @template E of object
|
|
|
|
@ -33,12 +34,9 @@ readonly class EntityAnalyzer
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @return EntityResult<E>
|
|
|
|
* @return EntityResult<E>
|
|
|
|
* @noinspection PhpDocMissingThrowsInspection
|
|
|
|
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function analyze(): EntityResult
|
|
|
|
public function analyze(): EntityResult
|
|
|
|
{
|
|
|
|
{
|
|
|
|
(new RecursionCheck($this->entity))->check();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$refClass = new ReflectionClass($this->entity);
|
|
|
|
$refClass = new ReflectionClass($this->entity);
|
|
|
|
|
|
|
|
|
|
|
|
$fields = $this->fields($refClass);
|
|
|
|
$fields = $this->fields($refClass);
|
|
|
|
@ -104,6 +102,9 @@ readonly class EntityAnalyzer
|
|
|
|
return $idFields[array_key_first($idFields)];
|
|
|
|
return $idFields[array_key_first($idFields)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @return EntityField
|
|
|
|
|
|
|
|
*/
|
|
|
|
private function fieldFromParameter(ReflectionParameter $param): EntityField
|
|
|
|
private function fieldFromParameter(ReflectionParameter $param): EntityField
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$attrs = $param->getAttributes(Column::class);
|
|
|
|
$attrs = $param->getAttributes(Column::class);
|
|
|
|
@ -129,7 +130,7 @@ readonly class EntityAnalyzer
|
|
|
|
);
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private function parameterType(ReflectionParameter $param): PrimitiveType|ClassType|ArrayType
|
|
|
|
private function parameterType(ReflectionParameter $param): PrimitiveType|ClassType
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (!$param->hasType()) {
|
|
|
|
if (!$param->hasType()) {
|
|
|
|
throw new InvalidEntity(
|
|
|
|
throw new InvalidEntity(
|
|
|
|
@ -139,9 +140,9 @@ readonly class EntityAnalyzer
|
|
|
|
|
|
|
|
|
|
|
|
$type = $param->getType();
|
|
|
|
$type = $param->getType();
|
|
|
|
if (!$type instanceof ReflectionNamedType) {
|
|
|
|
if (!$type instanceof ReflectionNamedType) {
|
|
|
|
throw new InvalidEntity(
|
|
|
|
throw new InvalidRepository(
|
|
|
|
"Entity parameter $this->entity::$param->name has an invalid return type. "
|
|
|
|
"Entity parameter $this->entity::$param->name has an invalid return type. "
|
|
|
|
. "Simple type expected. Unions, Intersections or no type are not simple."
|
|
|
|
. 'Simple type expected. Unions, Intersections or no type are not simple.'
|
|
|
|
);
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@ -149,23 +150,6 @@ readonly class EntityAnalyzer
|
|
|
|
/** @var class-string<object> $classType */
|
|
|
|
/** @var class-string<object> $classType */
|
|
|
|
$classType = $type->getName();
|
|
|
|
$classType = $type->getName();
|
|
|
|
|
|
|
|
|
|
|
|
if ($classType === "array") {
|
|
|
|
|
|
|
|
$listAttr = $param->getAttributes(ListOf::class);
|
|
|
|
|
|
|
|
if (empty($listAttr) || count($listAttr) > 1) {
|
|
|
|
|
|
|
|
throw new InvalidEntity(
|
|
|
|
|
|
|
|
"Entity parameter $this->entity::$param->name declares its typr as array. "
|
|
|
|
|
|
|
|
. "Exactly one #[ListOf(...)] attribute expected."
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @var ListOf $listAttrInstance */
|
|
|
|
|
|
|
|
$listAttrInstance = $listAttr[0]->newInstance();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return new ArrayType(
|
|
|
|
|
|
|
|
(new EntityAnalyzer($listAttrInstance->entitiyFqcn))->analyze()
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return match ($type->getName()) {
|
|
|
|
return match ($type->getName()) {
|
|
|
|
"string" => PrimitiveType::STRING,
|
|
|
|
"string" => PrimitiveType::STRING,
|
|
|
|
"bool" => PrimitiveType::BOOLEAN,
|
|
|
|
"bool" => PrimitiveType::BOOLEAN,
|
|
|
|
@ -177,7 +161,7 @@ readonly class EntityAnalyzer
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @param ReflectionClass<E> $refClass
|
|
|
|
* @param ReflectionClass<E> $refClass
|
|
|
|
* @noinspection PhpDocMissingThrowsInspection
|
|
|
|
* @return FieldAccess
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private function readAccess(ReflectionClass $refClass, ReflectionParameter $param): FieldAccess
|
|
|
|
private function readAccess(ReflectionClass $refClass, ReflectionParameter $param): FieldAccess
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|