Hoa central
Attachment.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Mail\Content;
38 
39 use Hoa\Mime;
40 use Hoa\Stream;
41 
50 class Attachment extends Content
51 {
57  protected $_stream = null;
58 
59 
60 
70  public function __construct(Stream $stream, $name = null, $mimeType = null)
71  {
72  parent::__construct();
73 
74  if (null === $name) {
75  if ($stream instanceof Stream\IStream\Pathable) {
76  $name = $stream->getBasename();
77  } else {
78  $name = basename($stream->getStreamName());
79  }
80  }
81 
82  if (null === $mimeType) {
83  $mimeType = null;
84 
85  try {
86  $mime = new Mime($stream);
87  $mimeType = $mime->getMime();
88  } catch (Mime\Exception $e) {
89  }
90 
91  if (null === $mimeType) {
92  $mimeType = 'application/octet-stream';
93  }
94  }
95 
96  $size = null;
97 
98  if ($stream instanceof Stream\IStream\Statable &&
99  false !== $_size = $stream->getSize()) {
100  $size = '; size=' . $_size;
101  }
102 
103  $this['content-type'] = $mimeType;
104  $this['content-disposition'] =
105  'attachment; filename="' .
106  str_replace('"', '-', $name) .
107  '"' .
108  $size;
109 
110  $this->setStream($stream);
111 
112  return;
113  }
114 
121  protected function setStream(Stream $stream)
122  {
123  $old = $this->_stream;
124  $this->_stream = $stream;
125 
126  return $old;
127  }
128 
134  public function getStream()
135  {
136  return $this->_stream;
137  }
138 
144  protected function _getContent()
145  {
146  return Encoder\Base64::encode($this->getStream()->readAll());
147  }
148 }
static encode($string, $isHeaderValue=false)
Definition: Base64.php:59
setStream(Stream $stream)
Definition: Attachment.php:121
__construct(Stream $stream, $name=null, $mimeType=null)
Definition: Attachment.php:70