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

Commit

Permalink
Merge remote-tracking branch 'Maks3w/use-headers-container-in-zend-mime'
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 15 changed files with 142 additions and 338 deletions.
59 changes: 13 additions & 46 deletions src/Header/AbstractAddressList.php
Expand Up @@ -53,23 +53,15 @@ abstract class AbstractAddressList implements HeaderInterface
protected $encoding = 'ASCII';

/**
* @var string lowercased field name
* @var string lower case field name
*/
protected static $type;

/**
* Parse string to create header object
*
* @param string $headerLine
* @throws Exception\InvalidArgumentException
* @return AbstractAddressList
*/
public static function fromString($headerLine)
{
$headerLine = iconv_mime_decode($headerLine, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, 'UTF-8');

$decodedLine = iconv_mime_decode($headerLine, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, 'UTF-8');
// split into name/value
list($fieldName, $fieldValue) = explode(': ', $headerLine, 2);
list($fieldName, $fieldValue) = explode(': ', $decodedLine, 2);

if (strtolower($fieldName) !== static::$type) {
throw new Exception\InvalidArgumentException(sprintf(
Expand All @@ -78,7 +70,9 @@ public static function fromString($headerLine)
));
}
$header = new static();

if ($decodedLine != $headerLine) {
$header->setEncoding('UTF-8');
}
// split value on ","
$fieldValue = str_replace(Headers::FOLDING, ' ', $fieldValue);
$values = explode(',', $fieldValue);
Expand All @@ -97,8 +91,6 @@ public static function fromString($headerLine)
}
if (empty($name)) {
$name = null;
} else {
$name = iconv_mime_decode($name, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, 'UTF-8');
}

if (isset($matches['namedEmail'])) {
Expand All @@ -112,26 +104,15 @@ public static function fromString($headerLine)
// populate address list
$addressList->add($email, $name);
}

return $header;
}

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

/**
* Get field value of this header
*
* @return string
*/
public function getFieldValue()
public function getFieldValue($format = HeaderInterface::FORMAT_RAW)
{
$emails = array();
$encoding = $this->getEncoding();
Expand All @@ -145,33 +126,24 @@ public function getFieldValue()
$name = sprintf('"%s"', $name);
}

if ('ASCII' !== $encoding) {
if ($format == HeaderInterface::FORMAT_ENCODED
&& 'ASCII' !== $encoding
) {
$name = HeaderWrap::mimeEncodeValue($name, $encoding);
}
$emails[] = sprintf('%s <%s>', $name, $email);
}
}
$string = implode(',' . Headers::FOLDING, $emails);
return $string;

return implode(',' . Headers::FOLDING, $emails);
}

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

/**
* Get header encoding
*
* @return string
*/
public function getEncoding()
{
return $this->encoding;
Expand Down Expand Up @@ -200,15 +172,10 @@ public function getAddressList()
return $this->addressList;
}

/**
* Serialize to string
*
* @return string
*/
public function toString()
{
$name = $this->getFieldName();
$value = $this->getFieldValue();
$value = $this->getFieldValue(HeaderInterface::FORMAT_ENCODED);
return (empty($value)) ? '' : sprintf('%s: %s', $name, $value);
}
}
41 changes: 4 additions & 37 deletions src/Header/ContentType.php
Expand Up @@ -49,13 +49,6 @@ class ContentType implements HeaderInterface
*/
protected $parameters = array();

/**
* Factory: create Content-Type header object from string
*
* @param string $headerLine
* @throws Exception\InvalidArgumentException
* @return ContentType
*/
public static function fromString($headerLine)
{
$headerLine = iconv_mime_decode($headerLine, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, 'UTF-8');
Expand Down Expand Up @@ -84,22 +77,12 @@ public static function fromString($headerLine)
return $header;
}

/**
* Get header name
*
* @return string
*/
public function getFieldName()
{
return 'Content-Type';
}

