diff --git a/README.md b/README.md index 69ae941..a027a2a 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ composer require devmoath/jql-builder ## Usage -Generate query with one condition: +Generate query with one condition: ```php \DevMoath\JqlBuilder\Jql::query() @@ -32,6 +32,17 @@ Generate query with many conditions: ->getQuery(); // "project = 'MY PROJECT' and issuetype = 'support' and status in ('wip', 'created')" ``` +Generate query with many conditions and order by: + +```php +\DevMoath\JqlBuilder\Jql::query() + ->whereProject('MY PROJECT') + ->whereIssueType('support') + ->whereStatus(['wip', 'created'], Jql::IN) + ->orderBy('created', Jql::ASC) + ->getQuery(); // "project = 'MY PROJECT' and issuetype = 'support' and status in ('wip', 'created') order by created asc" +``` + generate query with custom filed conditions: ```php @@ -50,7 +61,7 @@ generate query conditions based on your condition: ->getQuery(); // "project = 'MY PROJECT'" ``` -Also you can add macro functions as well: +Also, you can add macro functions as well: ```php $builder = new \DevMoath\JqlBuilder\Jql; diff --git a/src/Jql.php b/src/Jql.php index b6b2bf5..f7d470e 100644 --- a/src/Jql.php +++ b/src/Jql.php @@ -161,6 +161,11 @@ public function whenNot(mixed $value, callable $callback): self return $this; } + public function orderBy(string $column, string $direction): self + { + return tap($this, fn() => $this->appendQuery(self::ORDER_BY." $column $direction")); + } + public function getQuery(): string { return trim($this->query); @@ -171,7 +176,7 @@ public function appendQuery(string $query, string $boolean = ''): void if (empty($this->query)) { $this->query = $query; } else { - $this->query .= " $boolean $query"; + $this->query .= " ".trim("$boolean $query"); } } diff --git a/tests/JqlTest.php b/tests/JqlTest.php index 6a682e6..af77f90 100644 --- a/tests/JqlTest.php +++ b/tests/JqlTest.php @@ -29,6 +29,19 @@ public function it_can_generate_query_with_many_conditions(): void self::assertSame("project = 'MY PROJECT' and issuetype = 'support' and status in ('wip', 'created')", $query); } + /** @test */ + public function it_can_generate_query_with_many_conditions_and_order_by(): void + { + $query = Jql::query() + ->whereProject('MY PROJECT') + ->whereIssueType('support') + ->whereStatus(['wip', 'created'], Jql::IN) + ->orderBy('created', Jql::ASC) + ->getQuery(); + + self::assertSame("project = 'MY PROJECT' and issuetype = 'support' and status in ('wip', 'created') order by created asc", $query); + } + /** @test */ public function it_can_generate_query_with_custom_filed_conditions(): void {