Skip to content

Commit 7c641fb

Browse files
author
vc-urvin
committed
Update Naming Rules Logic
1 parent 15e8cff commit 7c641fb

File tree

3 files changed

+77
-60
lines changed

3 files changed

+77
-60
lines changed

src/Lang/en/messages.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
'plural' => 'Table name should be plural.',
4343
'space' => 'Space between words is not advised. Please Use Underscore "_"',
4444
'alphabets' => 'Numbers are not for names and is not advised! Please use alphabets for name.',
45-
'lowercase' => 'Name should be in lowercase.',
45+
'convention' => 'Name should be in lowercase, camelCase or snake_case.',
4646
'datatype_change' => 'Here you can use CHAR datatype instead of VARCHAR if data values in a column are of the same length.',
4747
],
4848
'question' => [

src/Traits/NamingRules.php

Lines changed: 68 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,81 +2,60 @@
22

33
namespace Vcian\LaravelDBAuditor\Traits;
44

5+
use Illuminate\Support\Facades\DB;
56
use Illuminate\Support\Str;
67
use Vcian\LaravelDBAuditor\Constants\Constant;
78

89
trait NamingRules
910
{
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;
2312

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-
}
3313

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)
4015
{
41-
return str_replace(" ", '_', $name);
16+
$this->conventionName = $name;
4217
}
4318

4419
/**
45-
* Check name has no space.
20+
* Check name only in lowercase. or camelCase or snake_case
4621
* @param string $name
4722
* @return string|bool
4823
*/
49-
public function nameHasNoSpace(string $name): string|bool
24+
public function nameConvention(): string|bool
5025
{
51-
if (str_contains($name, ' ')) {
52-
return strtolower($this->addSpecialCharacter($name));
26+
if (str_contains($this->conventionName, " ")) {
27+
return $this->convertToSnakeCase($this->conventionName);
5328
}
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);
5535
}
5636

5737
/**
5838
* Check name only in alphabets.
5939
* @param string $name
6040
* @return string|bool
6141
*/
62-
public function nameHasOnlyAlphabets(string $name): string|bool
42+
public function nameHasAlphabetCharacterSet(): string|bool
6343
{
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);
6848
}
69-
return Constant::STATUS_TRUE;
7049
}
7150

7251
/**
7352
* Check name has fix length.
7453
* @param string $name
7554
* @return bool
7655
*/
77-
public function nameHasFixLength(string $name): bool
56+
public function nameHasFixLength(): bool
7857
{
79-
if (strlen($name) >= Constant::NAME_LENGTH) {
58+
if (strlen($this->conventionName) >= Constant::NAME_LENGTH) {
8059
return Constant::STATUS_FALSE;
8160
}
8261
return Constant::STATUS_TRUE;
@@ -87,9 +66,9 @@ public function nameHasFixLength(string $name): bool
8766
* @param string $tableNames
8867
* @return string|bool
8968
*/
90-
public function nameHasNoPrefix(string $tableNames): string|bool
69+
public function nameHasNoPrefix(): string|bool
9170
{
92-
$nameIdentify = explode('_', $tableNames);
71+
$nameIdentify = explode('_', $this->conventionName);
9372
$name = $nameIdentify[0];
9473
if (strtolower($name) === Constant::PREFIX_STRING) {
9574
return strtolower($nameIdentify[1]);
@@ -102,12 +81,54 @@ public function nameHasNoPrefix(string $tableNames): string|bool
10281
* @param string $tableNames
10382
* @return string|bool
10483
*/
105-
public function nameAlwaysPlural(string $tableNames): string|bool
84+
public function nameAlwaysPlural(): string|bool
10685
{
107-
$pluralName = Str::plural($tableNames);
108-
if ($tableNames !== $pluralName) {
86+
$pluralName = Str::plural($this->conventionName);
87+
if ($this->conventionName !== $pluralName) {
10988
return strtolower($pluralName);
11089
}
11190
return Constant::STATUS_TRUE;
11291
}
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+
}
113134
}

src/Traits/Rules.php

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ public function checkRules(string $name, string $type = null): array
7171
{
7272
$messages = Constant::ARRAY_DECLARATION;
7373
try {
74-
$checkLowerCase = $this->nameOnlyLowerCase($name);
75-
$checkSpace = $this->nameHasNoSpace($name);
76-
$checkAlphabets = $this->nameHasOnlyAlphabets($name);
77-
74+
$this->setConvenationName($name);
75+
$checkConvention = $this->nameConvention();
76+
$checkAlphabets = $this->nameHasAlphabetCharacterSet();
77+
7878
if ($type === Constant::TABLE_RULES) {
79-
$checkLength = $this->nameHasFixLength($name);
80-
$checkNamePlural = $this->nameAlwaysPlural($name);
79+
$checkLength = $this->nameHasFixLength();
80+
$checkNamePlural = $this->nameAlwaysPlural();
8181

8282
if (!$checkLength) {
8383
$messages[] = __('Lang::messages.standard.error_message.length');
@@ -88,16 +88,12 @@ public function checkRules(string $name, string $type = null): array
8888
}
8989
}
9090

91-
if ($checkSpace !== Constant::STATUS_TRUE) {
92-
$messages[] = __('Lang::messages.standard.error_message.space') . "($checkSpace)";
93-
}
94-
9591
if ($checkAlphabets !== Constant::STATUS_TRUE) {
9692
$messages[] = __('Lang::messages.standard.error_message.alphabets') . "($checkAlphabets)";
9793
}
9894

99-
if ($checkLowerCase !== Constant::STATUS_TRUE) {
100-
$messages[] = __('Lang::messages.standard.error_message.lowercase') . "($checkLowerCase)";
95+
if ($checkConvention !== Constant::STATUS_TRUE) {
96+
$messages[] = __('Lang::messages.standard.error_message.convention') . "($checkConvention)";
10197
}
10298
} catch (Exception $exception) {
10399
Log::error($exception->getMessage());

0 commit comments

Comments
 (0)