@@ -10,20 +10,21 @@ class CreatorRepository implements IClassCreator
10
10
{
11
11
public function __construct (
12
12
public Collection $ columns ,
13
- public string $ sqlRepositoryVariable ,
14
- public string $ sqlRepositoryName ,
15
- public string $ repositoryStubsPath ,
16
- public string $ detectForeignKeys ,
17
- public string $ tableName ,
18
- public string $ entityVariableName ,
19
- public string $ entityName ,
20
- public string $ entityNamespace ,
21
- public string $ repositoryName ,
22
- public string $ interfaceName ,
23
- public string $ repositoryNamespace
13
+ public string $ sqlRepositoryVariable ,
14
+ public string $ sqlRepositoryName ,
15
+ public string $ repositoryStubsPath ,
16
+ public string $ detectForeignKeys ,
17
+ public string $ tableName ,
18
+ public string $ entityVariableName ,
19
+ public string $ entityName ,
20
+ public string $ entityNamespace ,
21
+ public string $ repositoryName ,
22
+ public string $ interfaceName ,
23
+ public string $ repositoryNamespace
24
24
)
25
25
{
26
26
}
27
+
27
28
use CustomMySqlQueries;
28
29
29
30
private function writeFunction (string $ functionStub , string $ functionName , string $ columnName , string $ attributeType ): string
@@ -49,14 +50,14 @@ private function writeFunction(string $functionStub, string $functionName, strin
49
50
50
51
private function writeSqlAttribute (string $ attributeStub , string $ sqlRepositoryVariable , string $ sqlRepositoryName ): string
51
52
{
52
- return str_replace (['{{ SqlRepositoryVariable }} ' ,'{{ SqlRepositoryName }} ' ],
53
- [$ sqlRepositoryVariable ,$ sqlRepositoryName ],
53
+ return str_replace (['{{ SqlRepositoryVariable }} ' , '{{ SqlRepositoryName }} ' ],
54
+ [$ sqlRepositoryVariable , $ sqlRepositoryName ],
54
55
$ attributeStub );
55
56
}
56
57
57
58
public function getNameSpace (): string
58
59
{
59
- return $ this ->repositoryNamespace . '\\' . $ this ->entityName ;
60
+ return $ this ->repositoryNamespace . '\\' . $ this ->entityName ;
60
61
}
61
62
62
63
public function createUses (): array
@@ -74,65 +75,66 @@ public function getClassName(): string
74
75
75
76
public function getExtendSection (): string
76
77
{
77
- return 'implements ' . $ this ->interfaceName ;
78
+ return 'implements ' . $ this ->interfaceName ;
78
79
}
79
80
80
81
public function createAttributs (): array
81
82
{
82
- $ attributeSqlStub = file_get_contents ($ this ->repositoryStubsPath .'attribute.sql.stub ' );
83
- // Initialize Repository
83
+ $ attributeSqlStub = file_get_contents ($ this ->repositoryStubsPath . 'attribute.sql.stub ' );
84
84
$ attributes = [];
85
- $ attributes [$ this ->sqlRepositoryVariable ] = $ this ->writeSqlAttribute ($ attributeSqlStub , $ this ->sqlRepositoryVariable ,$ this ->sqlRepositoryName );
86
- // dd($attributes);
85
+ $ attributes [$ this ->sqlRepositoryVariable ] = $ this ->writeSqlAttribute ($ attributeSqlStub , $ this ->sqlRepositoryVariable , $ this ->sqlRepositoryName );
87
86
return $ attributes ;
88
87
}
89
88
90
89
public function createFunctions (): array
91
90
{
92
- $ constructStub = file_get_contents ($ this ->repositoryStubsPath . 'construct.stub ' );
93
- $ functionStub = file_get_contents ($ this ->repositoryStubsPath . 'function.stub ' );
94
- $ setterSqlStub = file_get_contents ($ this ->repositoryStubsPath . 'setter.sql.stub ' );
91
+ $ constructStub = file_get_contents ($ this ->repositoryStubsPath . 'construct.stub ' );
92
+ $ functionStub = file_get_contents ($ this ->repositoryStubsPath . 'function.stub ' );
93
+ $ setterSqlStub = file_get_contents ($ this ->repositoryStubsPath . 'setter.sql.stub ' );
95
94
96
95
$ functions = [];
97
- $ functions ['__construct ' ] = $ this ->getConstruct ($ setterSqlStub ,$ constructStub );
96
+ $ functions ['__construct ' ] = $ this ->getConstruct ($ setterSqlStub , $ constructStub );
98
97
$ functions ['getOneById ' ] = $ this ->writeFunction ($ functionStub , 'getOneBy ' , 'id ' , 'int ' );
99
98
$ functions ['getAllByIds ' ] = $ this ->writeFunction ($ functionStub , 'getAllBy ' , 'id ' , 'array ' );
99
+ $ indexes = $ this ->extractIndexes ($ this ->tableName );
100
+ foreach ($ indexes as $ index ) {
101
+ $ fun_name = ucfirst (Str::plural (Str::camel ($ index ->COLUMN_NAME )));
102
+ $ functions ['getAllBy ' . $ fun_name ] = $ this ->writeFunction ($ functionStub , 'getAllBy ' , $ index ->COLUMN_NAME , 'array ' );
103
+ $ fun_name = ucfirst (Str::camel ($ index ->COLUMN_NAME ));
104
+ $ functions ['getOneBy ' . $ fun_name ] = $ this ->writeFunction ($ functionStub , 'getOneBy ' , $ index ->COLUMN_NAME , 'int ' );
105
+ }
100
106
101
107
if ($ this ->detectForeignKeys ) {
102
108
$ foreignKeys = $ this ->extractForeignKeys ($ this ->tableName );
103
109
104
110
foreach ($ foreignKeys as $ _foreignKey ) {
105
- $ fun_name = ucfirst (Str::plural (Str::camel ($ _foreignKey ->COLUMN_NAME )));
106
- $ functions [$ fun_name ] = $ this ->writeFunction ($ functionStub , 'getOneBy ' , $ _foreignKey ->COLUMN_NAME , 'int ' );
107
-
108
111
$ fun_name = ucfirst (Str::camel ($ _foreignKey ->COLUMN_NAME ));
109
- $ functions [$ fun_name ] = $ this ->writeFunction ($ functionStub , 'getAllBy ' , $ _foreignKey ->COLUMN_NAME , 'array ' );
112
+ $ functions ['getOneBy ' . $ fun_name ] = $ this ->writeFunction ($ functionStub , 'getOneBy ' , $ _foreignKey ->COLUMN_NAME , 'int ' );
113
+ $ fun_name = ucfirst (Str::plural (Str::camel ($ _foreignKey ->COLUMN_NAME )));
114
+ $ functions ['getAllBy ' . $ fun_name ] = $ this ->writeFunction ($ functionStub , 'getAllBy ' , $ _foreignKey ->COLUMN_NAME , 'array ' );
110
115
}
111
116
}
112
117
113
118
$ functions ['create ' ] = $ this ->writeFunction ($ functionStub , 'create ' , $ this ->entityVariableName , $ this ->entityName );
114
119
$ functions ['update ' ] = $ this ->writeFunction ($ functionStub , 'update ' , $ this ->entityVariableName , $ this ->entityName );
115
120
if (in_array ('deleted_at ' , $ this ->columns ->pluck ('COLUMN_NAME ' )->toArray (), true )) {
116
- $ functions ['remove ' ] = $ this ->writeFunction ($ functionStub , 'remove ' , $ this ->entityVariableName , $ this ->entityName );
117
- $ functions ['restore ' ] = $ this ->writeFunction ($ functionStub , 'restore ' , $ this ->entityVariableName , $ this ->entityName );
121
+ $ functions ['remove ' ] = $ this ->writeFunction ($ functionStub , 'remove ' , $ this ->entityVariableName , $ this ->entityName );
122
+ $ functions ['restore ' ] = $ this ->writeFunction ($ functionStub , 'restore ' , $ this ->entityVariableName , $ this ->entityName );
118
123
}
119
124
120
125
foreach ($ functions as &$ func ) {
121
- $ func= str_replace (["{{ SqlRepositoryVariable }} " ,'{{ SqlRepositoryName }} ' ,'{{ EntityName }} ' ],
122
- [$ this ->sqlRepositoryVariable ,$ this ->sqlRepositoryName ,$ this ->entityName ],
126
+ $ func = str_replace (["{{ SqlRepositoryVariable }} " , '{{ SqlRepositoryName }} ' , '{{ EntityName }} ' ],
127
+ [$ this ->sqlRepositoryVariable , $ this ->sqlRepositoryName , $ this ->entityName ],
123
128
$ func
124
129
);
125
130
}
126
131
return $ functions ;
127
132
}
128
133
129
- public function getConstruct (string $ setterSqlStub ,string $ constructStub ){
134
+ public function getConstruct (string $ setterSqlStub , string $ constructStub )
135
+ {
130
136
$ setters = '' ;
131
- $ setters = substr_replace ($ setters ,
132
- $ this ->writeSqlAttribute ($ setterSqlStub , $ this ->sqlRepositoryVariable ,$ this ->sqlRepositoryName ),
133
- -1 , 0 );
134
-
135
- return str_replace ("{{ Setters }} " ,$ setters ,$ constructStub );
136
-
137
+ $ setters = substr_replace ($ setters ,$ this ->writeSqlAttribute ($ setterSqlStub , $ this ->sqlRepositoryVariable , $ this ->sqlRepositoryName ),-1 , 0 );
138
+ return str_replace ("{{ Setters }} " , $ setters , $ constructStub );
137
139
}
138
140
}
0 commit comments