Skip to content

Commit fa9de34

Browse files
committed
Add ability to fetch message size
1 parent 4e2e8ff commit fa9de34

File tree

7 files changed

+98
-3
lines changed

7 files changed

+98
-3
lines changed

src/FileMessage.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ public function uid(): int
2424
throw new BadMethodCallException('FileMessage does not support a UID');
2525
}
2626

27+
/**
28+
* {@inheritDoc}
29+
*/
30+
public function size(): ?int
31+
{
32+
return strlen($this->contents);
33+
}
34+
2735
/**
2836
* {@inheritDoc}
2937
*/

src/Message.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public function __construct(
2222
protected array $flags,
2323
protected string $head,
2424
protected string $body,
25+
protected ?int $size = null,
2526
) {}
2627

2728
/**
@@ -30,7 +31,7 @@ public function __construct(
3031
public function __sleep(): array
3132
{
3233
// We don't want to serialize the parsed message.
33-
return ['folder', 'uid', 'flags', 'headers', 'contents'];
34+
return ['folder', 'uid', 'flags', 'headers', 'contents', 'size'];
3435
}
3536

3637
/**
@@ -49,6 +50,14 @@ public function uid(): int
4950
return $this->uid;
5051
}
5152

53+
/**
54+
* Get the message's size in bytes (RFC822.SIZE).
55+
*/
56+
public function size(): ?int
57+
{
58+
return $this->size;
59+
}
60+
5261
/**
5362
* Get the message's flags.
5463
*/
@@ -203,6 +212,7 @@ public function toArray(): array
203212
'flags' => $this->flags,
204213
'head' => $this->head,
205214
'body' => $this->body,
215+
'size' => $this->size,
206216
];
207217
}
208218

src/MessageInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ interface MessageInterface extends FlaggableInterface, Stringable
1515
*/
1616
public function uid(): int;
1717

18+
/**
19+
* Get the message's size in bytes (RFC822.SIZE).
20+
*/
21+
public function size(): ?int;
22+
1823
/**
1924
* Get the message date and time.
2025
*/

src/MessageQuery.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ protected function populate(Collection $uids): MessageCollection
239239
$response['flags'] ?? [],
240240
$response['headers'] ?? '',
241241
$response['contents'] ?? '',
242+
$response['size'] ?? null,
242243
)
243244
);
244245
}
@@ -264,6 +265,10 @@ protected function fetch(Collection $messages): array
264265
$fetch[] = 'FLAGS';
265266
}
266267

