Skip to content

Commit d189011

Browse files
refactor(Command): modify and refactor CreatorEntity (improvement code style)
1 parent f87003b commit d189011

File tree

1 file changed

+40
-29
lines changed

1 file changed

+40
-29
lines changed

src/Creators/CreatorEntity.php

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Eghamat24\DatabaseRepository\Creators;
44

5+
use Eghamat24\DatabaseRepository\Models\Enums\DataTypeEnum;
56
use Illuminate\Support\Collection;
67
use Eghamat24\DatabaseRepository\CustomMySqlQueries;
78

@@ -31,25 +32,21 @@ public function getExtendSection(): string
3132

3233
public function createAttributes(): array
3334
{
34-
$columns = $this->columns;
35-
$entityStubsPath = $this->entityStubsPath;
36-
$detectForeignKeys = $this->detectForeignKeys;
37-
$tableName = $this->tableName;
3835
$attributes = [];
3936

40-
foreach ($columns as $_column) {
37+
foreach ($this->columns as $_column) {
4138

4239
$dataType = $this->getDataType($_column->COLUMN_TYPE, $_column->DATA_TYPE);
4340

4441
$defaultValue = null;
4542
if ($_column->COLUMN_DEFAULT !== null) {
4643
$defaultValue = $_column->COLUMN_DEFAULT;
4744

48-
if ($dataType == 'int') {
45+
if ($dataType === DataTypeEnum::INTEGER_TYPE) {
4946
$defaultValue = intval($defaultValue);
5047
}
5148

52-
if ($dataType == self::BOOL_TYPE) {
49+
if ($dataType === self::BOOL_TYPE) {
5350
if (in_array($defaultValue, [0, '', "''"])) {
5451
$defaultValue = 'false';
5552
} elseif (in_array($defaultValue, [1, '1'])) {
@@ -59,29 +56,31 @@ public function createAttributes(): array
5956
}
6057

6158
$columnString = $_column->COLUMN_NAME;
59+
6260
if (!in_array($_column->COLUMN_DEFAULT, [null, 'NULL'])) {
6361
$columnString .= ' = ' . $defaultValue;
6462
}
63+
6564
if ($_column->IS_NULLABLE === 'YES') {
6665
$columnString .= ' = null';
6766
}
6867

6968
$attributes[$_column->COLUMN_NAME] =
7069
$this->writeAttribute(
71-
$entityStubsPath,
70+
$this->entityStubsPath,
7271
$columnString,
7372
($_column->IS_NULLABLE === 'YES' ? 'null|' : '') . $dataType
7473
);
7574
}
7675

77-
if ($detectForeignKeys) {
78-
$foreignKeys = $this->extractForeignKeys($tableName);
76+
if ($this->detectForeignKeys) {
77+
$foreignKeys = $this->extractForeignKeys($this->tableName);
7978

8079
// Create Additional Attributes from Foreign Keys
8180
foreach ($foreignKeys as $_foreignKey) {
8281
$attributes[$_column->COLUMN_NAME] =
8382
$this->writeAttribute(
84-
$entityStubsPath,
83+
$this->entityStubsPath,
8584
$_foreignKey->VARIABLE_NAME,
8685
$_foreignKey->ENTITY_DATA_TYPE
8786
);
@@ -93,73 +92,85 @@ public function createAttributes(): array
9392

9493
public function createUses(): array
9594
{
96-
return ["use Eghamat24\DatabaseRepository\Models\Entity\Entity;"];
95+
return ['use Eghamat24\DatabaseRepository\Models\Entity\Entity;'];
9796
}
9897

9998
public function createFunctions(): array
10099
{
101-
$columns = $this->columns;
102-
$entityStubsPath = $this->entityStubsPath;
103-
$detectForeignKeys = $this->detectForeignKeys;
104-
$tableName = $this->tableName;
105100
$settersAndGetters = [];
106-
foreach ($columns as $_column) {
101+
102+
foreach ($this->columns as $_column) {
107103
$dataType = $this->getDataType($_column->COLUMN_TYPE, $_column->DATA_TYPE);
108104

109105
$settersAndGetters['get' . ucwords($_column->COLUMN_NAME)] =
110106
$this->writeAccessors(
111-
$entityStubsPath,
107+
$this->entityStubsPath,
112108
$_column->COLUMN_NAME,
113109
($_column->IS_NULLABLE === 'YES' ? 'null|' : '') . $dataType,
114110
'getter'
115111
);
112+
116113
$settersAndGetters['set' . ucwords($_column->COLUMN_NAME)] =
117114
$this->writeAccessors(
118-
$entityStubsPath,
115+
$this->entityStubsPath,
119116
$_column->COLUMN_NAME,
120117
($_column->IS_NULLABLE === 'YES' ? 'null|' : '') . $dataType,
121118
'setter'
122119
);
123120

124121
}
125-
if ($detectForeignKeys) {
126-
$foreignKeys = $this->extractForeignKeys($tableName);
122+
123+
if ($this->detectForeignKeys) {
124+
$foreignKeys = $this->extractForeignKeys($this->tableName);
127125

128126
// Create Additional Setters and Getters from Foreign keys
129127
foreach ($foreignKeys as $_foreignKey) {
128+
130129
$settersAndGetters['get' . ucwords($_foreignKey->COLUMN_NAME)] =
131130
$this->writeAccessors(
132-
$entityStubsPath,
131+
$this->entityStubsPath,
133132
$_foreignKey->VARIABLE_NAME,
134133
$_foreignKey->ENTITY_DATA_TYPE,
135134
'getter'
136135
);
136+
137137
$settersAndGetters['set' . ucwords($_foreignKey->COLUMN_NAME)] =
138138
$this->writeAccessors(
139-
$entityStubsPath,
139+
$this->entityStubsPath,
140140
$_foreignKey->VARIABLE_NAME,
141141
$_foreignKey->ENTITY_DATA_TYPE,
142142
'setter'
143143
);
144144
}
145145
}
146+
146147
return $settersAndGetters;
147148
}
148149

149150
private function writeAttribute(string $entityStubsPath, string $attributeName, string $attributeType): string
150151
{
151152
$attributeStub = file_get_contents($entityStubsPath . 'attribute.stub');
152-
return str_replace(['{{ AttributeType }}', '{{ AttributeName }}'],
153-
[$attributeType, $attributeName],
154-
$attributeStub);
153+
154+
$replaceMapping = [
155+
'{{ AttributeType }}' => $attributeType,
156+
'{{ AttributeName }}' => $attributeName,
157+
];
158+
159+
return str_replace(array_keys($replaceMapping), array_values($replaceMapping), $attributeStub);
155160
}
156161

157162
private function writeAccessors(string $entityStubsPath, string $attributeName, string $attributeType, string $type): string
158163
{
159164
$accessorStub = file_get_contents($entityStubsPath . $type . '.stub');
160-
return str_replace(['{{ AttributeType }}', '{{ AttributeName }}', '{{ GetterName }}', '{{ SetterName }}'],
161-
[$attributeType, $attributeName, ucfirst($attributeName), ucfirst($attributeName)],
162-
$accessorStub);
165+
166+
$replaceMapping = [
167+
'{{ AttributeType }}' => $attributeType,
168+
'{{ AttributeName }}' => $attributeName,
169+
'{{ GetterName }}' => ucfirst($attributeName),
170+
'{{ SetterName }}' => ucfirst($attributeName)
171+
];
172+
173+
return str_replace(array_keys($replaceMapping), array_values($replaceMapping), $accessorStub);
163174
}
164175

165176
public function getNameSpace(): string

0 commit comments

Comments
 (0)