|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace DDDGen\VO; |
|
5
|
|
|
|
|
6
|
|
|
use Assert\Assert; |
|
7
|
|
|
|
|
8
|
|
|
final class FQCN |
|
9
|
|
|
{ |
|
10
|
|
|
/** @var string */ |
|
11
|
|
|
private $fqcn; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* FQCN constructor. |
|
15
|
|
|
* |
|
16
|
|
|
* @param string $fqcn |
|
17
|
|
|
*/ |
|
18
|
23 |
|
public function __construct($fqcn) |
|
19
|
|
|
{ |
|
20
|
|
|
// TODO double check this pattern |
|
21
|
|
|
// Ref: https://regex101.com/r/uRwOkT/1 |
|
22
|
23 |
|
Assert::that($fqcn)->regex("#^(?:[a-z_]|[a-z_]\w+|\\\\)+$#i"); |
|
23
|
|
|
|
|
24
|
18 |
|
$this->fqcn = $fqcn; |
|
25
|
18 |
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @return mixed |
|
29
|
|
|
*/ |
|
30
|
16 |
|
public function getFqcn() |
|
31
|
|
|
{ |
|
32
|
16 |
|
return $this->fqcn; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
1 |
|
public function append(string $postfix): self |
|
36
|
|
|
{ |
|
37
|
1 |
|
return self::fromString($this->getFqcn() . "\\" . $postfix); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
1 |
|
public function preppend(string $prefix): self |
|
41
|
|
|
{ |
|
42
|
1 |
|
return self::fromString($prefix . "\\" . $this->getFqcn()); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* toPSR4File |
|
47
|
|
|
* Ref: http://www.php-fig.org/psr/psr-4/ |
|
48
|
|
|
* |
|
49
|
|
|
* |
|
50
|
|
|
* @param $base_fqcn will be excluded from final path |
|
51
|
|
|
* |
|
52
|
|
|
* @return string |
|
53
|
|
|
*/ |
|
54
|
1 |
|
public function toPSR4File($base_fqcn = ""): string |
|
55
|
|
|
{ |
|
56
|
1 |
|
$fqcn = str_replace($base_fqcn, "", $this->getFqcn()); |
|
57
|
1 |
|
$path = str_replace("\\", "/", $fqcn) . ".php"; |
|
58
|
1 |
|
if ($path[0] == "/") { |
|
59
|
1 |
|
$path = substr($path, 1); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
return $path; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* toPSR4Dir |
|
67
|
|
|
* Ref: http://www.php-fig.org/psr/psr-4/ |
|
68
|
|
|
* |
|
69
|
|
|
* |
|
70
|
|
|
* @param $base_fqcn will be excluded from final path |
|
71
|
|
|
* |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
3 |
|
public function toPSR4Path($base_fqcn = ""): string |
|
75
|
|
|
{ |
|
76
|
3 |
|
$fqcn = str_replace($base_fqcn, "", $this->getFqcn()); |
|
77
|
3 |
|
$path = str_replace("\\", "/", $fqcn); |
|
78
|
3 |
|
$path = trim($path, "/"); |
|
79
|
|
|
|
|
80
|
3 |
|
return $path; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* getLastPart - return only last block from namespace |
|
85
|
|
|
* |
|
86
|
|
|
* |
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
4 |
|
public function getLastPart(): string |
|
90
|
|
|
{ |
|
91
|
4 |
|
preg_match("#[^\\\\]+$#", $this->getFqcn(), $p); |
|
92
|
|
|
|
|
93
|
4 |
|
return $p[0]; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* getLastPart - return base namespace without trailing part |
|
98
|
|
|
* |
|
99
|
|
|
* |
|
100
|
|
|
* @return string |
|
101
|
|
|
*/ |
|
102
|
3 |
|
public function getBasePart(): string |
|
103
|
|
|
{ |
|
104
|
3 |
|
$string = str_replace($this->getLastPart(), "", $this->getFqcn()); |
|
105
|
3 |
|
if (isset($string[-1]) && $string[-1] == "\\") { |
|
106
|
3 |
|
$string = substr($string, 0, -1); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
3 |
|
return $string; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* fromString - will replace multiple, ending slashes |
|
114
|
|
|
* |
|
115
|
|
|
* |
|
116
|
|
|
* @param string $string |
|
117
|
|
|
* |
|
118
|
|
|
* @return FQCN |
|
119
|
|
|
*/ |
|
120
|
18 |
|
static function fromString(string $string): self |
|
|
|
|
|
|
121
|
|
|
{ |
|
122
|
18 |
|
$string = str_replace("/", "\\", $string); |
|
123
|
18 |
|
$string = preg_replace("#(\\\\){2,}#", "\\", $string); |
|
124
|
18 |
|
if (substr($string, -1, 1) == "\\") { |
|
125
|
1 |
|
$string = substr($string, 0, -1); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
18 |
|
return new self($string); |
|
129
|
|
|
} |
|
130
|
|
|
} |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.