/**
* Get header value
*
* @return string
*/
public function getFieldValue()
public function getFieldValue($format = HeaderInterface::FORMAT_RAW)
{
$prepared = $this->type;
if (empty($this->parameters)) {
Expand All @@ -110,40 +93,24 @@ public function getFieldValue()
foreach ($this->parameters as $attribute => $value) {
$values[] = sprintf('%s="%s"', $attribute, $value);
}
$value = implode(';' . Headers::FOLDING, $values);
return $value;

return implode(';' . Headers::FOLDING, $values);
}

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

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

/**
* Serialize header to string
*
* @return string
*/
public function toString()
{
return 'Content-Type: ' . $this->getFieldValue();
return 'Content-Type: ' . $this->getFieldValue(HeaderInterface::FORMAT_RAW);
}

/**
Expand Down
37 changes: 2 additions & 35 deletions src/Header/Date.php
Expand Up @@ -43,13 +43,6 @@ class Date implements HeaderInterface
*/
protected $encoding = 'ASCII';

/**
* Factory: create header object from string
*
* @param string $headerLine
* @return Date
* @throws Exception\InvalidArgumentException
*/
public static function fromString($headerLine)
{
list($name, $value) = explode(': ', $headerLine, 2);
Expand All @@ -65,55 +58,29 @@ public static function fromString($headerLine)
return $header;
}

/**
* Get the header name
*
* @return string
*/
public function getFieldName()
{
return 'Date';
}

/**
* Get the header value
*
* @return string
*/
public function getFieldValue()
public function getFieldValue($format = HeaderInterface::FORMAT_RAW)
{
return $this->value;
}

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

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

/**
* Serialize header to string
*
* @return string
*/
public function toString()
{
return 'Date: ' . $this->getFieldValue();
return 'Date: ' . $this->getFieldValue(HeaderInterface::FORMAT_RAW);
}
}
51 changes: 12 additions & 39 deletions src/Header/GenericHeader.php
Expand Up @@ -28,7 +28,7 @@
* @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 GenericHeader implements HeaderInterface
class GenericHeader implements HeaderInterface, UnstructuredInterface
{
/**
* @var string
Expand All @@ -47,20 +47,17 @@ class GenericHeader implements HeaderInterface
*/
protected $encoding = 'ASCII';

/**
* Factory to generate a header object from a string
*
* @param string $headerLine
* @return GenericHeader
*/
public static function fromString($headerLine)
{
$headerLine = iconv_mime_decode($headerLine, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, 'UTF-8');
$parts = explode(': ', $headerLine, 2);
$decodedLine = iconv_mime_decode($headerLine, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, 'UTF-8');
$parts = explode(': ', $decodedLine, 2);
if (count($parts) != 2) {
throw new Exception\InvalidArgumentException('Header must match with the format "name: value"');
}
$header = new static($parts[0], $parts[1]);
if ($decodedLine != $headerLine) {
$header->setEncoding('UTF-8');
}
return $header;
}

Expand Down Expand Up @@ -108,11 +105,6 @@ public function setFieldName($fieldName)
return $this;
}

/**
* Retrieve header name
*
* @return string
*/
public function getFieldName()
{
return $this->fieldName;
Expand All @@ -136,49 +128,30 @@ public function setFieldValue($fieldValue)
return $this;
}

/**
* Retrieve header value
*
* @return string
*/
public function getFieldValue()
public function getFieldValue($format = HeaderInterface::FORMAT_RAW)
{
if (HeaderInterface::FORMAT_ENCODED) {
return HeaderWrap::wrap($this->fieldValue, $this);
}

return $this->fieldValue;
}

/**
* Set header encoding
*
* @param string $encoding
* @return GenericHeader
*/
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"
*
* @return string
*/
public function toString()
{
$name = $this->getFieldName();
$value = $this->getFieldValue();
$value = $this->getFieldValue(HeaderInterface::FORMAT_ENCODED);

return $name. ': ' . $value;
}
Expand Down

0 comments on commit ea8420a

Please sign in to comment.