PageRenderTime 42ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/zendframework/zendframework/library/Zend/Mail/Header/ContentType.php

https://bitbucket.org/pcelta/zf2
PHP | 180 lines | 151 code | 9 blank | 20 comment | 2 complexity | ed3f9cc99d22b9fd40e1311ff953dbfd MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. * @package Zend_Mail
  9. */
  10. namespace Zend\Mail\Header;
  11. use Zend\Mail\Headers;
  12. /**
  13. * @category Zend
  14. * @package Zend_Mail
  15. * @subpackage Header
  16. */
  17. class ContentType implements HeaderInterface
  18. {
  19. /**
  20. * @var string
  21. */
  22. protected $type;
  23. /**
  24. * @var array
  25. */
  26. protected $parameters = array();
  27. public static function fromString($headerLine)
  28. {
  29. $headerLine = iconv_mime_decode($headerLine, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, 'UTF-8');
  30. list($name, $value) = explode(': ', $headerLine, 2);
  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');
  34. }
  35. $value = str_replace(Headers::FOLDING, " ", $value);
  36. $values = preg_split('#\s*;\s*#', $value);
  37. $type = array_shift($values);
  38. $header = new static();
  39. $header->setType($type);
  40. if (count($values)) {
  41. foreach ($values as $keyValuePair) {
  42. list($key, $value) = explode('=', $keyValuePair, 2);
  43. $value = trim($value, "'\" \t\n\r\0\x0B");
  44. $header->addParameter($key, $value);
  45. }
  46. }
  47. return $header;
  48. }
  49. public function getFieldName()
  50. {
  51. return 'Content-Type';
  52. }
  53. public function getFieldValue($format = HeaderInterface::FORMAT_RAW)
  54. {
  55. $prepared = $this->type;
  56. if (empty($this->parameters)) {
  57. return $prepared;
  58. }
  59. $values = array($prepared);
  60. foreach ($this->parameters as $attribute => $value) {
  61. $values[] = sprintf('%s="%s"', $attribute, $value);
  62. }
  63. return implode(';' . Headers::FOLDING, $values);
  64. }
  65. public function setEncoding($encoding)
  66. {
  67. // This header must be always in US-ASCII
  68. return $this;
  69. }
  70. public function getEncoding()
  71. {
  72. return 'ASCII';
  73. }
  74. public function toString()
  75. {
  76. return 'Content-Type: ' . $this->getFieldValue();
  77. }
  78. /**
  79. * Set the content type
  80. *
  81. * @param string $type
  82. * @throws Exception\InvalidArgumentException
  83. * @return ContentType
  84. */
  85. public function setType($type)
  86. {
  87. if (!preg_match('/^[a-z-]+\/[a-z0-9.+-]+$/i', $type)) {
  88. throw new Exception\InvalidArgumentException(sprintf(
  89. '%s expects a value in the format "type/subtype"; received "%s"',
  90. __METHOD__,
  91. (string) $type
  92. ));
  93. }
  94. $this->type = $type;
  95. return $this;
  96. }
  97. /**
  98. * Retrieve the content type
  99. *
  100. * @return string
  101. */
  102. public function getType()
  103. {
  104. return $this->type;
  105. }
  106. /**
  107. * Add a parameter pair
  108. *
  109. * @param string $name
  110. * @param string $value
  111. * @return ContentType
  112. */
  113. public function addParameter($name, $value)
  114. {
  115. $name = strtolower($name);
  116. $this->parameters[$name] = (string) $value;
  117. return $this;
  118. }
  119. /**
  120. * Get all parameters
  121. *
  122. * @return array
  123. */
  124. public function getParameters()
  125. {
  126. return $this->parameters;
  127. }
  128. /**
  129. * Get a parameter by name
  130. *
  131. * @param string $name
  132. * @return null|string
  133. */
  134. public function getParameter($name)
  135. {
  136. $name = strtolower($name);
  137. if (isset($this->parameters[$name])) {
  138. return $this->parameters[$name];
  139. }
  140. return null;
  141. }
  142. /**
  143. * Remove a named parameter
  144. *
  145. * @param string $name
  146. * @return bool
  147. */
  148. public function removeParameter($name)
  149. {
  150. $name = strtolower($name);
  151. if (isset($this->parameters[$name])) {
  152. unset($this->parameters[$name]);
  153. return true;
  154. }
  155. return false;
  156. }
  157. }