Skip to content

Commit

Permalink
add orderBy function
Browse files Browse the repository at this point in the history
  • Loading branch information
devmoath committed Oct 29, 2021
1 parent 93c904f commit 21e26ed
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
15 changes: 13 additions & 2 deletions README.md
Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -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;
Expand Down
7 changes: 6 additions & 1 deletion src/Jql.php
Expand Up @@ -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);
Expand All @@ -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");
}
}

Expand Down
13 changes: 13 additions & 0 deletions tests/JqlTest.php
Expand Up @@ -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
{
Expand Down

0 comments on commit 21e26ed

Please sign in to comment.