Skip to content

Commit 289b155

Browse files
committed
✨ add ability to reset the current query #31
1 parent 4f239e3 commit 289b155

File tree

4 files changed

+48
-9
lines changed

4 files changed

+48
-9
lines changed

src/Jql.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ public function rawQuery(string $query): self
9595
return $this;
9696
}
9797

98+
public function reset(): self
99+
{
100+
$this->query = '';
101+
102+
return $this;
103+
}
104+
98105
public function getQuery(): string
99106
{
100107
return trim($this->query);
@@ -147,27 +154,23 @@ private function appendQuery(string $query, string $boolean = ''): void
147154
}
148155

149156
/**
150-
* @throws \InvalidArgumentException
157+
* @throws InvalidArgumentException
151158
*/
152159
private function invalidBooleanOrOperator(string $boolean, string $operator, mixed $value): void
153160
{
154-
if (! in_array($boolean, [Keyword::AND, Keyword::OR], true)) {
161+
if (! in_array($boolean, Keyword::booleans(), true)) {
155162
throw new InvalidArgumentException(sprintf(
156163
'Illegal boolean [%s] value. only [%s, %s] is acceptable',
157164
$boolean,
158-
Keyword::AND,
159-
Keyword::OR
165+
...Keyword::booleans(),
160166
));
161167
}
162168

163-
if (! in_array($operator, [Operator::IN, Operator::NOT_IN, Operator::WAS_IN, Operator::WAS_NOT_IN], true) && is_array($value)) {
169+
if (is_array($value) && ! in_array($operator, Operator::acceptList(), true)) {
164170
throw new InvalidArgumentException(sprintf(
165171
'Illegal operator [%s] value. only [%s, %s, %s, %s] is acceptable when $value type is array',
166172
$operator,
167-
Operator::IN,
168-
Operator::NOT_IN,
169-
Operator::WAS_IN,
170-
Operator::WAS_NOT_IN,
173+
...Operator::acceptList(),
171174
));
172175
}
173176
}

src/Keyword.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,15 @@ final class Keyword
1818
public const NULL = 'null';
1919

2020
public const ORDER_BY = 'order by';
21+
22+
/**
23+
* @return string[]
24+
*/
25+
public static function booleans(): array
26+
{
27+
return [
28+
self::AND,
29+
self::OR,
30+
];
31+
}
2132
}

src/Operator.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,17 @@ final class Operator
4040
public const WAS_NOT = 'was not';
4141

4242
public const CHANGED = 'changed';
43+
44+
/**
45+
* @return string[]
46+
*/
47+
public static function acceptList(): array
48+
{
49+
return [
50+
self::IN,
51+
self::NOT_IN,
52+
self::WAS_IN,
53+
self::WAS_NOT_IN,
54+
];
55+
}
4356
}

tests/Jql.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,15 @@
114114
it('will throw exception when invalid operator passed', function () {
115115
(new Jql())->where('project', '=', ['MY PROJECT']);
116116
})->throws(InvalidArgumentException::class, 'Illegal operator [=] value. only [in, not in, was in, was not in] is acceptable when $value type is array');
117+
118+
it('can reset the current query', function () {
119+
$query = new Jql();
120+
121+
$query->where('project', 'name');
122+
123+
expect($query)->getQuery()->toBe('project = "name"');
124+
125+
$query->reset();
126+
127+
expect($query)->getQuery()->toBeEmpty();
128+
});

0 commit comments

Comments
 (0)