2
2
3
3
namespace Vcian \LaravelDBAuditor \Traits ;
4
4
5
+ use Illuminate \Support \Facades \DB ;
5
6
use Illuminate \Support \Str ;
6
7
use Vcian \LaravelDBAuditor \Constants \Constant ;
7
8
8
9
trait NamingRules
9
10
{
10
- /**
11
- * Check name only in lowercase.
12
- * @param string $name
13
- * @return string|bool
14
- */
15
- public function nameOnlyLowerCase (string $ name ): string |bool
16
- {
17
- $ inputName = $ this ->removeSpecialCharacter ($ name );
18
- if (strtolower ($ inputName ) !== $ inputName ) {
19
- return strtolower ($ this ->addSpecialCharacter ($ name ));
20
- }
21
- return Constant::STATUS_TRUE ;
22
- }
11
+ public string $ conventionName ;
23
12
24
- /**
25
- * Remove underscore from name.
26
- * @param string $name
27
- * @return string
28
- */
29
- public function removeSpecialCharacter (string $ name ): string
30
- {
31
- return str_replace ("_ " , '' , $ name );
32
- }
33
13
34
- /**
35
- * Add special character
36
- * @param string $name
37
- * @return string
38
- */
39
- public function addSpecialCharacter (string $ name ): string
14
+ public function setConvenationName (string $ name )
40
15
{
41
- return str_replace ( " " , ' _ ' , $ name) ;
16
+ $ this -> conventionName = $ name ;
42
17
}
43
18
44
19
/**
45
- * Check name has no space.
20
+ * Check name only in lowercase. or camelCase or snake_case
46
21
* @param string $name
47
22
* @return string|bool
48
23
*/
49
- public function nameHasNoSpace ( string $ name ): string |bool
24
+ public function nameConvention ( ): string |bool
50
25
{
51
- if (str_contains ($ name , ' ' )) {
52
- return strtolower ( $ this ->addSpecialCharacter ( $ name ) );
26
+ if (str_contains ($ this -> conventionName , " " )) {
27
+ return $ this ->convertToSnakeCase ( $ this -> conventionName );
53
28
}
54
- return Constant::STATUS_TRUE ;
29
+
30
+ if ($ this ->isLowerCase ($ this ->conventionName ) || $ this ->isCamelCase ($ this ->conventionName ) || $ this ->isSnakeCase ($ this ->conventionName )) {
31
+ return Constant::STATUS_TRUE ;
32
+ }
33
+
34
+ return strtolower ($ this ->conventionName );
55
35
}
56
36
57
37
/**
58
38
* Check name only in alphabets.
59
39
* @param string $name
60
40
* @return string|bool
61
41
*/
62
- public function nameHasOnlyAlphabets ( string $ name ): string |bool
42
+ public function nameHasAlphabetCharacterSet ( ): string |bool
63
43
{
64
- $ title = str_replace ( ' ' , '' , $ this ->removeSpecialCharacter ( $ name ));
65
- if (! ctype_alpha ( $ title )) {
66
- $ result = $ this -> addSpecialCharacter ( preg_replace (Constant:: NUMERIC_PATTERN , '' , $ name ));
67
- return strtolower (( strpos ( $ result , " _ " ) === strlen ( $ result )- 1 )? substr_replace ( $ result , "" , - 1 ) : $ result );
44
+ if ( preg_match ( ' /^[A-Za-z$#_\s]+$/ ' , $ this ->conventionName )) {
45
+ return Constant:: STATUS_TRUE ;
46
+ } else {
47
+ return preg_replace ( ' /[^A-Za-z$#_]/ ' , '' , $ this -> conventionName );
68
48
}
69
- return Constant::STATUS_TRUE ;
70
49
}
71
50
72
51
/**
73
52
* Check name has fix length.
74
53
* @param string $name
75
54
* @return bool
76
55
*/
77
- public function nameHasFixLength (string $ name ): bool
56
+ public function nameHasFixLength (): bool
78
57
{
79
- if (strlen ($ name ) >= Constant::NAME_LENGTH ) {
58
+ if (strlen ($ this -> conventionName ) >= Constant::NAME_LENGTH ) {
80
59
return Constant::STATUS_FALSE ;
81
60
}
82
61
return Constant::STATUS_TRUE ;
@@ -87,9 +66,9 @@ public function nameHasFixLength(string $name): bool
87
66
* @param string $tableNames
88
67
* @return string|bool
89
68
*/
90
- public function nameHasNoPrefix (string $ tableNames ): string |bool
69
+ public function nameHasNoPrefix (): string |bool
91
70
{
92
- $ nameIdentify = explode ('_ ' , $ tableNames );
71
+ $ nameIdentify = explode ('_ ' , $ this -> conventionName );
93
72
$ name = $ nameIdentify [0 ];
94
73
if (strtolower ($ name ) === Constant::PREFIX_STRING ) {
95
74
return strtolower ($ nameIdentify [1 ]);
@@ -102,12 +81,54 @@ public function nameHasNoPrefix(string $tableNames): string|bool
102
81
* @param string $tableNames
103
82
* @return string|bool
104
83
*/
105
- public function nameAlwaysPlural (string $ tableNames ): string |bool
84
+ public function nameAlwaysPlural (): string |bool
106
85
{
107
- $ pluralName = Str::plural ($ tableNames );
108
- if ($ tableNames !== $ pluralName ) {
86
+ $ pluralName = Str::plural ($ this -> conventionName );
87
+ if ($ this -> conventionName !== $ pluralName ) {
109
88
return strtolower ($ pluralName );
110
89
}
111
90
return Constant::STATUS_TRUE ;
112
91
}
92
+
93
+ /**
94
+ * Check Name is snakeCase or not
95
+ * @param string $name
96
+ * @return bool
97
+ */
98
+ public function isCamelCase (string $ name )
99
+ {
100
+ return preg_match ('/^[a-z][a-zA-Z0-9]*$/ ' , $ name ) && preg_match ('/[A-Z]/ ' , $ name );
101
+ }
102
+
103
+ /**
104
+ * Check Name is snake_case or not
105
+ * @param string $name
106
+ * @return bool
107
+ */
108
+ public function isSnakeCase (string $ name ): bool
109
+ {
110
+ return preg_match ('/^[a-z0-9_]*$/ ' , $ name ) && strpos ($ name , '_ ' ) !== false ;
111
+ }
112
+
113
+ /**
114
+ * Check Name is lowercase or not
115
+ * @param string $name
116
+ * @return bool
117
+ */
118
+ public function isLowerCase (string $ name ): bool
119
+ {
120
+ return $ name === strtolower ($ name );
121
+ }
122
+
123
+ /**
124
+ * Convert string to snake case
125
+ * @param string $name
126
+ * @return string
127
+ */
128
+ public function convertToSnakeCase (string $ name ): string
129
+ {
130
+ $ snakeCase = str_replace (' ' , '_ ' , $ name );
131
+ $ snakeCase = strtolower ($ snakeCase );
132
+ return $ snakeCase ;
133
+ }
113
134
}
0 commit comments