Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Mail / Header / ContentType.php
blobab8a373ecf0a179f7e972244d6d339991814f144
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\Mail\Header;
12 use Zend\Mail\Headers;
14 class ContentType implements HeaderInterface
16 /**
17 * @var string
19 protected $type;
21 /**
22 * @var array
24 protected $parameters = array();
26 public static function fromString($headerLine)
28 $headerLine = iconv_mime_decode($headerLine, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, 'UTF-8');
29 list($name, $value) = GenericHeader::splitHeaderLine($headerLine);
31 // check to ensure proper header type for this factory
32 if (strtolower($name) !== 'content-type') {
33 throw new Exception\InvalidArgumentException('Invalid header line for Content-Type string');
36 $value = str_replace(Headers::FOLDING, " ", $value);
37 $values = preg_split('#\s*;\s*#', $value);
38 $type = array_shift($values);
40 $header = new static();
41 $header->setType($type);
43 if (count($values)) {
44 foreach ($values as $keyValuePair) {
45 list($key, $value) = explode('=', $keyValuePair, 2);
46 $value = trim($value, "'\" \t\n\r\0\x0B");
47 $header->addParameter($key, $value);
51 return $header;
54 public function getFieldName()
56 return 'Content-Type';
59 public function getFieldValue($format = HeaderInterface::FORMAT_RAW)
61 $prepared = $this->type;
62 if (empty($this->parameters)) {
63 return $prepared;
66 $values = array($prepared);
67 foreach ($this->parameters as $attribute => $value) {
68 $values[] = sprintf('%s="%s"', $attribute, $value);
71 return implode(';' . Headers::FOLDING, $values);
74 public function setEncoding($encoding)
76 // This header must be always in US-ASCII
77 return $this;
80 public function getEncoding()
82 return 'ASCII';
85 public function toString()
87 return 'Content-Type: ' . $this->getFieldValue();
90 /**
91 * Set the content type
93 * @param string $type
94 * @throws Exception\InvalidArgumentException
95 * @return ContentType
97 public function setType($type)
99 if (!preg_match('/^[a-z-]+\/[a-z0-9.+-]+$/i', $type)) {
100 throw new Exception\InvalidArgumentException(sprintf(
101 '%s expects a value in the format "type/subtype"; received "%s"',
102 __METHOD__,
103 (string) $type
106 $this->type = $type;
107 return $this;
111 * Retrieve the content type
113 * @return string
115 public function getType()
117 return $this->type;
121 * Add a parameter pair
123 * @param string $name
124 * @param string $value
125 * @return ContentType
127 public function addParameter($name, $value)
129 $name = strtolower($name);
130 $this->parameters[$name] = (string) $value;
131 return $this;
135 * Get all parameters
137 * @return array
139 public function getParameters()
141 return $this->parameters;
145 * Get a parameter by name
147 * @param string $name
148 * @return null|string
150 public function getParameter($name)
152 $name = strtolower($name);
153 if (isset($this->parameters[$name])) {
154 return $this->parameters[$name];
156 return null;
160 * Remove a named parameter
162 * @param string $name
163 * @return bool
165 public function removeParameter($name)
167 $name = strtolower($name);
168 if (isset($this->parameters[$name])) {
169 unset($this->parameters[$name]);
170 return true;
172 return false;