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
;
13 * Subject header class methods.
15 * @see https://tools.ietf.org/html/rfc2822 RFC 2822
16 * @see https://tools.ietf.org/html/rfc2047 RFC 2047
18 class Subject
implements UnstructuredInterface
23 protected $subject = '';
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) !== 'subject') {
39 throw new Exception\
InvalidArgumentException('Invalid header line for Subject string');
42 $header = new static();
43 $header->setSubject($value);
48 public function getFieldName()
53 public function getFieldValue($format = HeaderInterface
::FORMAT_RAW
)
55 if (HeaderInterface
::FORMAT_ENCODED
=== $format) {
56 return HeaderWrap
::wrap($this->subject
, $this);
59 return $this->subject
;
62 public function setEncoding($encoding)
64 $this->encoding
= $encoding;
68 public function getEncoding()
70 if (! $this->encoding
) {
71 $this->encoding
= Mime
::isPrintable($this->subject
) ?
'ASCII' : 'UTF-8';
74 return $this->encoding
;
77 public function setSubject($subject)
79 $subject = (string) $subject;
81 if (! HeaderWrap
::canBeEncoded($subject)) {
82 throw new Exception\
InvalidArgumentException(
83 'Subject value must be composed of printable US-ASCII or UTF-8 characters.'
87 $this->subject
= $subject;
88 $this->encoding
= null;
93 public function toString()
95 return 'Subject: ' . $this->getFieldValue(HeaderInterface
::FORMAT_ENCODED
);