|
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
|
4 |
|
public static function getSingularForm(string $name): string |
|
21
|
|
|
{ |
|
22
|
4 |
|
if (preg_match('/.*ies$/', $name)) { |
|
23
|
1 |
|
return preg_replace('/(.*)ies$/', '$1y', $name); |
|
24
|
3 |
|
} elseif (preg_match('/.*ses$/', $name)) { |
|
25
|
1 |
|
return preg_replace('/(.*)ses$/', '$1s', $name); |
|
26
|
2 |
|
} elseif (preg_match('/.*s$/', $name)) { |
|
27
|
2 |
|
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
|
4 |
|
public static function getPluralForm(string $name): string |
|
44
|
|
|
{ |
|
45
|
4 |
|
if (preg_match('/.*s$/', $name)) { |
|
46
|
1 |
|
return preg_replace('/(.*)s$/', '$1ses', $name); |
|
47
|
3 |
|
} elseif (preg_match('/.*y$/', $name)) { |
|
48
|
1 |
|
return preg_replace('/(.*)y$/', '$1ies', $name); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
2 |
|
return $name . 's'; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param string $fullyQualifiedName |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
16 |
|
public static function getBaseName(string $fullyQualifiedName): string |
|
59
|
|
|
{ |
|
60
|
16 |
|
if (($pos = strrpos($fullyQualifiedName, '\\')) !== false) { |
|
61
|
14 |
|
return ltrim(substr($fullyQualifiedName, $pos), '\\'); |
|
62
|
|
|
} |
|
63
|
2 |
|
return $fullyQualifiedName; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param string $fullyQualifiedName |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
11 |
|
public static function getRefName(string $fullyQualifiedName): string |
|
71
|
|
|
{ |
|
72
|
11 |
|
$baseName = self::getBaseName($fullyQualifiedName); |
|
73
|
11 |
|
return strtolower((new CamelCaseToUnderscore())->filter($baseName)); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|