1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace SlayerBirden\DFCodeGeneration\Util; |
5
|
|
|
|
6
|
|
|
use Zend\Filter\Word\CamelCaseToUnderscore; |
7
|
|
|
|
8
|
|
|
final class Lexer |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Convert plural into singular form |
12
|
|
|
* # buses => bus |
13
|
|
|
* # products => product |
14
|
|
|
* # stores => store |
15
|
|
|
* # categories => category |
16
|
|
|
* |
17
|
|
|
* @param string $name |
18
|
|
|
* @return string |
19
|
|
|
*/ |
20
|
|
|
public static function getSingularForm(string $name): string |
21
|
|
|
{ |
22
|
|
|
if (preg_match('/.*ies$/', $name)) { |
23
|
|
|
return preg_replace('/(.*)ies$/', '$1y', $name); |
24
|
|
|
} elseif (preg_match('/.*ses$/', $name)) { |
25
|
|
|
return preg_replace('/(.*)ses$/', '$1s', $name); |
26
|
|
|
} elseif (preg_match('/.*s$/', $name)) { |
27
|
|
|
return preg_replace('/(.*)s$/', '$1', $name); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
return $name; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Convert singular into plural form |
35
|
|
|
* # bus => buses |
36
|
|
|
* # product => products |
37
|
|
|
* # store => stores |
38
|
|
|
* # category => categories |
39
|
|
|
* |
40
|
|
|
* @param string $name |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
|
|
public static function getPluralForm(string $name): string |
44
|
|
|
{ |
45
|
|
|
if (preg_match('/.*s$/', $name)) { |
46
|
|
|
return preg_replace('/(.*)s$/', '$1ses', $name); |
47
|
|
|
} elseif (preg_match('/.*y$/', $name)) { |
48
|
|
|
return preg_replace('/(.*)y$/', '$1ies', $name); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $name . 's'; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $fullyQualifiedName |
56
|
|
|
* @return string |
57
|
|
|
* @throws \ReflectionException |
58
|
|
|
*/ |
59
|
2 |
|
public static function getBaseName(string $fullyQualifiedName): string |
60
|
|
|
{ |
61
|
2 |
|
$reflection = new \ReflectionClass($fullyQualifiedName); |
62
|
2 |
|
return $reflection->getShortName(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $fullyQualifiedName |
67
|
|
|
* @return string |
68
|
|
|
* @throws \ReflectionException |
69
|
|
|
*/ |
70
|
2 |
|
public static function getRefName(string $fullyQualifiedName): string |
71
|
|
|
{ |
72
|
2 |
|
$baseName = self::getBaseName($fullyQualifiedName); |
73
|
2 |
|
return strtolower((new CamelCaseToUnderscore())->filter($baseName)); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|