Skip to content

Commit d839a79

Browse files
committed
feat: remove set and get functions
1 parent a732631 commit d839a79

16 files changed

+40
-127
lines changed

src/Creators/BaseCreator.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,13 @@ public function createClass(string $filenameWithPath, BaseCommand $command): str
3333
$attributesArray = $this->checkDiffrence($filenameWithPath, $attributesArray, $command, $specificPattern, $generalPattern);
3434

3535
$attributes = trim(implode("\n\t", $attributesArray));
36-
$functions = trim(implode("\n", $functionsArray));
37-
$functions = (!empty($attributes)) ? "\n\n\t" . $functions : $functions;
36+
37+
$functions = '';
38+
if (count($functionsArray) > 0) {
39+
$functions = trim(implode("\n", $functionsArray));
40+
$functions = (!empty($attributes)) ? "\n\n\t" . $functions : $functions;
41+
}
42+
3843
$uses = implode(PHP_EOL, $usesArray);
3944

4045
$type = (isset($this->creator->enum)) ? self::ENUM_TYPE : self::CLASS_TYPE;

src/Creators/CreatorEntity.php

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -97,54 +97,7 @@ public function createUses(): array
9797

9898
public function createFunctions(): array
9999
{
100-
$settersAndGetters = [];
101-
102-
foreach ($this->columns as $_column) {
103-
$dataType = $this->getDataType($_column->COLUMN_TYPE, $_column->DATA_TYPE);
104-
105-
$settersAndGetters['get' . ucwords($_column->COLUMN_NAME)] =
106-
$this->writeAccessors(
107-
$this->entityStubsPath,
108-
$_column->COLUMN_NAME,
109-
($_column->IS_NULLABLE === 'YES' ? 'null|' : '') . $dataType,
110-
'getter'
111-
);
112-
113-
$settersAndGetters['set' . ucwords($_column->COLUMN_NAME)] =
114-
$this->writeAccessors(
115-
$this->entityStubsPath,
116-
$_column->COLUMN_NAME,
117-
($_column->IS_NULLABLE === 'YES' ? 'null|' : '') . $dataType,
118-
'setter'
119-
);
120-
121-
}
122-
123-
if ($this->detectForeignKeys) {
124-
$foreignKeys = $this->extractForeignKeys($this->tableName);
125-
126-
// Create Additional Setters and Getters from Foreign keys
127-
foreach ($foreignKeys as $_foreignKey) {
128-
129-
$settersAndGetters['get' . ucwords($_foreignKey->COLUMN_NAME)] =
130-
$this->writeAccessors(
131-
$this->entityStubsPath,
132-
$_foreignKey->VARIABLE_NAME,
133-
$_foreignKey->ENTITY_DATA_TYPE,
134-
'getter'
135-
);
136-
137-
$settersAndGetters['set' . ucwords($_foreignKey->COLUMN_NAME)] =
138-
$this->writeAccessors(
139-
$this->entityStubsPath,
140-
$_foreignKey->VARIABLE_NAME,
141-
$_foreignKey->ENTITY_DATA_TYPE,
142-
'setter'
143-
);
144-
}
145-
}
146-
147-
return $settersAndGetters;
100+
return [];
148101
}
149102

150103
private function writeAttribute(string $entityStubsPath, string $attributeName, string $attributeType): string

src/Creators/CreatorFactory.php

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,28 @@ public function getNameSpace(): string
2828

2929
public function createAttributes(): array
3030
{
31-
// TODO: Implement createAttributs() method.
32-
return [];
33-
}
34-
35-
public function createFunctions(): array
36-
{
37-
$setterStub = file_get_contents($this->factoryStubsPath . 'setter.stub');
38-
$setterFunctions = '';
31+
$setStub = file_get_contents($this->factoryStubsPath . 'set.stub');
32+
$sets = '';
3933
foreach ($this->columns as $_column) {
40-
$setterFunctions .= trim($this->writeSetter($setterStub, $_column->COLUMN_NAME)) . "\n\t\t";
34+
$replacementTokens = [
35+
'{{ AttributeName }}' => Str::camel($_column->COLUMN_NAME),
36+
'{{ DatabaseAttributeName }}' => Str::snake($_column->COLUMN_NAME)
37+
];
38+
39+
$sets .= str_replace(array_keys($replacementTokens), array_values($replacementTokens), $setStub) . "\t\t";
4140
}
4241

4342
return ['makeEntityFromStdClass' =>
44-
str_replace(['{{ SetterFunctions }}', '{{ EntityName }}', '{{ EntityVariableName }}'],
45-
[$setterFunctions, $this->entityName, $this->entityVariableName],
43+
str_replace(['{{ Sets }}', '{{ EntityName }}', '{{ EntityVariableName }}'],
44+
[$sets, $this->entityName, $this->entityVariableName],
4645
$this->baseContent)
4746
];
47+
return [];
48+
}
49+
50+
public function createFunctions(): array
51+
{
52+
return [];
4853
}
4954

5055
public function createUses(): array
@@ -62,16 +67,6 @@ public function getExtendSection(): string
6267
return 'extends ' . self::PARENT_NAME;
6368
}
6469

