torrentpier-lts/library/Zend/Mail/Header/GenericHeader.php

176 lines
4.6 KiB
PHP
Raw Normal View History

<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
2023-04-01 09:03:34 +03:00
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Mail\Header;
2023-04-01 09:03:34 +03:00
use Zend\Mime\Mime;
class GenericHeader implements HeaderInterface, UnstructuredInterface
{
/**
* @var string
*/
protected $fieldName = null;
/**
* @var string
*/
protected $fieldValue = null;
/**
* Header encoding
*
2023-04-01 09:03:34 +03:00
* @var null|string
*/
2023-04-01 09:03:34 +03:00
protected $encoding;
public static function fromString($headerLine)
{
2023-04-01 09:03:34 +03:00
list($name, $value) = self::splitHeaderLine($headerLine);
$value = HeaderWrap::mimeDecodeValue($value);
$header = new static($name, $value);
2023-04-01 09:03:34 +03:00
return $header;
}
/**
* Splits the header line in `name` and `value` parts.
*
* @param string $headerLine
* @return string[] `name` in the first index and `value` in the second.
* @throws Exception\InvalidArgumentException If header does not match with the format ``name:value``
*/
public static function splitHeaderLine($headerLine)
{
$parts = explode(':', $headerLine, 2);
if (count($parts) !== 2) {
throw new Exception\InvalidArgumentException('Header must match with the format "name:value"');
}
2023-04-01 09:03:34 +03:00
if (! HeaderName::isValid($parts[0])) {
throw new Exception\InvalidArgumentException('Invalid header name detected');
}
if (! HeaderValue::isValid($parts[1])) {
throw new Exception\InvalidArgumentException('Invalid header value detected');
}
$parts[0] = $parts[0];
$parts[1] = ltrim($parts[1]);
return $parts;
}
/**
* 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
* @return GenericHeader
2023-04-01 09:03:34 +03:00
* @throws Exception\InvalidArgumentException;
*/
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)));
2023-04-01 09:03:34 +03:00
if (! HeaderName::isValid($fieldName)) {
throw new Exception\InvalidArgumentException(
'Header name must be composed of printable US-ASCII characters, except colon.'
);
}
$this->fieldName = $fieldName;
return $this;
}
public function getFieldName()
{
return $this->fieldName;
}
/**
* Set header value
*
* @param string $fieldValue
* @return GenericHeader
2023-04-01 09:03:34 +03:00
* @throws Exception\InvalidArgumentException;
*/
public function setFieldValue($fieldValue)
{
$fieldValue = (string) $fieldValue;
2023-04-01 09:03:34 +03:00
if (! HeaderWrap::canBeEncoded($fieldValue)) {
throw new Exception\InvalidArgumentException(
'Header value must be composed of printable US-ASCII characters and valid folding sequences.'
);
}
$this->fieldValue = $fieldValue;
2023-04-01 09:03:34 +03:00
$this->encoding = null;
return $this;
}
public function getFieldValue($format = HeaderInterface::FORMAT_RAW)
{
if (HeaderInterface::FORMAT_ENCODED === $format) {
return HeaderWrap::wrap($this->fieldValue, $this);
}
return $this->fieldValue;
}
public function setEncoding($encoding)
{
$this->encoding = $encoding;
return $this;
}
public function getEncoding()
{
2023-04-01 09:03:34 +03:00
if (! $this->encoding) {
$this->encoding = Mime::isPrintable($this->fieldValue) ? 'ASCII' : 'UTF-8';
}
return $this->encoding;
}
public function toString()
{
2023-04-01 09:03:34 +03:00
$name = $this->getFieldName();
if (empty($name)) {
throw new Exception\RuntimeException('Header name is not set, use setFieldName()');
}
$value = $this->getFieldValue(HeaderInterface::FORMAT_ENCODED);
return $name . ': ' . $value;
}
}