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