3 * @see https://github.com/zendframework/zend-mail for the canonical source repository
4 * @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (https://www.zend.com)
5 * @license https://github.com/zendframework/zend-mail/blob/master/LICENSE.md New BSD License
8 namespace Zend\Mail\Header
;
10 use Zend\Mail\Headers
;
13 class ContentType
implements UnstructuredInterface
25 protected $encoding = 'ASCII';
30 protected $parameters = [];
32 public static function fromString($headerLine)
34 list($name, $value) = GenericHeader
::splitHeaderLine($headerLine);
35 $value = HeaderWrap
::mimeDecodeValue($value);
37 // check to ensure proper header type for this factory
38 if (strtolower($name) !== 'content-type') {
39 throw new Exception\
InvalidArgumentException('Invalid header line for Content-Type string');
42 $value = str_replace(Headers
::FOLDING
, ' ', $value);
43 $parts = explode(';', $value, 2);
45 $header = new static();
46 $header->setType($parts[0]);
48 if (isset($parts[1])) {
49 $values = ListParser
::parse(trim($parts[1]), [';', '=']);
50 $length = count($values);
52 for ($i = 0; $i < $length; $i +
= 2) {
53 $value = $values[$i +
1];
54 $value = trim($value, "'\" \t\n\r\0\x0B");
55 $header->addParameter($values[$i], $value);
62 public function getFieldName()
64 return 'Content-Type';
67 public function getFieldValue($format = HeaderInterface
::FORMAT_RAW
)
69 $prepared = $this->type
;
70 if (empty($this->parameters
)) {
74 $values = [$prepared];
75 foreach ($this->parameters
as $attribute => $value) {
76 if (HeaderInterface
::FORMAT_ENCODED
=== $format && ! Mime
::isPrintable($value)) {
77 $this->encoding
= 'UTF-8';
78 $value = HeaderWrap
::wrap($value, $this);
79 $this->encoding
= 'ASCII';
82 $values[] = sprintf('%s="%s"', $attribute, $value);
85 return implode(';' . Headers
::FOLDING
, $values);
88 public function setEncoding($encoding)
90 $this->encoding
= $encoding;
94 public function getEncoding()
96 return $this->encoding
;
99 public function toString()
101 return 'Content-Type: ' . $this->getFieldValue(HeaderInterface
::FORMAT_ENCODED
);
105 * Set the content type
107 * @param string $type
108 * @throws Exception\InvalidArgumentException
109 * @return ContentType
111 public function setType($type)
113 if (! preg_match('/^[a-z-]+\/[a-z0-9.+-]+$/i', $type)) {
114 throw new Exception\
InvalidArgumentException(sprintf(
115 '%s expects a value in the format "type/subtype"; received "%s"',
125 * Retrieve the content type
129 public function getType()
135 * Add a parameter pair
137 * @param string $name
138 * @param string $value
139 * @return ContentType
140 * @throws Exception\InvalidArgumentException for parameter names that do not follow RFC 2822
141 * @throws Exception\InvalidArgumentException for parameter values that do not follow RFC 2822
143 public function addParameter($name, $value)
145 $name = strtolower($name);
146 $value = (string) $value;
148 if (! HeaderValue
::isValid($name)) {
149 throw new Exception\
InvalidArgumentException('Invalid content-type parameter name detected');
151 if (! HeaderWrap
::canBeEncoded($value)) {
152 throw new Exception\
InvalidArgumentException(
153 'Parameter value must be composed of printable US-ASCII or UTF-8 characters.'
157 $this->parameters
[$name] = $value;
166 public function getParameters()
168 return $this->parameters
;
172 * Get a parameter by name
174 * @param string $name
175 * @return null|string
177 public function getParameter($name)
179 $name = strtolower($name);
180 if (isset($this->parameters
[$name])) {
181 return $this->parameters
[$name];
187 * Remove a named parameter
189 * @param string $name
192 public function removeParameter($name)
194 $name = strtolower($name);
195 if (isset($this->parameters
[$name])) {
196 unset($this->parameters
[$name]);