$class */ public function forClass(string $class): ClassMetadata { if (!class_exists($class) || !is_subclass_of($class, Model::class)) { throw MetadataException::notAModel($class); } $reflection = new ReflectionClass($class); $tableName = $reflection->getAttributes(TableName::class)[0] ?? null; if ($tableName === null) { throw MetadataException::missingTableName($class); } /** @var TableName $table */ $table = $tableName->newInstance(); if (trim($table->name) === '') { throw MetadataException::missingTableName($class); } $properties = []; $primaryKeys = []; $columnOwners = []; foreach ($reflection->getProperties(ReflectionProperty::IS_PUBLIC) as $property) { if ($property->isStatic()) { continue; } $declaring = $property->getDeclaringClass()->getName(); if ($declaring !== $class && $declaring === Model::class) { continue; } $metadata = $this->buildProperty($class, $reflection, $property); if (isset($columnOwners[$metadata->column])) { throw MetadataException::duplicateColumn( $class, $metadata->column, $columnOwners[$metadata->column], $metadata->name, ); } $columnOwners[$metadata->column] = $metadata->name; $properties[$metadata->name] = $metadata; if ($metadata->primaryKey) { $primaryKeys[] = $metadata->name; } } if (count($primaryKeys) !== 1) { throw MetadataException::primaryKeyCount($class, count($primaryKeys)); } $primaryKey = $primaryKeys[0]; $primaryKeyProperty = $properties[$primaryKey] ?? null; if ($primaryKeyProperty === null) { throw MetadataException::unknownProperty($class, $primaryKey); } return new ClassMetadata( $class, $table->name, $primaryKey, $primaryKeyProperty->column, $properties, ); } /** * @param class-string<\Camelia\Orm\Model> $class * @param ReflectionClass<\Camelia\Orm\Model> $reflection */ private function buildProperty( string $class, ReflectionClass $reflection, ReflectionProperty $property, ): PropertyMetadata { $name = $property->getName(); $type = $property->getType(); if (!$type instanceof ReflectionNamedType) { throw MetadataException::missingType($class, $name); } $relation = $this->buildRelation($class, $reflection, $property); $columnAttribute = $property->getAttributes(Column::class)[0] ?? null; $column = $columnAttribute !== null ? $columnAttribute->newInstance()->name : NameConverter::camelToSnake($name); if (preg_match('/[^\x20-\x7E]/', $column) === 1) { throw MetadataException::nonAsciiColumn($class, $name, $column); } $enumAttribute = $property->getAttributes(Enum::class)[0] ?? null; $enumClass = $enumAttribute !== null ? $enumAttribute->newInstance()->class : null; if ($relation !== null) { $this->assertRelationShape($class, $property, $relation); } $maxLength = $property->getAttributes(MaxLength::class)[0] ?? null; return new PropertyMetadata( name: $name, column: $column, type: $enumClass ?? $type->getName(), enumClass: $enumClass, relation: $relation, declaringClass: $class, constraint: new Constraint( nullable: $type->allowsNull() || $property->getAttributes(Nullable::class) !== [], ignored: $property->getAttributes(Ignore::class) !== [] || $relation !== null, guarded: $property->getAttributes(Guarded::class) !== [], primaryKey: $property->getAttributes(PrimaryKey::class) !== [], autoIncrement: $property->getAttributes(AutoIncrement::class) !== [], required: $property->getAttributes(Required::class) !== [], indexed: $property->getAttributes(Index::class) !== [], maxLength: $maxLength !== null ? $maxLength->newInstance()->value : null, ), ); } /** * @param class-string<\Camelia\Orm\Model> $class * @param ReflectionClass<\Camelia\Orm\Model> $reflection */ private function buildRelation( string $class, ReflectionClass $reflection, ReflectionProperty $property, ): ?RelationMetadata { $name = $property->getName(); $belongsTo = $property->getAttributes(BelongsTo::class)[0] ?? null; if ($belongsTo !== null) { /** @var BelongsTo $attribute */ $attribute = $belongsTo->newInstance(); return new RelationMetadata( RelationMetadata::KIND_BELONGS_TO, $this->resolveRelatedClass($class, $reflection, $attribute->related, $name), $attribute->foreignKey, ); } $hasOne = $property->getAttributes(HasOne::class)[0] ?? null; if ($hasOne !== null) { /** @var HasOne $attribute */ $attribute = $hasOne->newInstance(); return new RelationMetadata( RelationMetadata::KIND_HAS_ONE, $this->resolveRelatedClass($class, $reflection, $attribute->related, $name), $attribute->foreignKey, ); } $hasMany = $property->getAttributes(HasMany::class)[0] ?? null; if ($hasMany !== null) { /** @var HasMany $attribute */ $attribute = $hasMany->newInstance(); return new RelationMetadata( RelationMetadata::KIND_HAS_MANY, $this->resolveRelatedClass($class, $reflection, $attribute->related, $name), $attribute->foreignKey, ); } $belongsToMany = $property->getAttributes(BelongsToMany::class)[0] ?? null; if ($belongsToMany !== null) { /** @var BelongsToMany $attribute */ $attribute = $belongsToMany->newInstance(); return new RelationMetadata( RelationMetadata::KIND_BELONGS_TO_MANY, $this->resolveRelatedClass($class, $reflection, $attribute->related, $name), '', $attribute->through, $attribute->foreignPivotKey, $attribute->relatedPivotKey, ); } return null; } /** * @param class-string<\Camelia\Orm\Model> $class * @param ReflectionClass<\Camelia\Orm\Model> $reflection * @return class-string<\Camelia\Orm\Model> */ private function resolveRelatedClass( string $class, ReflectionClass $reflection, string $related, string $property, ): string { if (class_exists($related) && is_subclass_of($related, Model::class)) { return $related; } $namespace = $reflection->getNamespaceName(); $candidate = $namespace === '' ? $related : $namespace . '\\' . $related; if (class_exists($candidate) && is_subclass_of($candidate, Model::class)) { return $candidate; } throw MetadataException::unknownRelatedClass($class, $property, $related); } private function assertRelationShape( string $class, ReflectionProperty $property, RelationMetadata $relation, ): void { $name = $property->getName(); $type = $property->getType(); if (!$type instanceof ReflectionNamedType) { throw MetadataException::invalidRelationProperty($class, $name); } $isArray = $type->getName() === 'array'; if ($relation->isToMany() && !$isArray) { throw MetadataException::invalidRelationProperty($class, $name); } if (!$relation->isToMany() && (!$type->allowsNull() || $isArray)) { throw MetadataException::invalidRelationProperty($class, $name); } } }