Skip to content

Commit 0a76734

Browse files
committed
Added builder for ORDER keyword.
1 parent 997d824 commit 0a76734

2 files changed

Lines changed: 57 additions & 1 deletion

File tree

src/Components/OrderKeyword.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,19 @@ class OrderKeyword extends Component
3737
*
3838
* @var string
3939
*/
40-
public $type = 'ASC';
40+
public $type;
41+
42+
/**
43+
* Constructor.
44+
*
45+
* @param Expression $field The field that we are sorting by.
46+
* @param string $type The sorting type.
47+
*/
48+
public function __construct($field = null, $type = 'ASC')
49+
{
50+
$this->field = $field;
51+
$this->type = $type;
52+
}
4153

4254
/**
4355
* @param Parser $parser The parser that serves as context.
@@ -110,4 +122,22 @@ public static function parse(Parser $parser, TokensList $list, array $options =
110122
--$list->idx;
111123
return $ret;
112124
}
125+
126+
/**
127+
* @param OrderKeyword $component The component to be built.
128+
*
129+
* @return string
130+
*/
131+
public static function build($component)
132+
{
133+
if (is_array($component)) {
134+
$ret = array();
135+
foreach ($component as $c) {
136+
$ret[] = static::build($c);
137+
}
138+
return implode(", ", $ret);
139+
} else {
140+
return Expression::build($component->field) . ' ' . $component->type;
141+
}
142+
}
113143
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace SqlParser\Tests\Components;
4+
5+
use SqlParser\Parser;
6+
use SqlParser\Components\Expression;
7+
use SqlParser\Components\OrderKeyword;
8+
9+
use SqlParser\Tests\TestCase;
10+
11+
class OrderKeywordTest extends TestCase
12+
{
13+
14+
public function testBuild()
15+
{
16+
$this->assertEquals(
17+
OrderKeyword::build(
18+
array(
19+
new OrderKeyword(new Expression('a'), 'ASC'),
20+
new OrderKeyword(new Expression('b'), 'DESC')
21+
)
22+
),
23+
'a ASC, b DESC'
24+
);
25+
}
26+
}

0 commit comments

Comments
 (0)