Skip to content

Commit 8006313

Browse files
committed
Attachment serialization store contents of file
1 parent e7cc70d commit 8006313

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. This projec
44
to [Semantic Versioning] (http://semver.org/). For change log format,
55
use [Keep a Changelog] (http://keepachangelog.com/).
66

7+
## [1.4.0] - 2025-01-13
8+
9+
### Changed
10+
11+
- Attachment serialization store contents of file
12+
713
## [1.3.2] - 2021-07-09
814

915
### Changed

src/Attachment.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class Attachment
3131
private $disposition;
3232
/** @var string File name */
3333
private $fileName;
34+
/** @var ?string Contents */
35+
private $_contents;
3436

3537
/**
3638
* Attachment constructor.
@@ -47,6 +49,28 @@ public function __construct(string $fileName)
4749
}
4850
}
4951

52+
public function __serialize(): array
53+
{
54+
return [
55+
'id' => $this->id,
56+
'type' => $this->getType(),
57+
'name' => $this->name,
58+
'disposition' => $this->disposition,
59+
'fileName' => $this->fileName,
60+
'contents' => $this->getContents(),
61+
];
62+
}
63+
64+
public function __unserialize(array $data): void
65+
{
66+
$this->id = $data['id'];
67+
$this->type = $data['type'];
68+
$this->name = $data['name'];
69+
$this->disposition = $data['disposition'];
70+
$this->fileName = $data['fileName'];
71+
$this->_contents = $data['contents'];
72+
}
73+
5074
/**
5175
* Get content ID.
5276
*
@@ -175,6 +199,10 @@ public function setDisposition(string $disposition): Attachment
175199
*/
176200
public function getContents()
177201
{
202+
if (null !== $this->_contents) {
203+
return $this->_contents;
204+
}
205+
178206
return file_get_contents($this->fileName);
179207
}
180208
}

0 commit comments

Comments
 (0)