|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the Abstract builder package, an RunOpenCode project. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) 2017 RunOpenCode |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace RunOpenCode\AbstractBuilder\Utils; |
|
11
|
|
|
|
|
12
|
|
|
use RunOpenCode\AbstractBuilder\AbstractBuilder; |
|
13
|
|
|
use RunOpenCode\AbstractBuilder\Ast\Metadata\ClassMetadata; |
|
14
|
|
|
use RunOpenCode\AbstractBuilder\Exception\InvalidArgumentException; |
|
15
|
|
|
use RunOpenCode\AbstractBuilder\ReflectiveAbstractBuilder; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class ClassUtils |
|
19
|
|
|
* |
|
20
|
|
|
* @package RunOpenCode\AbstractBuilder\Utils |
|
21
|
|
|
*/ |
|
22
|
|
|
final class ClassUtils |
|
23
|
|
|
{ |
|
24
|
|
|
private function __construct() { /* noop */ } |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Check if class name is valid. |
|
28
|
|
|
* |
|
29
|
|
|
* @param string $fqcn |
|
30
|
|
|
* |
|
31
|
|
|
* @return bool |
|
32
|
|
|
*/ |
|
33
|
3 |
|
public static function isClassNameValid($fqcn) |
|
34
|
|
|
{ |
|
35
|
3 |
|
foreach (explode('\\', ltrim($fqcn, '\\')) as $part) { |
|
36
|
|
|
|
|
37
|
3 |
|
if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $part)) { |
|
38
|
1 |
|
return false; |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
3 |
|
return true; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Check if class can have its builder pattern class implemented. |
|
47
|
|
|
* |
|
48
|
|
|
* @param ClassMetadata $class |
|
49
|
|
|
* |
|
50
|
|
|
* @return bool |
|
51
|
|
|
*/ |
|
52
|
1 |
|
public static function isBuildable(ClassMetadata $class) |
|
53
|
|
|
{ |
|
54
|
1 |
|
if (!$class->hasPublicMethod('__construct')) { |
|
55
|
1 |
|
return false; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
1 |
|
if (0 === count($class->getPublicMethod('__construct')->getParameters())) { |
|
59
|
1 |
|
return false; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
return true; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Check if class is implementing required builder class. |
|
67
|
|
|
* |
|
68
|
|
|
* @param ClassMetadata $class |
|
69
|
|
|
* |
|
70
|
|
|
* @return bool |
|
71
|
|
|
*/ |
|
72
|
1 |
|
public static function isBuilder(ClassMetadata $class) |
|
73
|
|
|
{ |
|
74
|
1 |
|
if (null === $class->getParent()) { |
|
75
|
1 |
|
return false; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
1 |
|
$instanceOfAbstractBuilder = function (ClassMetadata $class) use (&$instanceOfAbstractBuilder) { |
|
79
|
|
|
|
|
80
|
1 |
|
if (null === $class->getParent()) { |
|
81
|
1 |
|
return false; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
if ( |
|
85
|
1 |
|
ReflectiveAbstractBuilder::class === $class->getParent()->getName() |
|
86
|
|
|
|| |
|
87
|
1 |
|
AbstractBuilder::class === $class->getParent()->getName() |
|
88
|
|
|
) { |
|
89
|
1 |
|
return true; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
1 |
|
return $instanceOfAbstractBuilder($class->getParent()); |
|
93
|
1 |
|
}; |
|
94
|
|
|
|
|
95
|
1 |
|
return $instanceOfAbstractBuilder($class); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Get namespace of class. |
|
100
|
|
|
* |
|
101
|
|
|
* @param $class |
|
102
|
|
|
* |
|
103
|
|
|
* @return string|null |
|
104
|
|
|
* |
|
105
|
|
|
* @throws \RunOpenCode\AbstractBuilder\Exception\InvalidArgumentException |
|
106
|
|
|
*/ |
|
107
|
1 |
|
public static function getNamespace($class) |
|
108
|
|
|
{ |
|
109
|
1 |
|
if ($class instanceof ClassMetadata) { |
|
110
|
|
|
$class = $class->getName(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
1 |
|
$class = trim($class, '\\'); |
|
114
|
|
|
|
|
115
|
1 |
|
$parts = explode('\\', $class); |
|
116
|
|
|
|
|
117
|
1 |
|
if (0 === count($parts)) { |
|
118
|
|
|
throw new InvalidArgumentException(sprintf('Invalid class name "%s".', $class)); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
1 |
|
if (1 === count($parts)) { |
|
122
|
1 |
|
return null; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
1 |
|
array_pop($parts); |
|
126
|
|
|
|
|
127
|
1 |
|
return implode('\\', $parts); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Get short name of class. |
|
132
|
|
|
* |
|
133
|
|
|
* @param $class |
|
134
|
|
|
* |
|
135
|
|
|
* @return string |
|
136
|
|
|
*/ |
|
137
|
|
|
public static function getShortName($class) |
|
138
|
|
|
{ |
|
139
|
|
|
if ($class instanceof ClassMetadata) { |
|
140
|
|
|
return $class->getShortName(); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
$class = trim($class, '\\'); |
|
144
|
|
|
|
|
145
|
|
|
$parts = explode('\\', $class); |
|
146
|
|
|
|
|
147
|
|
|
return array_pop($parts); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|