1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Cycle\ORM\Promise\Declaration\Declaration; |
5
|
|
|
|
6
|
|
|
use Cycle\ORM\Promise\Declaration\DeclarationInterface; |
7
|
|
|
|
8
|
|
|
final class ChildClassDeclaration implements DeclarationInterface |
9
|
|
|
{ |
10
|
|
|
/** @var string */ |
11
|
|
|
private $shortName; |
12
|
|
|
|
13
|
|
|
/** @var string|null */ |
14
|
|
|
private $namespace; |
15
|
|
|
|
16
|
|
|
public function __construct(string $name, DeclarationInterface $parent) |
17
|
|
|
{ |
18
|
|
|
$this->shortName = $this->makeShortName($name); |
19
|
|
|
$this->namespace = $this->makeNamespaceName($name, $parent); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function getShortName(): string |
23
|
|
|
{ |
24
|
|
|
return $this->shortName; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function getNamespaceName(): ?string |
28
|
|
|
{ |
29
|
|
|
return $this->namespace; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function getFullName(): string |
33
|
|
|
{ |
34
|
|
|
if (empty($this->namespace)) { |
35
|
|
|
return "\\{$this->shortName}"; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return "{$this->namespace}\\{$this->shortName}"; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
private function makeShortName(string $class): string |
42
|
|
|
{ |
43
|
|
|
$class = rtrim($class, '\\'); |
44
|
|
|
$lastPosition = mb_strripos($class, '\\'); |
45
|
|
|
if ($lastPosition === false) { |
46
|
|
|
return $class; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return mb_substr($class, $lastPosition + 1); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
private function makeNamespaceName(string $class, DeclarationInterface $parent): ?string |
53
|
|
|
{ |
54
|
|
|
$class = rtrim($class, '\\'); |
55
|
|
|
$lastPosition = mb_strripos($class, '\\'); |
56
|
|
|
if ($lastPosition === 0) { |
57
|
|
|
return null; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if ($lastPosition !== false) { |
61
|
|
|
return ltrim(mb_substr($class, 0, $lastPosition), '\\'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $parent->getNamespaceName(); |
65
|
|
|
} |
66
|
|
|
} |