Skip to content

Commit

Permalink
add when function to add where clause based on condition
Browse files Browse the repository at this point in the history
  • Loading branch information
devmoath committed Sep 16, 2021
1 parent 6c99a77 commit c62c841
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
22 changes: 20 additions & 2 deletions README.md
Expand Up @@ -13,9 +13,27 @@ composer require devmoath/jql-builder
```php
use DevMoath\JqlBuilder\Jql;

$example = (string) Jql::query()->whereProject('MY PROJECT'); // => "project = 'MY PROJECT'"
$example = (string) Jql::query()->whereProject('MY PROJECT');

$example = (string) Jql::query()->whereProject('MY PROJECT')->whereStatus(['wip', 'created']); // => "project = 'MY PROJECT' AND status in ('wip', 'created')"
echo $example; // "project = 'MY PROJECT'"

$example = (string) Jql::query()->whereProject('MY PROJECT')->whereStatus(['wip', 'created']);

echo $example; // "project = 'MY PROJECT' AND status in ('wip', 'created')"

$example = (string) Jql::query()->when('MY PROJECT', function (Jql $builder, $value) {
return $builder->whereProject($value);
});

echo $example; // "project = 'MY PROJECT'"

$testcase2 = (string) Jql::query()->when('', function (Jql $builder, $value) {
return $builder->whereProject($value);
});

echo $example; // ""

// more examples in the way.
```

## Testing
Expand Down
18 changes: 18 additions & 0 deletions src/Jql.php
Expand Up @@ -100,6 +100,24 @@ public function whereStatus(array|string $value, string $operator = self::IN): s
return $this;
}

public function when(mixed $value, callable $callback): self
{
if ($value) {
return $callback($this, $value);
}

return $this;
}

public function whenNot(mixed $value, callable $callback): self
{
if (! $value) {
return $callback($this, $value);
}

return $this;
}

public function __toString(): string
{
return trim(
Expand Down
19 changes: 18 additions & 1 deletion tests/JqlTest.php
Expand Up @@ -8,12 +8,29 @@
class JqlTest extends TestCase
{
/** @test */
public function it_can_generate_jql(): void
public function it_can_generate_simple_jql(): void
{
$testcase = (string) Jql::query()->whereProject('MY PROJECT');
$testcase2 = (string) Jql::query()->whereProject('MY PROJECT')->whereStatus(['wip', 'created']);

$this->assertSame("project = 'MY PROJECT'", $testcase);
$this->assertSame("project = 'MY PROJECT' AND status in ('wip', 'created')", $testcase2);
}

/** @test */
public function it_can_generate_simple_jql_using_condition(): void
{
$projectName = 'MY PROJECT';
$emptyProjectName = '';

$testcase = (string) Jql::query()->when($projectName, function (Jql $builder, $value) {
return $builder->whereProject($value);
});
$testcase2 = (string) Jql::query()->when($emptyProjectName, function (Jql $builder, $value) {
return $builder->whereProject($value);
});

$this->assertSame("project = 'MY PROJECT'", $testcase);
$this->assertSame("", $testcase2);
}
}

0 comments on commit c62c841

Please sign in to comment.