@@ -36,33 +36,17 @@ public function parse(): Interface\Results
3636 public function toQuery (): Interface \Query
3737 {
3838 $ this ->rewind ();
39- $ stream = null ;
4039 while (!$ this ->isEOF ()) {
4140 $ token = $ this ->nextToken ();
4241 if (strtoupper ($ token ) !== 'FROM ' ) {
4342 continue ;
4443 }
4544
46- $ fileQueryString = $ this ->nextToken ();
47- $ this ->validateFileQueryPath ($ fileQueryString );
48-
49- $ fileQuery = new Query \FileQuery ($ fileQueryString );
50- $ fileName = null ;
51- if ($ fileQuery ->file !== null ) {
52- $ fileName = $ this ->basePath !== null
53- ? $ this ->basePath . DIRECTORY_SEPARATOR . $ fileQuery ->file
54- : $ fileQuery ->file ;
55- }
56-
57- $ stream = Stream \Provider::fromFile ($ fileName ?? '' , $ fileQuery ->extension );
58- break ;
45+ $ fileQuery = $ this ->validateFileQueryPath ($ this ->nextToken ());
46+ return $ this ->parseWithQuery (Query \Provider::fromFileQuery ((string ) $ fileQuery ));
5947 }
6048
61- if ($ stream === null ) {
62- throw new Exception \UnexpectedValueException ('No query found ' );
63- }
64-
65- return $ this ->parseWithQuery ($ stream ->query ());
49+ throw new Exception \UnexpectedValueException ('Undefined file in query ' );
6650 }
6751
6852 /**
@@ -81,26 +65,23 @@ public function parseWithQuery(Interface\Query $query): Interface\Query
8165 break ;
8266
8367 case Interface \Query::FROM :
84- $ fileQuery = new Query \FileQuery ($ this ->nextToken ());
85- $ this ->validateFileQueryPath ($ fileQuery );
86-
68+ $ fileQuery = $ this ->validateFileQueryPath ($ this ->nextToken ());
8769 $ query ->from ($ fileQuery ->query ?? '' );
8870 break ;
8971
9072 case 'INNER ' :
9173 case 'LEFT ' :
9274 $ this ->nextToken (); // Consume "JOIN"
9375
94- $ joinQuery = $ this ->nextToken ();
95- $ this ->validateFileQueryPath ($ joinQuery );
76+ $ joinQuery = $ this ->validateFileQueryPath ($ this ->nextToken ());
9677
9778 $ this ->expect ('AS ' );
9879 $ alias = $ this ->nextToken ();
9980
10081 if (strtolower ($ token ) === 'left ' ) {
101- $ query ->leftJoin (Query \Provider::fromFileQuery ($ joinQuery ), $ alias );
82+ $ query ->leftJoin (Query \Provider::fromFileQuery (( string ) $ joinQuery ), $ alias );
10283 } elseif (strtolower ($ token ) === 'inner ' ) {
103- $ query ->innerJoin (Query \Provider::fromFileQuery ($ joinQuery ), $ alias );
84+ $ query ->innerJoin (Query \Provider::fromFileQuery (( string ) $ joinQuery ), $ alias );
10485 }
10586 $ this ->expect ('ON ' );
10687
@@ -111,13 +92,11 @@ public function parseWithQuery(Interface\Query $query): Interface\Query
11192 $ query ->on ($ field , $ operator , $ value );
11293 break ;
11394 case 'JOIN ' :
114- $ joinQuery = $ this ->nextToken ();
115- $ this ->validateFileQueryPath ($ joinQuery );
116-
95+ $ joinQuery = $ this ->validateFileQueryPath ($ this ->nextToken ());
11796 $ this ->expect (Interface \Query::AS );
11897 $ alias = $ this ->nextToken ();
11998
120- $ query ->innerJoin (Query \Provider::fromFileQuery ($ joinQuery ), $ alias );
99+ $ query ->innerJoin (Query \Provider::fromFileQuery (( string ) $ joinQuery ), $ alias );
121100 $ this ->expect ('ON ' );
122101
123102 $ field = $ this ->nextToken ();
@@ -238,6 +217,7 @@ private function applyFunctionToQuery(string $field, Interface\Query $query): vo
238217 {
239218 $ functionName = $ this ->getFunction ($ field );
240219 $ arguments = $ this ->getFunctionArguments ($ field );
220+ dump ($ arguments );
241221
242222 match (strtoupper ($ functionName )) {
243223 // aggregate
@@ -393,8 +373,12 @@ private function parseSort(Interface\Query $query): void
393373 $ direction = match ($ directionString ) {
394374 'ASC ' => Enum \Sort::ASC ,
395375 'DESC ' => Enum \Sort::DESC ,
396- default => throw new Exception \ SortException ( sprintf ( ' Invalid direction %s ' , $ directionString )) ,
376+ default => false ,
397377 };
378+ if ($ direction === false ) {
379+ $ direction = Enum \Sort::ASC ;
380+ $ this ->rewindToken ();
381+ }
398382 $ query ->orderBy ($ field , $ direction );
399383 }
400384 }
@@ -454,15 +438,15 @@ public function setBasePath(?string $basePath): void
454438 /**
455439 * @throws Exception\InvalidFormatException
456440 */
457- private function validateFileQueryPath (string $ fileQueryString ): void
441+ private function validateFileQueryPath (string $ fileQueryString ): Query \ FileQuery
458442 {
443+ $ fileQuery = new Query \FileQuery ($ fileQueryString );
459444 if ($ this ->basePath === null ) {
460- return ;
445+ return $ fileQuery ;
461446 }
462447
463- $ fileQuery = new Query \FileQuery ($ fileQueryString );
464448 if ($ fileQuery ->file === null ) {
465- return ;
449+ return $ fileQuery ;
466450 }
467451
468452 $ fileName = $ this ->basePath . DIRECTORY_SEPARATOR . $ fileQuery ->file ;
@@ -473,7 +457,9 @@ private function validateFileQueryPath(string $fileQueryString): void
473457 $ basePathRealPath === false ||
474458 !str_starts_with ($ fileNameRealPath , $ basePathRealPath )
475459 ) {
476- throw new Exception \InvalidFormatException ('Invalid query base path ' );
460+ throw new Exception \InvalidFormatException ('Invalid path of file ' );
477461 }
462+
463+ return $ fileQuery ->withFile ($ fileNameRealPath );
478464 }
479465}
0 commit comments