@@ -10,20 +10,21 @@ class CreatorRepository implements IClassCreator
1010{
1111 public function __construct (
1212 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
2424 )
2525 {
2626 }
27+
2728 use CustomMySqlQueries;
2829
2930 private function writeFunction (string $ functionStub , string $ functionName , string $ columnName , string $ attributeType ): string
@@ -49,14 +50,14 @@ private function writeFunction(string $functionStub, string $functionName, strin
4950
5051 private function writeSqlAttribute (string $ attributeStub , string $ sqlRepositoryVariable , string $ sqlRepositoryName ): string
5152 {
52- return str_replace (['{{ SqlRepositoryVariable }} ' ,'{{ SqlRepositoryName }} ' ],
53- [$ sqlRepositoryVariable ,$ sqlRepositoryName ],
53+ return str_replace (['{{ SqlRepositoryVariable }} ' , '{{ SqlRepositoryName }} ' ],
54+ [$ sqlRepositoryVariable , $ sqlRepositoryName ],
5455 $ attributeStub );
5556 }
5657
5758 public function getNameSpace (): string
5859 {
59- return $ this ->repositoryNamespace . '\\' . $ this ->entityName ;
60+ return $ this ->repositoryNamespace . '\\' . $ this ->entityName ;
6061 }
6162
6263 public function createUses (): array
@@ -74,65 +75,66 @@ public function getClassName(): string
7475
7576 public function getExtendSection (): string
7677 {
77- return 'implements ' . $ this ->interfaceName ;
78+ return 'implements ' . $ this ->interfaceName ;
7879 }
7980
8081 public function createAttributs (): array
8182 {
82- $ attributeSqlStub = file_get_contents ($ this ->repositoryStubsPath .'attribute.sql.stub ' );
83- // Initialize Repository
83+ $ attributeSqlStub = file_get_contents ($ this ->repositoryStubsPath . 'attribute.sql.stub ' );
8484 $ 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 );
8786 return $ attributes ;
8887 }
8988
9089 public function createFunctions (): array
9190 {
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 ' );
9594
9695 $ functions = [];
97- $ functions ['__construct ' ] = $ this ->getConstruct ($ setterSqlStub ,$ constructStub );
96+ $ functions ['__construct ' ] = $ this ->getConstruct ($ setterSqlStub , $ constructStub );
9897 $ functions ['getOneById ' ] = $ this ->writeFunction ($ functionStub , 'getOneBy ' , 'id ' , 'int ' );
9998 $ 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+ }
100106
101107 if ($ this ->detectForeignKeys ) {
102108 $ foreignKeys = $ this ->extractForeignKeys ($ this ->tableName );
103109
104110 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-
108111 $ 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 ' );
110115 }
111116 }
112117
113118 $ functions ['create ' ] = $ this ->writeFunction ($ functionStub , 'create ' , $ this ->entityVariableName , $ this ->entityName );
114119 $ functions ['update ' ] = $ this ->writeFunction ($ functionStub , 'update ' , $ this ->entityVariableName , $ this ->entityName );
115120 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 );
118123 }
119124
120125 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 ],
123128 $ func
124129 );
125130 }
126131 return $ functions ;
127132 }
128133
129- public function getConstruct (string $ setterSqlStub ,string $ constructStub ){
134+ public function getConstruct (string $ setterSqlStub , string $ constructStub )
135+ {
130136 $ 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 );
137139 }
138140}
0 commit comments