65-
public function writeSetter(string $setterStub, string $columnName): string
66-
{
67-
$replacementTokens = [
68-
'{{ SetterName }}' => ucfirst($columnName),
69-
'{{ AttributeName }}' => Str::snake($columnName)
70-
];
71-
72-
return str_replace(array_keys($replacementTokens), array_values($replacementTokens), $setterStub);
73-
}
74-
7570
public function getClassName(): string
7671
{
7772
return $this->factoryName;

src/Creators/CreatorMySqlRepository.php

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -153,21 +153,7 @@ private function writeGetAllFunction(string $getOneStub, string $columnName, str
153153
$getOneStub);
154154
}
155155

156-
private function writeGetterFunction(string $getterStub, string $columnName): string
157-
{
158-
return str_replace(['{{ ColumnName }}', '{{ GetterName }}'],
159-
[$columnName, ucfirst(Str::camel($columnName))],
160-
$getterStub);
161-
}
162-
163-
private function writeSetterFunction(string $setterStub, string $columnName): string
164-
{
165-
return str_replace('{{ SetterName }}',
166-
ucfirst(Str::camel($columnName)),
167-
$setterStub);
168-
}
169-
170-
private function getConstruct(string $tableName, string $factoryName, bool $hasSoftDelete, string $constructContent)
156+
private function getConstruct(string $tableName, string $factoryName, bool $hasSoftDelete, string $constructContent)
171157
{
172158
return str_replace(
173159
['{{ TableName }}', '{{ FactoryName }}', '{{ HasSoftDelete }}'],
@@ -186,11 +172,11 @@ public function makeCreateFunction(array &$stubContent, string &$getterFunctions
186172
{
187173
foreach ($this->columns as $_column) {
188174
if (!in_array($_column->COLUMN_NAME, ['id', 'deleted_at'])) {
189-
$getterFunctions .= trim($this->writeGetterFunction($stubContent['getterStub'], $_column->COLUMN_NAME)) . "\n\t\t\t\t";
175+
$getterFunctions .= trim(str_replace(['{{ ColumnName }}', '{{ AttributeName }}'], [$_column->COLUMN_NAME, Str::camel($_column->COLUMN_NAME)], $stubContent['getterStub'])) . "\n\t\t\t\t";
190176
}
191177

192178
if (in_array($_column->COLUMN_NAME, ['created_at', 'updated_at'], true)) {
193-
$setterFunctions .= trim($this->writeSetterFunction($stubContent['setterStub'], $_column->COLUMN_NAME)) . "\n\t\t";
179+
$setterFunctions .= trim(str_replace('{{ AttributeName }}', Str::camel($_column->COLUMN_NAME), $stubContent['setterStub'])) . "\n\t\t";
194180
}
195181
}
196182

@@ -216,11 +202,11 @@ public function makeUpdateFunction(array &$stubContent, string &$getterFunctions
216202
foreach ($this->columns as $_column) {
217203

218204
if (!in_array($_column->COLUMN_NAME, ['id', 'created_at', 'deleted_at'])) {
219-
$getterFunctions .= trim($this->writeGetterFunction($stubContent['getterStub'], $_column->COLUMN_NAME)) . "\n\t\t\t\t";
205+
$getterFunctions .= trim(str_replace(['{{ ColumnName }}', '{{ AttributeName }}'], [$_column->COLUMN_NAME, Str::camel($_column->COLUMN_NAME)], $stubContent['getterStub'])) . "\n\t\t\t\t";;
220206
}
221207

222208
if ($_column->COLUMN_NAME === 'updated_at') {
223-
$setterFunctions .= trim($this->writeSetterFunction($stubContent['setterStub'], $_column->COLUMN_NAME)) . "\n\t\t";
209+
$setterFunctions .= trim(str_replace('{{ AttributeName }}', Str::camel($_column->COLUMN_NAME), $stubContent['setterStub'])). "\n\t\t";;
224210
}
225211
}
226212

src/Creators/CreatorResource.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ public function createAttributes(): array
5555

5656
public function createFunctions(): array
5757
{
58-
$getterStub = file_get_contents($this->resourceStubsPath . 'getter.default.stub');
58+
$getsStub = file_get_contents($this->resourceStubsPath . 'getter.default.stub');
5959
$foreignGetterStub = file_get_contents($this->resourceStubsPath . 'getter.foreign.stub');
6060
$foreignFunStub = file_get_contents($this->resourceStubsPath . 'function.foreign.stub');
6161
$getterFunStub = file_get_contents($this->resourceStubsPath . 'function.getter.stub');
6262

6363
$getters = '';
6464
foreach ($this->columns as $_column) {
65-
$getters .= $this->writeGetter($getterStub, $_column->COLUMN_NAME, Str::camel($_column->COLUMN_NAME)) . "\t\t\t";
65+
$getters .= $this->writeGet($getsStub, $_column->COLUMN_NAME, Str::camel($_column->COLUMN_NAME)) . "\t\t\t";
6666
}
6767

6868
$foreignGetterFunctions = '';
@@ -82,11 +82,11 @@ public function createFunctions(): array
8282
return $functions;
8383
}
8484

85-
public function writeGetter(string $getterStub, string $columnName, string $attributeName): array|string
85+
public function writeGet(string $getterStub, string $columnName, string $attributeName): array|string
8686
{
8787
$replaceMapping = [
8888
'{{ ColumnName }}' => $columnName,
89-
'{{ GetterName }}' => ucfirst($attributeName),
89+
'{{ AttributeName }}' => Str::camel($attributeName),
9090
];
9191

9292
return str_replace(array_keys($replaceMapping), array_values($replaceMapping), $getterStub);

src/Models/Entity/Entity.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,11 @@ abstract class Entity implements JsonSerializable, Arrayable
1111
// contain originals value of attributes
1212
private $originals = [];
1313

14-
abstract public function getId(): int;
15-
1614
public function __construct()
1715
{
1816

1917
}
2018

21-
public function __set($name, $value)
22-
{
23-
if (property_exists($this, $name)) {
24-
$function = Str::camel('set_' . Str::snake($name));
25-
$this->$function($value);
26-
}
27-
}
28-
29-
public function __get($name)
30-
{
31-
if (property_exists($this, $name)) {
32-
$function = Str::camel('get_' . Str::snake($name));
33-
return $this->$function();
34-
}
35-
}
36-
3719
public function __isset($name)
3820
{
3921
return property_exists($this, $name);

stubs/Entities/entity.attribute.stub

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
protected {{ AttributeType }} ${{ AttributeName }};
1+
public {{ AttributeType }} ${{ AttributeName }};

stubs/Entities/entity.getter.stub

Lines changed: 0 additions & 4 deletions
This file was deleted.

stubs/Entities/entity.setter.stub

Lines changed: 0 additions & 4 deletions
This file was deleted.

stubs/Factories/factory.class.stub

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
{
33
${{ EntityVariableName }} = new {{ EntityName }}();
44

5-
{{ SetterFunctions }}
5+
{{ Sets }}
66
return ${{ EntityVariableName }};
77
}

stubs/Factories/factory.set.stub

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
${{ EntityVariableName }}->{{ AttributeName }} = $entity->{{ DatabaseAttributeName }};

stubs/Factories/factory.setter.stub

Lines changed: 0 additions & 1 deletion
This file was deleted.

stubs/Repositories/Mysql/mysql.create.stub

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
{{ GetterFunctions }}
88
]);
99

10-
${{ EntityVariableName }}->setId($id);
10+
${{ EntityVariableName }}->id = $id;
1111

1212
return ${{ EntityVariableName }};
1313
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
'{{ ColumnName }}' => ${{ EntityVariableName }}->get{{ GetterName }}(),
1+
'{{ ColumnName }}' => ${{ EntityVariableName }}->{{ AttributeName }},
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
${{ EntityVariableName }}->set{{ SetterName }}(date('Y-m-d H:i:s'));
1+
${{ EntityVariableName }}->{{ AttributeName }} = date('Y-m-d H:i:s');
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
'{{ ColumnName }}' => ${{ EntityVariableName }}->get{{ GetterName }}(),
1+
'{{ ColumnName }}' => ${{ EntityVariableName }}->{{ AttributeName }},

0 commit comments

Comments
 (0)