Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Ddeboer\Imap\Message; |
| 6 | |
| 7 | use Ddeboer\Imap\Exception\NotEmbeddedMessageException; |
| 8 | |
| 9 | /** |
| 10 | * An e-mail attachment. |
| 11 | */ |
| 12 | final class Attachment extends AbstractPart implements AttachmentInterface |
| 13 | { |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 14 | public function getFilename(): ?string |
| 15 | { |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 16 | $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 Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 23 | } |
| 24 | |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 25 | public function getSize() |
| 26 | { |
| 27 | $size = $this->getParameters()->get('size'); |
| 28 | if (\is_numeric($size)) { |
| 29 | $size = (int) $size; |
| 30 | } |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 31 | \assert(null === $size || \is_int($size)); |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 32 | |
| 33 | return $size; |
| 34 | } |
| 35 | |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 36 | public function isEmbeddedMessage(): bool |
| 37 | { |
| 38 | return self::TYPE_MESSAGE === $this->getType(); |
| 39 | } |
| 40 | |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 41 | 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 | } |