blob: b134b8d9143a53f9a2c60a7713ee3a57af24ff5a [file] [log] [blame]
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001<?php
2
3declare(strict_types=1);
4
5namespace Ddeboer\Imap\Message;
6
7use Ddeboer\Imap\Exception\NotEmbeddedMessageException;
8
9/**
10 * An e-mail attachment.
11 */
12final class Attachment extends AbstractPart implements AttachmentInterface
13{
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010014 public function getFilename(): ?string
15 {
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020016 $filename = $this->getParameters()->get('filename');
17 if (null === $filename || '' === $filename) {
18 $filename = $this->getParameters()->get('name');
19 }
20 \assert(null === $filename || \is_string($filename));
21
22 return $filename;
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010023 }
24
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010025 public function getSize()
26 {
27 $size = $this->getParameters()->get('size');
28 if (\is_numeric($size)) {
29 $size = (int) $size;
30 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020031 \assert(null === $size || \is_int($size));
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010032
33 return $size;
34 }
35
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010036 public function isEmbeddedMessage(): bool
37 {
38 return self::TYPE_MESSAGE === $this->getType();
39 }
40
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010041 public function getEmbeddedMessage(): EmbeddedMessageInterface
42 {
43 if (!$this->isEmbeddedMessage()) {
44 throw new NotEmbeddedMessageException(\sprintf(
45 'Attachment "%s" in message "%s" is not embedded message',
46 $this->getPartNumber(),
47 $this->getNumber()
48 ));
49 }
50
51 return new EmbeddedMessage($this->resource, $this->getNumber(), $this->getPartNumber(), $this->getStructure()->parts[0]);
52 }
53}