Skip to content

Commit fb187fb

Browse files
authored
Merge pull request #100 from DirectoryTree/fetch-since-uid
Add ability to fetch UID sequence range in messages query
2 parents e1abaf0 + 9c920bb commit fb187fb

File tree

4 files changed

+45
-5
lines changed

4 files changed

+45
-5
lines changed

src/Connection/ImapQueryBuilder.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,9 @@ public function header(string $header, string $value): static
230230
/**
231231
* Add a where "UID" clause to the query.
232232
*/
233-
public function uid(int|string|array $uid): static
233+
public function uid(int|string|array $from, int|float|null $to = null): static
234234
{
235-
return $this->where(ImapSearchKey::Uid, new RawQueryValue(
236-
Str::set(array_map('intval', (array) $uid))
237-
));
235+
return $this->where(ImapSearchKey::Uid, new RawQueryValue(Str::set($from, $to)));
238236
}
239237

240238
/**

src/Support/Str.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public static function enum(BackedEnum|string $enum): string
7777
/**
7878
* Make a range set for use in a search command.
7979
*/
80-
public static function set(array|int $from, int|float|null $to = null): string
80+
public static function set(int|string|array $from, int|float|string|null $to = null): string
8181
{
8282
// If $from is an array with multiple elements, return them as a comma-separated list.
8383
if (is_array($from) && count($from) > 1) {

tests/Unit/Connection/ImapQueryBuilderTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,43 @@ function (ImapQueryBuilder $q) {
244244

245245
expect($builder->toImap())->toBe('UID 2,3,5');
246246
});
247+
248+
test('compiles UID range to infinity with from and to', function () {
249+
$builder = new ImapQueryBuilder;
250+
251+
$builder->uid(2, INF);
252+
253+
expect($builder->toImap())->toBe('UID 2:*');
254+
});
255+
256+
test('compiles UID range with upper bound with array', function () {
257+
$builder = new ImapQueryBuilder;
258+
259+
$builder->uid([2, 5]);
260+
261+
expect($builder->toImap())->toBe('UID 2,5');
262+
});
263+
264+
test('compiles UID range with upper bound with from and to', function () {
265+
$builder = new ImapQueryBuilder;
266+
267+
$builder->uid(2, 5);
268+
269+
expect($builder->toImap())->toBe('UID 2:5');
270+
});
271+
272+
test('compiles UID range with single value', function () {
273+
$builder = new ImapQueryBuilder;
274+
275+
$builder->uid(2);
276+
277+
expect($builder->toImap())->toBe('UID 2');
278+
});
279+
280+
test('compiles UID range with single value array', function () {
281+
$builder = new ImapQueryBuilder;
282+
283+
$builder->uid([2]);
284+
285+
expect($builder->toImap())->toBe('UID 2');
286+
});

tests/Unit/Support/StrTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55

66
test('set', function () {
77
expect(Str::set(5, 10))->toBe('5:10');
8+
expect(Str::set('5', '10'))->toBe('5:10');
89
expect(Str::set(5, INF))->toBe('5:*');
910
expect(Str::set([5, 10]))->toBe('5,10');
11+
expect(Str::set(['5', '10']))->toBe('5,10');
1012
expect(Str::set([5]))->toBe('5');
1113
expect(Str::set(5))->toBe('5');
1214
});

0 commit comments

Comments
 (0)