|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace HelloWordPl\SimpleEntityGeneratorBundle\Lib; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use JMS\Serializer\TypeParser; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Tools |
|
10
|
|
|
* |
|
11
|
|
|
* @author Sławomir Kania <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
class Tools |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @param string $namespace |
|
18
|
|
|
* @param string $mandatoryFirstBackslash |
|
19
|
|
|
* @return boolean |
|
20
|
|
|
*/ |
|
21
|
|
|
public static function isNamespaceValid($namespace, $mandatoryFirstBackslash = true) |
|
22
|
|
|
{ |
|
23
|
|
|
if ($mandatoryFirstBackslash && false == self::isFirstCharBackslash($namespace)) { |
|
|
|
|
|
|
24
|
|
|
return false; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
if (1 !== preg_match("/[\\\\]{1,1}/", $namespace)) { |
|
28
|
|
|
return false; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
if (1 === preg_match("/[\\\\]{2,}/", $namespace)) { |
|
32
|
|
|
return false; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
if (1 === preg_match('/[^a-zA-Z0-9\\\\\\_]+/', $namespace)) { |
|
36
|
|
|
return false; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$namespaceElementsArray = explode("\\", $namespace); |
|
40
|
|
|
if (count($namespaceElementsArray) < 3) { |
|
41
|
|
|
return false; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
foreach ($namespaceElementsArray as $element) { |
|
45
|
|
|
if (ctype_digit(substr($element, 0, 1))) { |
|
46
|
|
|
return false; |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return true; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param string $name |
|
55
|
|
|
* @return boolean |
|
56
|
|
|
*/ |
|
57
|
|
|
public static function isValidPropertyNameString($name) |
|
58
|
|
|
{ |
|
59
|
|
|
if (false == is_string($name)) { |
|
|
|
|
|
|
60
|
|
|
return false; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if (empty($name)) { |
|
64
|
|
|
return false; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if ("_" == $name) { |
|
68
|
|
|
return false; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
if (1 === preg_match('/[^a-zA-Z0-9\_]+/', $name)) { |
|
72
|
|
|
return false; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
if (ctype_digit(substr($name, 0, 1))) { |
|
76
|
|
|
return false; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return true; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param string $type |
|
84
|
|
|
* @return boolean |
|
85
|
|
|
*/ |
|
86
|
|
|
public static function isValidPropertyTypeString($type) |
|
87
|
|
|
{ |
|
88
|
|
|
if (false == is_string($type)) { |
|
|
|
|
|
|
89
|
|
|
return false; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
try { |
|
93
|
|
|
$jmsTypeParser = new TypeParser(); |
|
94
|
|
|
$jmsTypeParser->parse($type); |
|
95
|
|
|
} catch (Exception $ex) { |
|
96
|
|
|
return false; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return true; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param string $namespace |
|
104
|
|
|
* @return string |
|
105
|
|
|
* @throws Exception |
|
106
|
|
|
*/ |
|
107
|
|
|
public static function getNamespaceWithoutName($namespace) |
|
108
|
|
|
{ |
|
109
|
|
|
self::checkNamespaceValid($namespace); |
|
110
|
|
|
$parts = explode('\\', $namespace); |
|
111
|
|
|
unset($parts[count($parts) - 1]); |
|
112
|
|
|
return implode("\\", $parts); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param string $namespace |
|
117
|
|
|
* @return string |
|
118
|
|
|
* @throws Exception |
|
119
|
|
|
*/ |
|
120
|
|
|
public static function getDirectoryFromNamespace($namespace) |
|
121
|
|
|
{ |
|
122
|
|
|
self::checkNamespaceValid($namespace); |
|
123
|
|
|
return str_replace("\\", "/", self::getNamespaceWithoutName($namespace)); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param string $namespace |
|
128
|
|
|
* @return string |
|
129
|
|
|
* @throws Exception |
|
130
|
|
|
*/ |
|
131
|
|
|
public static function getNameFromNamespace($namespace) |
|
132
|
|
|
{ |
|
133
|
|
|
self::checkNamespaceValid($namespace); |
|
134
|
|
|
$parts = explode('\\', $namespace); |
|
135
|
|
|
return $parts[count($parts) - 1]; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @param string $namespace |
|
140
|
|
|
* @return string |
|
141
|
|
|
*/ |
|
142
|
|
|
public static function removeBackslashPrefixFromNamespace($namespace) |
|
143
|
|
|
{ |
|
144
|
|
|
self::checkNamespaceValid($namespace); |
|
145
|
|
|
if ("\\" == substr($namespace, 0, 1)) { |
|
146
|
|
|
return substr($namespace, 1); |
|
147
|
|
|
} |
|
148
|
|
|
return $namespace; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Throw Exception when namespace invalid |
|
153
|
|
|
* |
|
154
|
|
|
* @param string $namespace |
|
155
|
|
|
* @throws Exception |
|
156
|
|
|
*/ |
|
157
|
|
|
public static function checkNamespaceValid($namespace) |
|
158
|
|
|
{ |
|
159
|
|
|
if (false == self::isNamespaceValid($namespace)) { |
|
|
|
|
|
|
160
|
|
|
throw new Exception(sprintf("Invalid namespace: %s", $namespace)); |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @param string $string |
|
166
|
|
|
* @return array |
|
167
|
|
|
* @throws Exception |
|
168
|
|
|
*/ |
|
169
|
|
|
public static function explodeTemplateStringToArray($string) |
|
170
|
|
|
{ |
|
171
|
|
|
if (false == is_string($string)) { |
|
|
|
|
|
|
172
|
|
|
throw new Exception("Param has to be string!"); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
return preg_split("/\n/", $string); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* @param array $array |
|
180
|
|
|
* @return string |
|
181
|
|
|
* @throws Exception |
|
182
|
|
|
*/ |
|
183
|
|
|
public static function implodeArrayToTemplate($array) |
|
184
|
|
|
{ |
|
185
|
|
|
if (false == is_array($array)) { |
|
|
|
|
|
|
186
|
|
|
throw new Exception("Invalid array!"); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
return implode("\n", $array); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Check existance of constraint |
|
194
|
|
|
* proper annotation eg. @\Symfony\Component\Validator\Constraints\Valid() |
|
195
|
|
|
* |
|
196
|
|
|
* @param string $constraintAnnotation |
|
197
|
|
|
* @return boolean |
|
198
|
|
|
*/ |
|
199
|
|
|
public static function isCallableConstraintAnnotation($constraintAnnotation) |
|
200
|
|
|
{ |
|
201
|
|
|
$output = []; |
|
202
|
|
|
preg_match("/\@(.*?)\(.*?\)/", $constraintAnnotation, $output); |
|
203
|
|
|
|
|
204
|
|
|
if ($output < 2) { |
|
205
|
|
|
return false; |
|
206
|
|
|
} |
|
207
|
|
|
if (false == class_exists(end($output))) { |
|
|
|
|
|
|
208
|
|
|
return false; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
return true; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* If first character of string is backslash then return true |
|
216
|
|
|
* |
|
217
|
|
|
* @param string $string |
|
218
|
|
|
* @return boolean |
|
219
|
|
|
* @throws Exception |
|
220
|
|
|
*/ |
|
221
|
|
|
public static function isFirstCharBackslash($string) |
|
222
|
|
|
{ |
|
223
|
|
|
if (false == is_string($string)) { |
|
|
|
|
|
|
224
|
|
|
throw new Exception("Invalid string!"); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
return "\\" == substr((string) $string, 0, 1); |
|
228
|
|
|
} |
|
229
|
|
|
} |
|
230
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.