1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
<?php
/* --------------------------------------------------------------
EmailAttachment.inc.php 2015-01-29 gm
Gambio GmbH
http://www.gambio.de
Copyright (c) 2015 Gambio GmbH
Released under the GNU General Public License (Version 2)
[http://www.gnu.org/licenses/gpl-2.0.html]
--------------------------------------------------------------
*/
MainFactory::load_class('EmailAttachmentInterface');
/**
* Class EmailAttachment
*
* Email attachment will serve as the path to the file that needs to be attached
* (located on the server).
*
* @category System
* @package Email
* @subpackage Entities
*/
class EmailAttachment implements EmailAttachmentInterface
{
/**
* E-Mail attachment path.
* @var string
*/
protected $path;
/**
* E-Mail attachment name.
* @var string
*/
protected $name;
/**
* Constructor
*
* Entity representing an email attachment.
*
* @param AttachmentPathInterface $path (optional) Attachment path.
* @param AttachmentNameInterface $name (optional) Attachment name.
*/
public function __construct(AttachmentPathInterface $path = null, AttachmentNameInterface $name = null)
{
$this->path = $this->_convertToRelativePath($path);
$this->name = (string)$name;
}
/**
* Sets the path of an email attachment.
*
* @param AttachmentPathInterface $path Attachment path.
*/
public function setPath(AttachmentPathInterface $path)
{
$this->path = $this->_convertToRelativePath($path);
}
/**
* Returns the path of an email attachment.
*
* @param bool $absolutePath (optional) Whether to return the absolute path or the relative one.
*
* @return AttachmentPathInterface Attachment path.
*/
public function getPath($absolutePath = true)
{
$path = ($absolutePath) ? DIR_FS_CATALOG . $this->path : $this->path;
return ($path !== DIR_FS_CATALOG) ? MainFactory::create('AttachmentPath', $path) : null;
}
/**
* Sets the name of an email attachment.
*
* @param AttachmentNameInterface $name Attachment name.
*/
public function setName(AttachmentNameInterface $name)
{
$this->name = (string)$name;
}
/**
* Returns the name of an email attachment.
*
* @return AttachmentNameInterface Attachment name.
*/
public function getName()
{
return MainFactory::create('AttachmentName', $this->name);
}
/**
* Convert a path to relative.
*
* Due to different server setups this process can be tedious and hard to foresee. The
* following method contains the conversion logic and must be used in any setter of the class.
*
* @param AttachmentPathInterface $path Attachment path.
*
* @return string Returns the converted path.
*/
protected function _convertToRelativePath(AttachmentPathInterface $path = null)
{
if(DIR_FS_CATALOG === '/' && substr((string)$path, 0, 1) === '/')
{
// Remove the initial slash.
$relativePath = substr((string)$path, 1);
}
else if(DIR_FS_CATALOG !== '/')
{
// Remove the entire DIR_FS_CATALOG from the path.
$relativePath = str_replace(DIR_FS_CATALOG, '', (string)$path);
}
else
{
// Path is already relative.
$relativePath = (string)$path;
}
return $relativePath;
}
}