Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/mail-refactor' of https://github.com/Maks3w/zf2
Browse files Browse the repository at this point in the history
…into feature/mail-improvements
  • Loading branch information
Show file tree
Hide file tree
Showing 20 changed files with 360 additions and 397 deletions.
7 changes: 4 additions & 3 deletions src/Header/AbstractAddressList.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
namespace Zend\Mail\Header;

use Zend\Mail\AddressList;
use Zend\Mail\Headers;

/**
* Base class for headers composing address lists (to, from, cc, bcc, reply-to)
Expand Down Expand Up @@ -79,7 +80,7 @@ public static function fromString($headerLine)
$header = new static();

// split value on ","
$fieldValue = str_replace("\r\n ", " ", $fieldValue);
$fieldValue = str_replace(Headers::FOLDING, ' ', $fieldValue);
$values = explode(',', $fieldValue);
array_walk($values, 'trim');

Expand Down Expand Up @@ -150,7 +151,7 @@ public function getFieldValue()
$emails[] = sprintf('%s <%s>', $name, $email);
}
}
$string = implode(",\r\n ", $emails);
$string = implode(',' . Headers::FOLDING, $emails);
return $string;
}

Expand Down Expand Up @@ -208,6 +209,6 @@ public function toString()
{
$name = $this->getFieldName();
$value = $this->getFieldValue();
return sprintf("%s: %s\r\n", $name, $value);
return (empty($value)) ? '' : sprintf('%s: %s', $name, $value);
}
}
8 changes: 5 additions & 3 deletions src/Header/ContentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

namespace Zend\Mail\Header;

use Zend\Mail\Headers;

/**
* @category Zend
* @package Zend_Mail
Expand Down Expand Up @@ -64,7 +66,7 @@ public static function fromString($headerLine)
throw new Exception\InvalidArgumentException('Invalid header line for Content-Type string');
}

$value = str_replace("\r\n ", " ", $value);
$value = str_replace(Headers::FOLDING, " ", $value);
$values = preg_split('#\s*;\s*#', $value);
$type = array_shift($values);

Expand Down Expand Up @@ -108,7 +110,7 @@ public function getFieldValue()
foreach ($this->parameters as $attribute => $value) {
$values[] = sprintf('%s="%s"', $attribute, $value);
}
$value = implode(";\r\n ", $values);
$value = implode(';' . Headers::FOLDING, $values);
return $value;
}

Expand Down Expand Up @@ -141,7 +143,7 @@ public function getEncoding()
*/
public function toString()
{
return 'Content-Type: ' . $this->getFieldValue() . "\r\n";
return 'Content-Type: ' . $this->getFieldValue();
}

/**
Expand Down
25 changes: 15 additions & 10 deletions src/Header/GenericHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ class GenericHeader implements HeaderInterface
public static function fromString($headerLine)
{
$headerLine = iconv_mime_decode($headerLine, ICONV_MIME_DECODE_CONTINUE_ON_ERROR);
list($fieldName, $fieldValue) = explode(': ', $headerLine, 2);
$header = new static($fieldName, $fieldValue);
$parts = explode(': ', $headerLine, 2);
if (count($parts) != 2) {
throw new Exception\InvalidArgumentException('Header must match with the format "name: value"');
}
$header = new static($parts[0], $parts[1]);
return $header;
}

Expand All @@ -79,7 +82,7 @@ public function __construct($fieldName = null, $fieldValue = null)
}

/**
* Set header field name
* Set header name
*
* @param string $fieldName
* @throws Exception\InvalidArgumentException
Expand All @@ -96,15 +99,17 @@ public function setFieldName($fieldName)

// Validate what we have
if (!preg_match('/^[a-z][a-z0-9-]*$/i', $fieldName)) {
throw new Exception\InvalidArgumentException('Header name must start with a letter, and consist of only letters, numbers, and dashes');
throw new Exception\InvalidArgumentException(
'Header name must start with a letter, and consist of only letters, numbers and dashes'
);
}

$this->fieldName = $fieldName;
return $this;
}

/**
* Retrieve header field name
* Retrieve header name
*
* @return string
*/
Expand All @@ -114,7 +119,7 @@ public function getFieldName()
}

/**
* Set header field value
* Set header value
*
* @param string $fieldValue
* @return GenericHeader
Expand All @@ -132,7 +137,7 @@ public function setFieldValue($fieldValue)
}

/**
* Retrieve header field value
* Retrieve header value
*
* @return string
*/
Expand Down Expand Up @@ -164,9 +169,9 @@ public function getEncoding()
}

/**
* Cast to string as a well formed HTTP header line
* Cast to string
*
* Returns in form of "NAME: VALUE\r\n"
* Returns in form of "NAME: VALUE"
*
* @return string
*/
Expand All @@ -175,6 +180,6 @@ public function toString()
$name = $this->getFieldName();
$value = $this->getFieldValue();

return $name. ': ' . $value . "\r\n";
return $name. ': ' . $value;
}
}
154 changes: 13 additions & 141 deletions src/Header/GenericMultiHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,41 +22,30 @@
namespace Zend\Mail\Header;

/**
* Generic class for Headers with multiple occurs in the same message
*
* @category Zend
* @package Zend_Mail
* @subpackage Header
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class GenericMultiHeader implements MultipleHeadersInterface
class GenericMultiHeader extends GenericHeader implements MultipleHeadersInterface
{
/**
* @var string
*/
protected $fieldName = null;

