PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/mail/Attachment.php

http://github.com/sendgrid/sendgrid-php
PHP | 225 lines | 107 code | 21 blank | 97 comment | 9 complexity | c968420a139108758845e6bfece544d5 MD5 | raw file
Possible License(s): MIT, LGPL-3.0
  1. <?php
  2. /**
  3. * This helper builds the Attachment object for a /mail/send API call
  4. */
  5. namespace SendGrid\Mail;
  6. use SendGrid\Helper\Assert;
  7. /**
  8. * This class is used to construct a Attachment object for the /mail/send API call
  9. *
  10. * @package SendGrid\Mail
  11. */
  12. class Attachment implements \JsonSerializable
  13. {
  14. /** @var $content string Base64 encoded content */
  15. private $content;
  16. /** @var $type string Mime type of the attachment */
  17. private $type;
  18. /** @var $filename string File name of the attachment */
  19. private $filename;
  20. /** @var $disposition string How the attachment should be displayed: inline or attachment, default is attachment */
  21. private $disposition;
  22. /** @var $content_id string Used when disposition is inline to display the file within the body of the email */
  23. private $content_id;
  24. /**
  25. * Optional constructor
  26. *
  27. * @param string $content Base64 encoded content
  28. * @param string $type Mime type of the attachment
  29. * @param string $filename File name of the attachment
  30. * @param string $disposition How the attachment should be displayed: inline
  31. * or attachment, default is attachment
  32. * @param string $content_id Used when disposition is inline to display the
  33. * file within the body of the email
  34. * @throws \SendGrid\Mail\TypeException
  35. */
  36. public function __construct(
  37. $content = null,
  38. $type = null,
  39. $filename = null,
  40. $disposition = null,
  41. $content_id = null
  42. ) {
  43. if (isset($content)) {
  44. $this->setContent($content);
  45. }
  46. if (isset($type)) {
  47. $this->setType($type);
  48. }
  49. if (isset($filename)) {
  50. $this->setFilename($filename);
  51. }
  52. if (isset($disposition)) {
  53. $this->setDisposition($disposition);
  54. }
  55. if (isset($content_id)) {
  56. $this->setContentID($content_id);
  57. }
  58. }
  59. /**
  60. * Add the content to a Attachment object
  61. *
  62. * @param string $content Base64 encoded content
  63. *
  64. * @throws \SendGrid\Mail\TypeException
  65. */
  66. public function setContent($content)
  67. {
  68. Assert::minLength($content, 'content', 1);
  69. if (!$this->isBase64($content)) {
  70. $this->content = base64_encode($content);
  71. } else {
  72. $this->content = $content;
  73. }
  74. }
  75. /**
  76. * Retrieve the content from a Attachment object
  77. *
  78. * @return string
  79. */
  80. public function getContent()
  81. {
  82. return $this->content;
  83. }
  84. /**
  85. * Add the mime type to a Attachment object
  86. *
  87. * @param string $type Mime type of the attachment
  88. *
  89. * @throws \SendGrid\Mail\TypeException
  90. */
  91. public function setType($type)
  92. {
  93. Assert::minLength($type, 'type', 1);
  94. $this->type = $type;
  95. }
  96. /**
  97. * Retrieve the mime type from a Attachment object
  98. *
  99. * @return string
  100. */
  101. public function getType()
  102. {
  103. return $this->type;
  104. }
  105. /**
  106. * Add the file name to a Attachment object
  107. *
  108. * @param string $filename File name of the attachment
  109. *
  110. * @throws \SendGrid\Mail\TypeException
  111. */
  112. public function setFilename($filename)
  113. {
  114. Assert::string($filename, 'filename');
  115. $this->filename = $filename;
  116. }
  117. /**
  118. * Retrieve the file name from a Attachment object
  119. *
  120. * @return string
  121. */
  122. public function getFilename()
  123. {
  124. return $this->filename;
  125. }
  126. /**
  127. * Add the disposition to a Attachment object
  128. *
  129. * @param string $disposition How the attachment should be displayed:
  130. * inline or attachment, default is attachment
  131. *
  132. * @throws \SendGrid\Mail\TypeException
  133. */
  134. public function setDisposition($disposition)
  135. {
  136. Assert::anyOf($disposition, 'disposition', ['inline', 'attachment']);
  137. $this->disposition = $disposition;
  138. }
  139. /**
  140. * Retrieve the disposition from a Attachment object
  141. *
  142. * @return string
  143. */
  144. public function getDisposition()
  145. {
  146. return $this->disposition;
  147. }
  148. /**
  149. * Add the content id to a Attachment object
  150. *
  151. * @param string $content_id Used when disposition is inline to display
  152. * the file within the body of the email
  153. * @throws \SendGrid\Mail\TypeException
  154. */
  155. public function setContentID($content_id)
  156. {
  157. Assert::string($content_id, 'content_id');
  158. $this->content_id = $content_id;
  159. }
  160. /**
  161. * Retrieve the content id from a Attachment object
  162. *
  163. * @return string
  164. */
  165. public function getContentID()
  166. {
  167. return $this->content_id;
  168. }
  169. /**
  170. * Verifies whether or not the provided string is a valid base64 string
  171. *
  172. * @param $string string The string that has to be checked
  173. * @return bool
  174. */
  175. private function isBase64($string)
  176. {
  177. $decoded_data = base64_decode($string, true);
  178. $encoded_data = base64_encode($decoded_data);
  179. if ($encoded_data != $string) {
  180. return false;
  181. }
  182. return true;
  183. }
  184. /**
  185. * Return an array representing a Attachment object for the Twilio SendGrid API
  186. *
  187. * @return null|array
  188. */
  189. public function jsonSerialize()
  190. {
  191. return array_filter(
  192. [
  193. 'content' => $this->getContent(),
  194. 'type' => $this->getType(),
  195. 'filename' => $this->getFilename(),
  196. 'disposition' => $this->getDisposition(),
  197. 'content_id' => $this->getContentID()
  198. ],
  199. function ($value) {
  200. return $value !== null;
  201. }
  202. ) ?: null;
  203. }
  204. }