Skip to content

Commit ac8dab3

Browse files
Merge pull request #596 from kamil-tekiela/OrderSortKeyword
OrderSortKeyword enum
2 parents 5c4dc47 + ce86c62 commit ac8dab3

21 files changed

+114
-29
lines changed

src/Components/GroupKeyword.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
*/
1414
final class GroupKeyword implements Component
1515
{
16-
/** @var 'ASC'|'DESC'|null */
17-
public string|null $type = null;
16+
public OrderSortKeyword|null $type = null;
1817

1918
/**
2019
* The expression that is used for grouping.

src/Components/OrderKeyword.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,21 @@ final class OrderKeyword implements Component
1919
/**
2020
* The order type.
2121
*/
22-
public string $type;
22+
public OrderSortKeyword $type;
2323

2424
/**
25-
* @param Expression|null $expr the expression that we are sorting by
26-
* @param string $type the sorting type
25+
* @param Expression|null $expr the expression that we are sorting by
26+
* @param OrderSortKeyword $type the sorting type
2727
*/
28-
public function __construct(Expression|null $expr = null, string $type = 'ASC')
28+
public function __construct(Expression|null $expr = null, OrderSortKeyword $type = OrderSortKeyword::Asc)
2929
{
3030
$this->expr = $expr;
3131
$this->type = $type;
3232
}
3333

3434
public function build(): string
3535
{
36-
return $this->expr . ' ' . $this->type;
36+
return $this->expr . ' ' . $this->type->value;
3737
}
3838

3939
public function __toString(): string
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpMyAdmin\SqlParser\Components;
6+
7+
enum OrderSortKeyword: string
8+
{
9+
case Asc = 'ASC';
10+
case Desc = 'DESC';
11+
}

src/Parsers/GroupKeywords.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PhpMyAdmin\SqlParser\Parsers;
66

77
use PhpMyAdmin\SqlParser\Components\GroupKeyword;
8+
use PhpMyAdmin\SqlParser\Components\OrderSortKeyword;
89
use PhpMyAdmin\SqlParser\Parseable;
910
use PhpMyAdmin\SqlParser\Parser;
1011
use PhpMyAdmin\SqlParser\TokensList;
@@ -66,7 +67,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
6667
($token->type === TokenType::Keyword)
6768
&& (($token->keyword === 'ASC') || ($token->keyword === 'DESC'))
6869
) {
69-
$expr->type = $token->keyword;
70+
$expr->type = OrderSortKeyword::from($token->keyword);
7071
} elseif (($token->type === TokenType::Operator) && ($token->value === ',')) {
7172
if (! empty($expr->expr)) {
7273
$ret[] = $expr;

src/Parsers/OrderKeywords.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PhpMyAdmin\SqlParser\Parsers;
66

77
use PhpMyAdmin\SqlParser\Components\OrderKeyword;
8+
use PhpMyAdmin\SqlParser\Components\OrderSortKeyword;
89
use PhpMyAdmin\SqlParser\Parseable;
910
use PhpMyAdmin\SqlParser\Parser;
1011
use PhpMyAdmin\SqlParser\TokensList;
@@ -66,7 +67,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
6667
($token->type === TokenType::Keyword)
6768
&& (($token->keyword === 'ASC') || ($token->keyword === 'DESC'))
6869
) {
69-
$expr->type = $token->keyword;
70+
$expr->type = OrderSortKeyword::from($token->keyword);
7071
} elseif (($token->type === TokenType::Operator) && ($token->value === ',')) {
7172
if (! empty($expr->expr)) {
7273
$ret[] = $expr;

tests/Components/OrderKeywordTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PhpMyAdmin\SqlParser\Components\Expression;
88
use PhpMyAdmin\SqlParser\Components\OrderKeyword;
9+
use PhpMyAdmin\SqlParser\Components\OrderSortKeyword;
910
use PhpMyAdmin\SqlParser\Parsers\OrderKeywords;
1011
use PhpMyAdmin\SqlParser\Tests\TestCase;
1112

@@ -17,8 +18,8 @@ public function testBuildAll(): void
1718
'a ASC, b DESC',
1819
OrderKeywords::buildAll(
1920
[
20-
new OrderKeyword(new Expression('a'), 'ASC'),
21-
new OrderKeyword(new Expression('b'), 'DESC'),
21+
new OrderKeyword(new Expression('a'), OrderSortKeyword::Asc),
22+
new OrderKeyword(new Expression('b'), OrderSortKeyword::Desc),
2223
],
2324
),
2425
);

tests/data/parser/parseDelete.out

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,11 @@
547547
"function": null,
548548
"subquery": null
549549
},
550-
"type": "ASC"
550+
"type": {
551+
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
552+
"name": "Asc",
553+
"value": "ASC"
554+
}
551555
}
552556
],
553557
"limit": null,

tests/data/parser/parseDelete4.out

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,11 @@
277277
"function": null,
278278
"subquery": null
279279
},
280-
"type": "ASC"
280+
"type": {
281+
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
282+
"name": "Asc",
283+
"value": "ASC"
284+
}
281285
}
282286
],
283287
"limit": null,

tests/data/parser/parseDelete5.out

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,11 @@
343343
"function": null,
344344
"subquery": null
345345
},
346-
"type": "ASC"
346+
"type": {
347+
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
348+
"name": "Asc",
349+
"value": "ASC"
350+
}
347351
}
348352
],
349353
"limit": {

tests/data/parser/parseDelete6.out

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,11 @@
197197
"function": null,
198198
"subquery": null
199199
},
200-
"type": "ASC"
200+
"type": {
201+
"@type": "PhpMyAdmin\\SqlParser\\Components\\OrderSortKeyword",
202+
"name": "Asc",
203+
"value": "ASC"
204+
}
201205
}
202206
],
203207
"limit": null,

0 commit comments

Comments
 (0)