Skip to content

Commit

Permalink
escape spaces in custom field names #24 (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
devmoath authored Oct 9, 2022
1 parent f0412e5 commit c3511f3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/Jql.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function where(

$this->invalidBooleanOrOperator($boolean, $operator, $value);

$this->appendQuery("$column $operator {$this->quote($operator, $value)}", $boolean);
$this->appendQuery("{$this->escapeSpaces($column)} $operator {$this->quote($operator, $value)}", $boolean);

return $this;
}
Expand Down Expand Up @@ -82,7 +82,7 @@ public function whenNot(mixed $value, callable $callback): self

public function orderBy(string $column, string $direction): self
{
$this->appendQuery(Keyword::ORDER_BY." $column $direction");
$this->appendQuery(Keyword::ORDER_BY." {$this->escapeSpaces($column)} $direction");

return $this;
}
Expand All @@ -104,6 +104,15 @@ public function __toString(): string
return $this->getQuery();
}

private function escapeSpaces(string $column): string
{
if (! str_contains($column, ' ')) {
return $column;
}

return "\"$column\"";
}

private function quote(string $operator, mixed $value): string
{
if (in_array($operator, [Operator::IN, Operator::NOT_IN, Operator::WAS_IN, Operator::WAS_NOT_IN])) {
Expand Down
6 changes: 6 additions & 0 deletions tests/JqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,10 @@ public function it_will_throw_exception_when_invalid_operator_passed(): void

(new Jql())->where('project', '=', ['MY PROJECT']);
}

/** @test */
public function it_will_qoute_custom_field_that_coontains_spaces(): void
{
$this->assertEquals('"project name" = "MY PROJECT"', (new Jql())->where('project name', '=', 'MY PROJECT')->getQuery());
}
}

0 comments on commit c3511f3

Please sign in to comment.