88class CreatorEntity implements IClassCreator
99{
1010 use CustomMySqlQueries;
11+
1112 protected const PARENT_NAME = 'Entity ' ;
1213
14+ private const BOOL_TYPE = 'bool ' ;
15+
1316 public function __construct (
1417 public Collection $ columns ,
1518 public string $ detectForeignKeys ,
@@ -32,13 +35,19 @@ public function createAttributs():array{
3235 $ detectForeignKeys = $ this ->detectForeignKeys ;
3336 $ tableName = $ this ->tableName ;
3437 $ attributes = [];
38+
3539 foreach ($ columns as $ _column ) {
40+ $ dataType = $ this ->getDataType ($ _column ->COLUMN_TYPE , $ _column ->DATA_TYPE );
3641 $ defaultValue = ($ _column ->COLUMN_DEFAULT ?? 'null ' ) ? ($ _column ->COLUMN_DEFAULT ?? 'null ' ) : "'' " ;
42+
43+ $ defaultValue = ($ dataType == self ::BOOL_TYPE ) ? ((in_array ($ defaultValue , [0 , '' , "'' " ])) ? 'false ' :
44+ ((in_array ($ defaultValue , [1 , '1 ' ])) ? 'true ' : $ defaultValue )) : $ defaultValue ;
45+
3746 $ attributes [$ _column ->COLUMN_NAME ] =
3847 $ this ->writeAttribute (
3948 $ entityStubsPath ,
40- $ _column ->COLUMN_NAME .($ _column ->IS_NULLABLE === ' YES ' ? ' = ' .$ defaultValue : '' ),
41- ($ _column ->IS_NULLABLE === 'YES ' ? 'null| ' : '' ) . $ this -> dataTypes [ $ _column -> DATA_TYPE ]
49+ $ _column ->COLUMN_NAME .(! in_array ( $ _column ->COLUMN_DEFAULT , [ null , ' NULL ' ]) ? ' = ' .$ defaultValue : '' ),
50+ ($ _column ->IS_NULLABLE === 'YES ' ? 'null| ' : '' ) . $ dataType
4251 );
4352 }
4453
@@ -73,18 +82,20 @@ public function createFunctions():array
7382 $ tableName = $ this ->tableName ;
7483 $ settersAndGetters = [];
7584 foreach ($ columns as $ _column ) {
85+ $ dataType = $ this ->getDataType ($ _column ->COLUMN_TYPE , $ _column ->DATA_TYPE );
86+
7687 $ settersAndGetters ['get ' .ucwords ($ _column ->COLUMN_NAME )] =
7788 $ this ->writeAccessors (
7889 $ entityStubsPath ,
7990 $ _column ->COLUMN_NAME ,
80- ($ _column ->IS_NULLABLE === 'YES ' ? 'null| ' : '' ) . $ this -> dataTypes [ $ _column -> DATA_TYPE ] ,
91+ ($ _column ->IS_NULLABLE === 'YES ' ? 'null| ' : '' ) . $ dataType ,
8192 'getter '
8293 );
8394 $ settersAndGetters ['set ' .ucwords ($ _column ->COLUMN_NAME )] =
8495 $ this ->writeAccessors (
8596 $ entityStubsPath ,
8697 $ _column ->COLUMN_NAME ,
87- ($ _column ->IS_NULLABLE === 'YES ' ? 'null| ' : '' ) . $ this -> dataTypes [ $ _column -> DATA_TYPE ] ,
98+ ($ _column ->IS_NULLABLE === 'YES ' ? 'null| ' : '' ) . $ dataType ,
8899 'setter '
89100 );
90101
@@ -94,14 +105,14 @@ public function createFunctions():array
94105
95106 // Create Additional Setters and Getters from Foreign keys
96107 foreach ($ foreignKeys as $ _foreignKey ) {
97- $ settersAndGetters ['get ' .ucwords ($ _column ->COLUMN_NAME )] =
108+ $ settersAndGetters ['get ' .ucwords ($ _foreignKey ->COLUMN_NAME )] =
98109 $ this ->writeAccessors (
99110 $ entityStubsPath ,
100111 $ _foreignKey ->VARIABLE_NAME ,
101112 $ _foreignKey ->ENTITY_DATA_TYPE ,
102113 'getter '
103114 );
104- $ settersAndGetters ['set ' .ucwords ($ _column ->COLUMN_NAME )] =
115+ $ settersAndGetters ['set ' .ucwords ($ _foreignKey ->COLUMN_NAME )] =
105116 $ this ->writeAccessors (
106117 $ entityStubsPath ,
107118 $ _foreignKey ->VARIABLE_NAME ,
@@ -115,7 +126,6 @@ public function createFunctions():array
115126
116127 private function writeAttribute (string $ entityStubsPath , string $ attributeName , string $ attributeType ): string
117128 {
118-
119129 $ attributeStub = file_get_contents ($ entityStubsPath .'attribute.stub ' );
120130 return str_replace (['{{ AttributeType }} ' , '{{ AttributeName }} ' ],
121131 [$ attributeType , $ attributeName ],
0 commit comments