|
22 | 22 | final class ClassData
|
23 | 23 | {
|
24 | 24 | private function __construct(
|
25 |
| - public readonly string $className, |
26 |
| - public readonly string $namespace, |
27 |
| - public readonly string $fullClassName, |
| 25 | + private string $className, |
| 26 | + private string $namespace, |
28 | 27 | public readonly ?string $extends,
|
29 | 28 | public readonly bool $isEntity,
|
30 | 29 | private UseStatementGenerator $useStatementGenerator,
|
31 | 30 | private bool $isFinal = true,
|
| 31 | + private string $rootNamespace = 'App', |
32 | 32 | ) {
|
33 | 33 | }
|
34 | 34 |
|
35 |
| - public static function create(string $class, ?string $extendsClass = null, bool $isEntity = false, array $useStatements = []): self |
| 35 | + public static function create(string $class, ?string $suffix = null, ?string $extendsClass = null, bool $isEntity = false, array $useStatements = []): self |
36 | 36 | {
|
| 37 | + $className = Str::getShortClassName($class); |
| 38 | + |
| 39 | + if (null !== $suffix && !str_ends_with($className, $suffix)) { |
| 40 | + $className = Str::asClassName(sprintf('%s%s', $className, $suffix)); |
| 41 | + } |
| 42 | + |
37 | 43 | $useStatements = new UseStatementGenerator($useStatements);
|
38 | 44 |
|
39 | 45 | if ($extendsClass) {
|
40 | 46 | $useStatements->addUseStatement($extendsClass);
|
41 | 47 | }
|
42 | 48 |
|
43 | 49 | return new self(
|
44 |
| - className: Str::getShortClassName($class), |
| 50 | + className: Str::asClassName($className), |
45 | 51 | namespace: Str::getNamespace($class),
|
46 |
| - fullClassName: $class, |
47 | 52 | extends: null === $extendsClass ? null : Str::getShortClassName($extendsClass),
|
48 | 53 | isEntity: $isEntity,
|
49 | 54 | useStatementGenerator: $useStatements,
|
50 | 55 | );
|
51 | 56 | }
|
52 | 57 |
|
| 58 | + public function getClassName(): string |
| 59 | + { |
| 60 | + return $this->className; |
| 61 | + } |
| 62 | + |
| 63 | + public function getNamespace(): string |
| 64 | + { |
| 65 | + if (empty($this->namespace)) { |
| 66 | + return $this->rootNamespace; |
| 67 | + } |
| 68 | + |
| 69 | + return sprintf('%s\%s', $this->rootNamespace, $this->namespace); |
| 70 | + } |
| 71 | + |
| 72 | + public function getFullClassName(): string |
| 73 | + { |
| 74 | + return sprintf('%s\%s', $this->getNamespace(), $this->className); |
| 75 | + } |
| 76 | + |
| 77 | + public function setRootNamespace(string $rootNamespace): self |
| 78 | + { |
| 79 | + $this->rootNamespace = $rootNamespace; |
| 80 | + |
| 81 | + return $this; |
| 82 | + } |
| 83 | + |
53 | 84 | public function getClassDeclaration(): string
|
54 | 85 | {
|
55 | 86 | $extendsDeclaration = '';
|
|
0 commit comments