/**
* @var string
*/
protected $fieldValue = null;

/**
* Header encoding
*
* @var string
*/
protected $encoding = 'ASCII';

/**
* Unserialize from a string
*
* @param string $headerLine
* @return GenericMultiHeader
* @return GenericMultiHeader|GenericMultiHeader[]
*/
public static function fromString($headerLine)
{
$headerLine = iconv_mime_decode($headerLine, ICONV_MIME_DECODE_CONTINUE_ON_ERROR);
list($fieldName, $fieldValue) = explode(': ', $headerLine, 2);
$parts = explode(': ', $headerLine, 2);
if (count($parts) != 2) {
throw new Exception\InvalidArgumentException('Header must match with the format "name: value"');
}
list($fieldName, $fieldValue) = $parts;

if (strpos($fieldValue, ',')) {
$headers = array();
Expand All @@ -68,125 +57,6 @@ public static function fromString($headerLine)
$header = new static($fieldName, $fieldValue);
return $header;
}


}

/**
* Constructor
*
* @param string $fieldName Optional
* @param string $fieldValue Optional
*/
public function __construct($fieldName = null, $fieldValue = null)
{
if ($fieldName) {
$this->setFieldName($fieldName);
}

if ($fieldValue) {
$this->setFieldValue($fieldValue);
}
}

/**
* Set header name
*
* @param string $fieldName
* @throws Exception\InvalidArgumentException
* @return GenericHeader
*/
public function setFieldName($fieldName)
{
if (!is_string($fieldName) || empty($fieldName)) {
throw new Exception\InvalidArgumentException('Header name must be a string');
}

// Pre-filter to normalize valid characters, change underscore to dash
$fieldName = str_replace(' ', '-', ucwords(str_replace(array('_', '-'), ' ', $fieldName)));

// Validate what we have
if (!preg_match('/^[a-z][a-z0-9-]*$/i', $fieldName)) {
throw new Exception\InvalidArgumentException('Header name must start with a letter, and consist of only letters, numbers, and dashes');
}

$this->fieldName = $fieldName;
return $this;
}

/**
* Retrieve header name
*
* @return string
*/
public function getFieldName()
{
return $this->fieldName;
}

/**
* Set header value
*
* @param string|array $fieldValue
* @return GenericHeader
*/
public function setFieldValue($fieldValue)
{
$fieldValue = (string) $fieldValue;

if (empty($fieldValue) || preg_match('/^\s+$/', $fieldValue)) {
$fieldValue = '';
}

$this->fieldValue = $fieldValue;
return $this;
}

/**
* Retrieve header value
*
* @return string
*/
public function getFieldValue()
{
return $this->fieldValue;
}

/**
* Set header encoding
*
* @param string $encoding
* @return GenericMultiHeader
*/
public function setEncoding($encoding)
{
$this->encoding = $encoding;
return $this;
}

/**
* Get header encoding
*
* @return string
*/
public function getEncoding()
{
return $this->encoding;
}

/**
* Cast to string
*
* Returns in form of "NAME: VALUE\r\n"
*
* @return string
*/
public function toString()
{
$name = $this->getFieldName();
$value = $this->getFieldValue();

return $name. ': ' . $value . "\r\n";
}

/**
Expand All @@ -202,10 +72,12 @@ public function toStringMultipleHeaders(array $headers)
$values = array($this->getFieldValue());
foreach ($headers as $header) {
if (!$header instanceof static) {
throw new Exception\InvalidArgumentException('This method toStringMultipleHeaders was expecting an array of headers of the same type');
throw new Exception\InvalidArgumentException(
'This method toStringMultipleHeaders was expecting an array of headers of the same type'
);
}
$values[] = $header->getFieldValue();
}
return $name. ': ' . implode(',', $values) . "\r\n";
return $name. ': ' . implode(',', $values);
}
}
48 changes: 48 additions & 0 deletions src/Header/HeaderLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Mail
*/

namespace Zend\Mail\Header;

use Zend\Loader\PluginClassLoader;

/**
* Plugin Class Loader implementation for HTTP headers
*
* @category Zend
* @package Zend_Mail
* @subpackage Header
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class HeaderLoader extends PluginClassLoader
{
/**
* @var array Pre-aliased Header plugins
*/
protected $plugins = array(
'bcc' => 'Zend\Mail\Header\Bcc',
'cc' => 'Zend\Mail\Header\Cc',
'contenttype' => 'Zend\Mail\Header\ContentType',
'content_type' => 'Zend\Mail\Header\ContentType',
'content-type' => 'Zend\Mail\Header\ContentType',
'date' => 'Zend\Mail\Header\Date',
'from' => 'Zend\Mail\Header\From',
'mimeversion' => 'Zend\Mail\Header\MimeVersion',
'mime_version' => 'Zend\Mail\Header\MimeVersion',
'mime-version' => 'Zend\Mail\Header\MimeVersion',
'received' => 'Zend\Mail\Header\Received',
'replyto' => 'Zend\Mail\Header\ReplyTo',
'reply_to' => 'Zend\Mail\Header\ReplyTo',
'reply-to' => 'Zend\Mail\Header\ReplyTo',
'sender' => 'Zend\Mail\Header\Sender',
'subject' => 'Zend\Mail\Header\Subject',
'to' => 'Zend\Mail\Header\To',
);
}
Loading

0 comments on commit 6149722

Please sign in to comment.