1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Spiral Framework. Cycle ProxyFactory |
5
|
|
|
* |
6
|
|
|
* @license MIT |
7
|
|
|
* @author Valentin V (Vvval) |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Cycle\ORM\Promise\Declaration\Declaration; |
13
|
|
|
|
14
|
|
|
use Cycle\ORM\Promise\Declaration\DeclarationInterface; |
15
|
|
|
|
16
|
|
|
final class ChildClassDeclaration implements DeclarationInterface |
17
|
|
|
{ |
18
|
|
|
/** @var string */ |
19
|
|
|
private $shortName; |
20
|
|
|
|
21
|
|
|
/** @var string|null */ |
22
|
|
|
private $namespace; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param string $name |
26
|
|
|
* @param DeclarationInterface $parent |
27
|
|
|
*/ |
28
|
|
|
public function __construct(string $name, DeclarationInterface $parent) |
29
|
|
|
{ |
30
|
|
|
$this->shortName = $this->makeShortName($name); |
31
|
|
|
$this->namespace = $this->makeNamespaceName($name, $parent); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
|
|
public function getShortName(): string |
38
|
|
|
{ |
39
|
|
|
return $this->shortName; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return string|null |
44
|
|
|
*/ |
45
|
|
|
public function getNamespaceName(): ?string |
46
|
|
|
{ |
47
|
|
|
return $this->namespace; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
|
|
public function getFullName(): string |
54
|
|
|
{ |
55
|
|
|
if (empty($this->namespace)) { |
56
|
|
|
return "\\{$this->shortName}"; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return "{$this->namespace}\\{$this->shortName}"; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $class |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
|
|
private function makeShortName(string $class): string |
67
|
|
|
{ |
68
|
|
|
$class = rtrim($class, '\\'); |
69
|
|
|
$lastPosition = mb_strripos($class, '\\'); |
70
|
|
|
if ($lastPosition === false) { |
71
|
|
|
return $class; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return mb_substr($class, $lastPosition + 1); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $class |
79
|
|
|
* @param DeclarationInterface $parent |
80
|
|
|
* @return string|null |
81
|
|
|
*/ |
82
|
|
|
private function makeNamespaceName(string $class, DeclarationInterface $parent): ?string |
83
|
|
|
{ |
84
|
|
|
$class = rtrim($class, '\\'); |
85
|
|
|
$lastPosition = mb_strripos($class, '\\'); |
86
|
|
|
if ($lastPosition === 0) { |
87
|
|
|
return null; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if ($lastPosition !== false) { |
91
|
|
|
return ltrim(mb_substr($class, 0, $lastPosition), '\\'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $parent->getNamespaceName(); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|