From 77cf08260aee96d734d3b7e2f4be8ce32ca03d99 Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Sat, 10 Sep 2022 22:52:40 -0500 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Support=20attachments?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Attachment.php | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/Map.php | 25 ++++++++++++++++++++++--- 2 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 src/Attachment.php diff --git a/src/Attachment.php b/src/Attachment.php new file mode 100644 index 0000000..5db4fd8 --- /dev/null +++ b/src/Attachment.php @@ -0,0 +1,46 @@ +map = $map; + $this->input = $file; + // Shouldn't be necessary, but just in case + $this->codec = new Format\Copy(); + } + + public function mime(string $mime): static + { + return $this->addMetadata('mimetype', $mime); + } + + public function setCodec(FormatInterface $codec): static + { + return $this; + } + + protected function parseCodec(): static + { + return $this; + } + + public function saveAttachment(): Map + { + return $this->saveStream();; + } + + public function buildCommand(int $idx = 0): array + { + $commands = parent::buildCommand($idx); + $commands[0] = '-attach'; + + return $commands; + } +} diff --git a/src/Map.php b/src/Map.php index d18196a..c6ef98c 100644 --- a/src/Map.php +++ b/src/Map.php @@ -13,6 +13,8 @@ class Map protected string $path; /** @var Stream[] */ protected array $streams = []; + /** @var Attachment[] */ + protected array $attachments = []; /** @var AbstractProgressListener[] */ protected array $listeners = []; @@ -35,9 +37,8 @@ class Map return $this; } - public function stream(callable $callback = null): Stream|static + protected function doStream(Stream $stream, callable $callback = null): Stream|static { - $stream = new Stream($this); if (!$callback) { return $stream; } @@ -48,8 +49,24 @@ class Map return $this; } + public function stream(callable $callback = null): Stream|static + { + return $this->doStream(new Stream($this), $callback); + } + + public function attach(string $file = '', callable $callback = null): Attachment|static + { + return $this->doStream(new Attachment($this, $file), $callback); + } + public function saveStream(Stream $stream): static { + if ($stream instanceof Attachment){ + $this->attachments[] = $stream; + + return $this; + } + $this->streams[] = $stream; $format = $stream->getCodec(); if ($format instanceof ProgressableInterface) { @@ -72,7 +89,9 @@ class Map public function buildCommand(): array { $commands = []; - foreach ($this->streams as $idx => $stream) { + $streams = $this->streams; + array_push($streams, ...$this->attachments); + foreach ($streams as $idx => $stream) { array_push($commands, ...$stream->buildCommand($idx)); } foreach ($this->metadata as $k => $v) {