@@ -33,21 +33,6 @@ class OrmClassesGenerator {
3333 */
3434 public const TEMPLATE_RECORD_FILE_NAME = 'TypeRecord.php.tpl ' ;
3535
36- /**
37- * @var string
38- */
39- public const DEFAULT_COLLECTION_CLASS_TO_EXTEND = '\\LeanOrm \\Model \\Collection ' ;
40-
41- /**
42- * @var string
43- */
44- public const DEFAULT_MODEL_CLASS_TO_EXTEND = '\\LeanOrm \\Model ' ;
45-
46- /**
47- * @var string
48- */
49- public const DEFAULT_RECORD_CLASS_TO_EXTEND = '\\LeanOrm \\Model \\Record ' ;
50-
5136 protected array $ defaultConfig = [];
5237
5338 protected string $ defaultTemplatesDirectory = '' ;
@@ -85,12 +70,14 @@ public function __construct(array $config) {
8570
8671 throw new \Exception ('`pdo` entry is missing in config! ' );
8772
88- } elseif (!is_array ($ this ->config ['pdo ' ])) {
73+ } elseif (!is_array ($ this ->config ['pdo ' ]) && !( $ this -> config [ ' pdo ' ] instanceof \ PDO ) ) {
8974
90- throw new \Exception ('`pdo` entry in config is not an array! ' );
75+ throw new \Exception ('`pdo` entry in config is not an array & is also not a PDO instance ! ' );
9176 }
9277
93- $ this ->pdo = new \PDO (...$ this ->config ['pdo ' ]);
78+ $ this ->pdo = ($ this ->config ['pdo ' ] instanceof \PDO )
79+ ? $ this ->config ['pdo ' ]
80+ : new \PDO (...$ this ->config ['pdo ' ]);
9481
9582 // fill config with defaults
9683 foreach ($ this ->defaultConfig as $ key =>$ val ) {
@@ -100,68 +87,83 @@ public function __construct(array $config) {
10087 $ this ->config [$ key ] = $ val ;
10188 }
10289 }
90+
91+ ////////////////////////////////////////////////////////////////////////
92+ // VALIDATE THE REMAINING ENTRIES IN THE CONFIG
93+ ////////////////////////////////////////////////////////////////////////
94+
95+ if (!is_string ($ this ->config ['namespace ' ]) && $ this ->config ['namespace ' ]!== null ) {
96+
97+ throw new \Exception ('`namespace` entry in config is not a string! ' );
98+ }
10399
104100 if (!is_string ($ this ->config ['directory ' ])) {
105101
106102 throw new \Exception ('`directory` entry in config is not a string! ' );
107103 }
104+
105+ if (!FileIoUtils::isDir ($ this ->config ['directory ' ])) {
108106
109- $ this ->destinationDirectory = $ this ->config ['directory ' ];
107+ throw new \Exception ('`directory` entry in config is not a valid directory! ' );
108+ }
110109
111- if (OtherUtils:: isNonEmptyString ( $ this ->config ['custom_templates_directory ' ])) {
110+ $ this ->destinationDirectory = $ this -> config ['directory ' ];
112111
112+ if (
113+ OtherUtils::isNonEmptyString ($ this ->config ['custom_templates_directory ' ])
114+ ) {
113115 if (!FileIoUtils::isDir ($ this ->config ['custom_templates_directory ' ])) {
114116
115117 throw new \Exception ('`custom_templates_directory` entry in config is not a valid directory! ' );
116118 }
117119
118120 $ this ->customTemplatesDirectory = $ this ->config ['custom_templates_directory ' ];
121+
122+ } elseif (
123+ !is_string ($ this ->config ['custom_templates_directory ' ])
124+ && $ this ->config ['custom_templates_directory ' ] !== null
125+ ) {
126+ throw new \Exception ('`custom_templates_directory` entry in config is not a string! ' );
119127 }
120-
128+
121129 if (!is_array ($ this ->config ['tables_to_skip ' ])) {
122130
123- $ this -> config [ ' tables_to_skip ' ] = [] ;
131+ throw new \ Exception ( ' ` tables_to_skip` entry in config is not an array of strings (names of tables & views to skip)! ' ) ;
124132 }
133+
134+ if (!is_string ($ this ->config ['collection_class_to_extend ' ])) {
125135
126- if (!OtherUtils::isNonEmptyString ($ this ->config ['collection_class_to_extend ' ])) {
127-
128- $ this ->config ['collection_class_to_extend ' ] = static ::DEFAULT_COLLECTION_CLASS_TO_EXTEND ;
136+ throw new \Exception ('`collection_class_to_extend` entry in config is not a string! ' );
129137 }
138+
139+ if (!is_string ($ this ->config ['model_class_to_extend ' ])) {
130140
131- if (!OtherUtils::isNonEmptyString ($ this ->config ['model_class_to_extend ' ])) {
132-
133- $ this ->config ['model_class_to_extend ' ] = static ::DEFAULT_MODEL_CLASS_TO_EXTEND ;
141+ throw new \Exception ('`model_class_to_extend` entry in config is not a string! ' );
134142 }
143+
144+ if (!is_string ($ this ->config ['record_class_to_extend ' ])) {
135145
136- if (!OtherUtils::isNonEmptyString ($ this ->config ['record_class_to_extend ' ])) {
137-
138- $ this ->config ['record_class_to_extend ' ] = static ::DEFAULT_RECORD_CLASS_TO_EXTEND ;
146+ throw new \Exception ('`record_class_to_extend` entry in config is not a string! ' );
139147 }
148+
149+ if (!is_string ($ this ->config ['created_timestamp_column_name ' ]) && $ this ->config ['created_timestamp_column_name ' ]!== null ) {
140150
141- if (
142- $ this ->config ['created_timestamp_column_name ' ] !== null
143- && !OtherUtils::isNonEmptyString ($ this ->config ['created_timestamp_column_name ' ])
144- ) {
145- $ this ->config ['created_timestamp_column_name ' ] = null ;
151+ throw new \Exception ('`created_timestamp_column_name` entry in config is not a string! ' );
146152 }
153+
154+ if (!is_string ($ this ->config ['updated_timestamp_column_name ' ]) && $ this ->config ['updated_timestamp_column_name ' ]!== null ) {
147155
148- if (
149- $ this ->config ['updated_timestamp_column_name ' ] !== null
150- && !OtherUtils::isNonEmptyString ($ this ->config ['updated_timestamp_column_name ' ])
151- ) {
152- $ this ->config ['updated_timestamp_column_name ' ] = null ;
156+ throw new \Exception ('`updated_timestamp_column_name` entry in config is not a string! ' );
153157 }
154-
158+
155159 if (!is_callable ($ this ->config ['table_name_to_record_class_prefix_transformer ' ])) {
156160
157- $ this ->config ['table_name_to_record_class_prefix_transformer ' ] =
158- $ this ->defaultConfig ['table_name_to_record_class_prefix_transformer ' ];
161+ throw new \Exception ('`table_name_to_record_class_prefix_transformer` entry in config is not a callable! ' );
159162 }
160-
163+
161164 if (!is_callable ($ this ->config ['table_name_to_collection_and_model_class_prefix_transformer ' ])) {
162165
163- $ this ->config ['table_name_to_collection_and_model_class_prefix_transformer ' ] =
164- $ this ->defaultConfig ['table_name_to_collection_and_model_class_prefix_transformer ' ];
166+ throw new \Exception ('`table_name_to_collection_and_model_class_prefix_transformer` entry in config is not a callable! ' );
165167 }
166168 }
167169
0 commit comments