Skip to content

Commit

Permalink
Update Naming Rules Logic
Browse files Browse the repository at this point in the history
  • Loading branch information
vc-urvin committed Jun 29, 2023
1 parent 15e8cff commit 7c641fb
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 60 deletions.
2 changes: 1 addition & 1 deletion src/Lang/en/messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
'plural' => 'Table name should be plural.',
'space' => 'Space between words is not advised. Please Use Underscore "_"',
'alphabets' => 'Numbers are not for names and is not advised! Please use alphabets for name.',
'lowercase' => 'Name should be in lowercase.',
'convention' => 'Name should be in lowercase, camelCase or snake_case.',
'datatype_change' => 'Here you can use CHAR datatype instead of VARCHAR if data values in a column are of the same length.',
],
'question' => [
Expand Down
115 changes: 68 additions & 47 deletions src/Traits/NamingRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,81 +2,60 @@

namespace Vcian\LaravelDBAuditor\Traits;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Vcian\LaravelDBAuditor\Constants\Constant;

trait NamingRules
{
/**
* Check name only in lowercase.
* @param string $name
* @return string|bool
*/
public function nameOnlyLowerCase(string $name): string|bool
{
$inputName = $this->removeSpecialCharacter($name);
if (strtolower($inputName) !== $inputName) {
return strtolower($this->addSpecialCharacter($name));
}
return Constant::STATUS_TRUE;
}
public string $conventionName;

/**
* Remove underscore from name.
* @param string $name
* @return string
*/
public function removeSpecialCharacter(string $name): string
{
return str_replace("_", '', $name);
}

/**
* Add special character
* @param string $name
* @return string
*/
public function addSpecialCharacter(string $name): string
public function setConvenationName(string $name)
{
return str_replace(" ", '_', $name);
$this->conventionName = $name;
}

/**
* Check name has no space.
* Check name only in lowercase. or camelCase or snake_case
* @param string $name
* @return string|bool
*/
public function nameHasNoSpace(string $name): string|bool
public function nameConvention(): string|bool
{
if (str_contains($name, ' ')) {
return strtolower($this->addSpecialCharacter($name));
if (str_contains($this->conventionName, " ")) {
return $this->convertToSnakeCase($this->conventionName);
}
return Constant::STATUS_TRUE;

if ($this->isLowerCase($this->conventionName) || $this->isCamelCase($this->conventionName) || $this->isSnakeCase($this->conventionName)) {
return Constant::STATUS_TRUE;
}

return strtolower($this->conventionName);
}

/**
* Check name only in alphabets.
* @param string $name
* @return string|bool
*/
public function nameHasOnlyAlphabets(string $name): string|bool
public function nameHasAlphabetCharacterSet(): string|bool
{
$title = str_replace(' ', '', $this->removeSpecialCharacter($name));
if (!ctype_alpha($title)) {
$result = $this->addSpecialCharacter(preg_replace(Constant::NUMERIC_PATTERN, '', $name));
return strtolower((strpos($result, "_") === strlen($result)-1 )? substr_replace($result ,"", -1) : $result);
if (preg_match('/^[A-Za-z$#_\s]+$/', $this->conventionName)) {
return Constant::STATUS_TRUE;
} else {
return preg_replace('/[^A-Za-z$#_]/', '', $this->conventionName);
}
return Constant::STATUS_TRUE;
}

/**
* Check name has fix length.
* @param string $name
* @return bool
*/
public function nameHasFixLength(string $name): bool
public function nameHasFixLength(): bool
{
if (strlen($name) >= Constant::NAME_LENGTH) {
if (strlen($this->conventionName) >= Constant::NAME_LENGTH) {
return Constant::STATUS_FALSE;
}
return Constant::STATUS_TRUE;
Expand All @@ -87,9 +66,9 @@ public function nameHasFixLength(string $name): bool
* @param string $tableNames
* @return string|bool
*/
public function nameHasNoPrefix(string $tableNames): string|bool
public function nameHasNoPrefix(): string|bool
{
$nameIdentify = explode('_', $tableNames);
$nameIdentify = explode('_', $this->conventionName);
$name = $nameIdentify[0];
if (strtolower($name) === Constant::PREFIX_STRING) {
return strtolower($nameIdentify[1]);
Expand All @@ -102,12 +81,54 @@ public function nameHasNoPrefix(string $tableNames): string|bool
* @param string $tableNames
* @return string|bool
*/
public function nameAlwaysPlural(string $tableNames): string|bool
public function nameAlwaysPlural(): string|bool
{
$pluralName = Str::plural($tableNames);
if ($tableNames !== $pluralName) {
$pluralName = Str::plural($this->conventionName);
if ($this->conventionName !== $pluralName) {
return strtolower($pluralName);
}
return Constant::STATUS_TRUE;
}

/**
* Check Name is snakeCase or not
* @param string $name
* @return bool
*/
public function isCamelCase(string $name)
{
return preg_match('/^[a-z][a-zA-Z0-9]*$/', $name) && preg_match('/[A-Z]/', $name);
}

/**
* Check Name is snake_case or not
* @param string $name
* @return bool
*/
public function isSnakeCase(string $name): bool
{
return preg_match('/^[a-z0-9_]*$/', $name) && strpos($name, '_') !== false;
}

/**
* Check Name is lowercase or not
* @param string $name
* @return bool
*/
public function isLowerCase(string $name): bool
{
return $name === strtolower($name);
}

/**
* Convert string to snake case
* @param string $name
* @return string
*/
public function convertToSnakeCase(string $name): string
{
$snakeCase = str_replace(' ', '_', $name);
$snakeCase = strtolower($snakeCase);
return $snakeCase;
}
}
20 changes: 8 additions & 12 deletions src/Traits/Rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ public function checkRules(string $name, string $type = null): array
{
$messages = Constant::ARRAY_DECLARATION;
try {
$checkLowerCase = $this->nameOnlyLowerCase($name);
$checkSpace = $this->nameHasNoSpace($name);
$checkAlphabets = $this->nameHasOnlyAlphabets($name);

$this->setConvenationName($name);
$checkConvention = $this->nameConvention();
$checkAlphabets = $this->nameHasAlphabetCharacterSet();
if ($type === Constant::TABLE_RULES) {
$checkLength = $this->nameHasFixLength($name);
$checkNamePlural = $this->nameAlwaysPlural($name);
$checkLength = $this->nameHasFixLength();
$checkNamePlural = $this->nameAlwaysPlural();

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

if ($checkSpace !== Constant::STATUS_TRUE) {
$messages[] = __('Lang::messages.standard.error_message.space') . "($checkSpace)";
}

if ($checkAlphabets !== Constant::STATUS_TRUE) {
$messages[] = __('Lang::messages.standard.error_message.alphabets') . "($checkAlphabets)";
}

if ($checkLowerCase !== Constant::STATUS_TRUE) {
$messages[] = __('Lang::messages.standard.error_message.lowercase') . "($checkLowerCase)";
if ($checkConvention !== Constant::STATUS_TRUE) {
$messages[] = __('Lang::messages.standard.error_message.convention') . "($checkConvention)";
}
} catch (Exception $exception) {
Log::error($exception->getMessage());
Expand Down

0 comments on commit 7c641fb

Please sign in to comment.