|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Nette\PHPStan\Database; |
| 4 | + |
| 5 | +use PHPStan\Reflection\ReflectionProvider; |
| 6 | +use PHPStan\Type\ObjectType; |
| 7 | + |
| 8 | + |
| 9 | +/** |
| 10 | + * Resolves database table names to entity row class types. |
| 11 | + * Convention: table_name -> PascalCase -> prepend prefix + append suffix from mask. |
| 12 | + * Explicit overrides in $tables take precedence over convention. |
| 13 | + */ |
| 14 | +class TableRowTypeResolver |
| 15 | +{ |
| 16 | + private string $prefix; |
| 17 | + private string $suffix; |
| 18 | + |
| 19 | + |
| 20 | + /** |
| 21 | + * @param string $convention mask like App\Entity\*Row, where * is replaced by PascalCase table name |
| 22 | + * @param array<string, string> $tables explicit table -> FQCN overrides |
| 23 | + */ |
| 24 | + public function __construct( |
| 25 | + private ReflectionProvider $reflectionProvider, |
| 26 | + string $convention = '', |
| 27 | + private array $tables = [], |
| 28 | + ) { |
| 29 | + if ($convention !== '' && str_contains($convention, '*')) { |
| 30 | + [$this->prefix, $this->suffix] = explode('*', $convention, 2); |
| 31 | + } else { |
| 32 | + $this->prefix = ''; |
| 33 | + $this->suffix = ''; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + |
| 38 | + /** |
| 39 | + * Resolves a table name to an ObjectType for the entity row class. |
| 40 | + * Returns null if no mapping applies (class does not exist). |
| 41 | + */ |
| 42 | + public function resolve(string $tableName): ?ObjectType |
| 43 | + { |
| 44 | + // 1. Explicit tables take precedence |
| 45 | + if (isset($this->tables[$tableName])) { |
| 46 | + $className = $this->tables[$tableName]; |
| 47 | + return $this->reflectionProvider->hasClass($className) |
| 48 | + ? new ObjectType($className) |
| 49 | + : null; |
| 50 | + } |
| 51 | + |
| 52 | + // 2. Convention disabled when no mask configured |
| 53 | + if ($this->prefix === '' && $this->suffix === '') { |
| 54 | + return null; |
| 55 | + } |
| 56 | + |
| 57 | + // 3. Convention: prefix + PascalCase(table) + suffix |
| 58 | + $className = $this->prefix . $this->snakeToPascalCase($tableName) . $this->suffix; |
| 59 | + return $this->reflectionProvider->hasClass($className) |
| 60 | + ? new ObjectType($className) |
| 61 | + : null; |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | + /** |
| 66 | + * Extracts the table name from a key parameter. |
| 67 | + * For related()/ref(), key can be 'table' or 'table.column'. |
| 68 | + */ |
| 69 | + public function extractTableName(string $key): string |
| 70 | + { |
| 71 | + $pos = strpos($key, '.'); |
| 72 | + return $pos !== false ? substr($key, 0, $pos) : $key; |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | + private function snakeToPascalCase(string $table): string |
| 77 | + { |
| 78 | + return str_replace(' ', '', ucwords(strtr($table, '_', ' '))); |
| 79 | + } |
| 80 | +} |
0 commit comments