268+
if ($this->fetchSize) {
269+
$fetch[] = 'RFC822.SIZE';
270+
}
271+
267272
if ($this->fetchBody) {
268273
$fetch[] = $this->fetchAsUnread
269274
? 'BODY.PEEK[TEXT]'
@@ -279,6 +284,7 @@ protected function fetch(Collection $messages): array
279284
if (empty($fetch)) {
280285
return $uids->mapWithKeys(fn (string|int $uid) => [
281286
$uid => [
287+
'size' => null,
282288
'flags' => [],
283289
'headers' => '',
284290
'contents' => '',
@@ -299,8 +305,11 @@ protected function fetch(Collection $messages): array
299305

300306
$uid = $data->lookup('UID')->value;
301307

308+
$size = $data->lookup('RFC822.SIZE')?->value;
309+
302310
return [
303311
$uid => [
312+
'size' => $size !== null ? (int) $size : null,
304313
'flags' => $data->lookup('FLAGS')?->values() ?? [],
305314
'headers' => $data->lookup('[HEADER]')->value ?? '',
306315
'contents' => $data->lookup('[TEXT]')->value ?? '',
@@ -356,9 +365,9 @@ protected function id(int $id, ImapFetchIdentifier $identifier = ImapFetchIdenti
356365
/**
357366
* Make a new message from given raw components.
358367
*/
359-
protected function newMessage(int $uid, array $flags, string $headers, string $contents): Message
368+
protected function newMessage(int $uid, array $flags, string $headers, string $contents, ?int $size = null): Message
360369
{
361-
return new Message($this->folder, $uid, $flags, $headers, $contents);
370+
return new Message($this->folder, $uid, $flags, $headers, $contents, $size);
362371
}
363372

364373
/**

src/MessageQueryInterface.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ public function isFetchingFlags(): bool;
6161
*/
6262
public function isFetchingHeaders(): bool;
6363

64+
/**
65+
* Determine if the size of messages is being fetched.
66+
*/
67+
public function isFetchingSize(): bool;
68+
6469
/**
6570
* Fetch the flags of messages.
6671
*/
@@ -76,6 +81,11 @@ public function withBody(): MessageQueryInterface;
7681
*/
7782
public function withHeaders(): MessageQueryInterface;
7883

84+
/**
85+
* Fetch the size of messages.
86+
*/
87+
public function withSize(): MessageQueryInterface;
88+
7989
/**
8090
* Don't fetch the body of messages.
8191
*/
@@ -91,6 +101,11 @@ public function withoutHeaders(): MessageQueryInterface;
91101
*/
92102
public function withoutFlags(): MessageQueryInterface;
93103

104+
/**
105+
* Don't fetch the size of messages.
106+
*/
107+
public function withoutSize(): MessageQueryInterface;
108+
94109
/**
95110
* Set the fetch order.
96111
*/

src/QueriesMessages.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ trait QueriesMessages
4040
*/
4141
protected bool $fetchHeaders = false;
4242

43+
/**
44+
* Whether to fetch the message size.
45+
*/
46+
protected bool $fetchSize = false;
47+
4348
/**
4449
* The fetch order.
4550
*
@@ -165,6 +170,14 @@ public function isFetchingHeaders(): bool
165170
return $this->fetchHeaders;
166171
}
167172

173+
/**
174+
* {@inheritDoc}
175+
*/
176+
public function isFetchingSize(): bool
177+
{
178+
return $this->fetchSize;
179+
}
180+
168181
/**
169182
* {@inheritDoc}
170183
*/
@@ -189,6 +202,14 @@ public function withHeaders(): MessageQueryInterface
189202
return $this->setFetchHeaders(true);
190203
}
191204

205+
/**
206+
* {@inheritDoc}
207+
*/
208+
public function withSize(): MessageQueryInterface
209+
{
210+
return $this->setFetchSize(true);
211+
}
212+
192213
/**
193214
* {@inheritDoc}
194215
*/
@@ -213,6 +234,14 @@ public function withoutFlags(): MessageQueryInterface
213234
return $this->setFetchFlags(false);
214235
}
215236

237+
/**
238+
* {@inheritDoc}
239+
*/
240+
public function withoutSize(): MessageQueryInterface
241+
{
242+
return $this->setFetchSize(false);
243+
}
244+
216245
/**
217246
* Set whether to fetch the flags.
218247
*/
@@ -243,6 +272,16 @@ protected function setFetchHeaders(bool $fetchHeaders): MessageQueryInterface
243272
return $this;
244273
}
245274

275+
/**
276+
* Set whether to fetch the size.
277+
*/
278+
protected function setFetchSize(bool $fetchSize): MessageQueryInterface
279+
{
280+
$this->fetchSize = $fetchSize;
281+
282+
return $this;
283+
}
284+
246285
/** {@inheritDoc} */
247286
public function setFetchOrder(string $fetchOrder): MessageQueryInterface
248287
{

src/Testing/FakeMessage.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public function __construct(
1919
protected int $uid,
2020
protected array $flags = [],
2121
protected string $contents = '',
22+
protected ?int $size = null,
2223
) {}
2324

2425
/**
@@ -29,6 +30,14 @@ public function uid(): int
2930
return $this->uid;
3031
}
3132

33+
/**
34+
* {@inheritDoc}
35+
*/
36+
public function size(): ?int
37+
{
38+
return $this->size;
39+
}
40+
3241
/**
3342
* {@inheritDoc}
3443
*/

0 commit comments

Comments
 (0)