summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/Components/OrderKeyword.php32
-rw-r--r--tests/Components/OrderKeywordTest.php26
2 files changed, 57 insertions, 1 deletions
diff --git a/src/Components/OrderKeyword.php b/src/Components/OrderKeyword.php
index 26d0f20..7f1879c 100644
--- a/src/Components/OrderKeyword.php
+++ b/src/Components/OrderKeyword.php
@@ -37,7 +37,19 @@ class OrderKeyword extends Component
*
* @var string
*/
- public $type = 'ASC';
+ public $type;
+
+ /**
+ * Constructor.
+ *
+ * @param Expression $field The field that we are sorting by.
+ * @param string $type The sorting type.
+ */
+ public function __construct($field = null, $type = 'ASC')
+ {
+ $this->field = $field;
+ $this->type = $type;
+ }
/**
* @param Parser $parser The parser that serves as context.
@@ -110,4 +122,22 @@ class OrderKeyword extends Component
--$list->idx;
return $ret;
}
+
+ /**
+ * @param OrderKeyword $component The component to be built.
+ *
+ * @return string
+ */
+ public static function build($component)
+ {
+ if (is_array($component)) {
+ $ret = array();
+ foreach ($component as $c) {
+ $ret[] = static::build($c);
+ }
+ return implode(", ", $ret);
+ } else {
+ return Expression::build($component->field) . ' ' . $component->type;
+ }
+ }
}
diff --git a/tests/Components/OrderKeywordTest.php b/tests/Components/OrderKeywordTest.php
new file mode 100644
index 0000000..df8c95c
--- /dev/null
+++ b/tests/Components/OrderKeywordTest.php
@@ -0,0 +1,26 @@
+<?php
+
+namespace SqlParser\Tests\Components;
+
+use SqlParser\Parser;
+use SqlParser\Components\Expression;
+use SqlParser\Components\OrderKeyword;
+
+use SqlParser\Tests\TestCase;
+
+class OrderKeywordTest extends TestCase
+{
+
+ public function testBuild()
+ {
+ $this->assertEquals(
+ OrderKeyword::build(
+ array(
+ new OrderKeyword(new Expression('a'), 'ASC'),
+ new OrderKeyword(new Expression('b'), 'DESC')
+ )
+ ),
+ 'a ASC, b DESC'
+ );
+ }
+}