44
55namespace midorikocak \querymaker ;
66
7- use Exception ;
87use InvalidArgumentException ;
98
109use function array_keys ;
@@ -24,6 +23,11 @@ class QueryMaker implements QueryInterface
2423 private string $ orderBy ;
2524
2625 public function __construct ()
26+ {
27+ $ this ->reset ();
28+ }
29+
30+ private function reset (): void
2731 {
2832 $ this ->query = '' ;
2933 $ this ->statement = '' ;
@@ -35,8 +39,7 @@ public function __construct()
3539
3640 public function select ($ table , array $ columns = ['* ' ]): QueryInterface
3741 {
38- $ this ->checkInitialized ();
39-
42+ $ this ->reset ();
4043 $ columnsText = implode (', ' , $ columns );
4144 $ this ->statement = 'SELECT ' . $ columnsText . ' FROM ' . $ table ;
4245 $ this ->query = 'SELECT ' . $ columnsText . ' FROM ' . $ table ;
@@ -45,8 +48,7 @@ public function select($table, array $columns = ['*']): QueryInterface
4548
4649 public function update ($ table , array $ values ): QueryInterface
4750 {
48- $ this ->checkInitialized ();
49-
51+ $ this ->reset ();
5052 $ this ->statement = 'UPDATE ' . $ table . ' SET ' ;
5153 $ this ->query = 'UPDATE ' . $ table . ' SET ' ;
5254 $ this ->prepareParams ($ values , ', ' );
@@ -55,7 +57,7 @@ public function update($table, array $values): QueryInterface
5557
5658 public function insert ($ table , array $ values ): QueryInterface
5759 {
58- $ this ->checkInitialized ();
60+ $ this ->reset ();
5961 $ fields = implode (', ' , array_keys ($ values ));
6062 $ params = implode (', ' , array_map (fn ($ key ) => ': ' . $ key , array_keys ($ values )));
6163 $ queryValues = implode (', ' , array_map (fn ($ value ) => "' $ value' " , array_values ($ values )));
@@ -69,7 +71,7 @@ public function insert($table, array $values): QueryInterface
6971
7072 public function delete ($ table ): QueryInterface
7173 {
72- $ this ->checkInitialized ();
74+ $ this ->reset ();
7375 $ this ->statement = 'DELETE FROM ' . $ table ;
7476 $ this ->query = 'DELETE FROM ' . $ table ;
7577 return $ this ;
@@ -189,11 +191,4 @@ private function checkOperator(string $operator): void
189191 throw new InvalidArgumentException ('Invalid Operator ' );
190192 }
191193 }
192-
193- private function checkInitialized ()
194- {
195- if ($ this ->statement !== '' && $ this ->query !== '' ) {
196- throw new Exception ('Invalid Query Order ' );
197- }
198- }
199194}
0 commit comments