This repository has been archived by the owner on Jan 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request zendframework/zendframework#5140 branch 'hotfix/5140'
- Loading branch information
Showing
4 changed files
with
219 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace Zend\Mail\Header; | ||
|
||
use Zend\Mail\Headers; | ||
|
||
class ContentTransferEncoding implements HeaderInterface | ||
{ | ||
/** | ||
* Allowed Content-Transfer-Encoding parameters specified by RFC 1521 | ||
* (reduced set) | ||
* @var array | ||
*/ | ||
protected static $allowedTransferEncodings = array( | ||
'7bit', | ||
'8bit', | ||
'quoted-printable', | ||
'base64', | ||
/* | ||
* not implemented: | ||
* 'binary', | ||
* x-token: 'X-' | ||
*/ | ||
); | ||
|
||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $transferEncoding; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $parameters = array(); | ||
|
||
public static function fromString($headerLine) | ||
{ | ||
$headerLine = iconv_mime_decode($headerLine, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, 'UTF-8'); | ||
list($name, $value) = GenericHeader::splitHeaderLine($headerLine); | ||
|
||
// check to ensure proper header type for this factory | ||
if (strtolower($name) !== 'content-transfer-encoding') { | ||
throw new Exception\InvalidArgumentException('Invalid header line for Content-Transfer-Encoding string'); | ||
} | ||
|
||
$header = new static(); | ||
$header->setTransferEncoding($value); | ||
|
||
return $header; | ||
} | ||
|
||
public function getFieldName() | ||
{ | ||
return 'Content-Transfer-Encoding'; | ||
} | ||
|
||
public function getFieldValue($format = HeaderInterface::FORMAT_RAW) | ||
{ | ||
return $this->transferEncoding; | ||
} | ||
|
||
public function setEncoding($encoding) | ||
{ | ||
// Header must be always in US-ASCII | ||
return $this; | ||
} | ||
|
||
public function getEncoding() | ||
{ | ||
return 'ASCII'; | ||
} | ||
|
||
public function toString() | ||
{ | ||
return 'Content-Transfer-Encoding: ' . $this->getFieldValue(); | ||
} | ||
|
||
/** | ||
* Set the content transfer encoding | ||
* | ||
* @param string $transferEncoding | ||
* @throws Exception\InvalidArgumentException | ||
* @return self | ||
*/ | ||
public function setTransferEncoding($transferEncoding) | ||
{ | ||
if (!in_array($transferEncoding, self::$allowedTransferEncodings)) { | ||
throw new Exception\InvalidArgumentException(sprintf( | ||
'%s expects one of "'. implode(', ', self::$allowedTransferEncodings) . '"; received "%s"', | ||
__METHOD__, | ||
(string) $transferEncoding | ||
)); | ||
} | ||
$this->transferEncoding = $transferEncoding; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Retrieve the content transfer encoding | ||
* | ||
* @return string | ||
*/ | ||
public function getTransferEncoding() | ||
{ | ||
return $this->transferEncoding; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace ZendTest\Mail\Header; | ||
|
||
use Zend\Mail\Header\ContentTransferEncoding; | ||
|
||
/** | ||
* @group Zend_Mail | ||
*/ | ||
class ContentTransferEncodingTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
|
||
public function testContentTransferEncodingFromStringCreatesValidContentTransferEncodingHeader() | ||
{ | ||
$contentTransferEncodingHeader = ContentTransferEncoding::fromString('Content-Transfer-Encoding: 7bit'); | ||
$this->assertInstanceOf('Zend\Mail\Header\HeaderInterface', $contentTransferEncodingHeader); | ||
$this->assertInstanceOf('Zend\Mail\Header\ContentTransferEncoding', $contentTransferEncodingHeader); | ||
} | ||
|
||
public function testContentTransferEncodingFromStringCreateExcaption() | ||
{ | ||
$this->setExpectedException('Zend\Mail\Header\Exception\InvalidArgumentException'); | ||
$contentTransferEncodingHeader = ContentTransferEncoding::fromString('Content-Transfer-Encoding: 9bit'); | ||
} | ||
|
||
public function testContentTransferEncodingGetFieldNameReturnsHeaderName() | ||
{ | ||
$contentTransferEncodingHeader = new ContentTransferEncoding(); | ||
$this->assertEquals('Content-Transfer-Encoding', $contentTransferEncodingHeader->getFieldName()); | ||
} | ||
|
||
public function testContentTransferEncodingGetFieldValueReturnsProperValue() | ||
{ | ||
$contentTransferEncodingHeader = new ContentTransferEncoding(); | ||
$contentTransferEncodingHeader->setTransferEncoding('7bit'); | ||
$this->assertEquals('7bit', $contentTransferEncodingHeader->getFieldValue()); | ||
} | ||
|
||
public function testContentTransferEncodingToStringReturnsHeaderFormattedString() | ||
{ | ||
$contentTransferEncodingHeader = new ContentTransferEncoding(); | ||
$contentTransferEncodingHeader->setTransferEncoding('8bit'); | ||
$this->assertEquals("Content-Transfer-Encoding: 8bit", $contentTransferEncodingHeader->toString()); | ||
} | ||
|
||
public function testProvidingParametersIntroducesHeaderFolding() | ||
{ | ||
$header = new ContentTransferEncoding(); | ||
$header->setTransferEncoding('quoted-printable'); | ||
$string = $header->toString(); | ||
|
||
$this->assertContains("Content-Transfer-Encoding: quoted-printable", $string); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters