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
10 namespace Zend\Http\Header
;
13 * Content-Location Header
16 class GenericHeader
implements HeaderInterface
21 protected $fieldName = null;
26 protected $fieldValue = null;
29 * Factory to generate a header object from a string
32 * @param string $headerLine
33 * @return GenericHeader
35 public static function fromString($headerLine)
37 list($fieldName, $fieldValue) = GenericHeader
::splitHeaderLine($headerLine);
38 $header = new static($fieldName, $fieldValue);
43 * Splits the header line in `name` and `value` parts.
45 * @param string $headerLine
46 * @return string[] `name` in the first index and `value` in the second.
47 * @throws Exception\InvalidArgumentException If header does not match with the format ``name:value``
49 public static function splitHeaderLine($headerLine)
51 $parts = explode(':', $headerLine, 2);
52 if (count($parts) !== 2) {
53 throw new Exception\
InvalidArgumentException('Header must match with the format "name:value"');
56 $parts[1] = ltrim($parts[1]);
64 * @param null|string $fieldName
65 * @param null|string $fieldValue
67 public function __construct($fieldName = null, $fieldValue = null)
70 $this->setFieldName($fieldName);
73 if ($fieldValue !== null) {
74 $this->setFieldValue($fieldValue);
79 * Set header field name
81 * @param string $fieldName
82 * @return GenericHeader
83 * @throws Exception\InvalidArgumentException If the name does not match with RFC 2616 format.
85 public function setFieldName($fieldName)
87 if (!is_string($fieldName) ||
empty($fieldName)) {
88 throw new Exception\
InvalidArgumentException('Header name must be a string');
91 // Pre-filter to normalize valid characters, change underscore to dash
92 $fieldName = str_replace('_', '-', $fieldName);
95 * Following RFC 2616 section 4.2
97 * message-header = field-name ":" [ field-value ]
100 * @see http://tools.ietf.org/html/rfc2616#section-2.2 for token definition.
102 if (!preg_match('/^[!#-\'*+\-\.0-9A-Z\^-z|~]+$/', $fieldName)) {
103 throw new Exception\
InvalidArgumentException(
104 'Header name must be a valid RFC 2616 (section 4.2) field-name.'
108 $this->fieldName
= $fieldName;
113 * Retrieve header field name
117 public function getFieldName()
119 return $this->fieldName
;
123 * Set header field value
125 * @param string $fieldValue
126 * @return GenericHeader
128 public function setFieldValue($fieldValue)
130 $fieldValue = (string) $fieldValue;
132 if (preg_match('/^\s+$/', $fieldValue)) {
136 $this->fieldValue
= $fieldValue;
141 * Retrieve header field value
145 public function getFieldValue()
147 return $this->fieldValue
;
151 * Cast to string as a well formed HTTP header line
153 * Returns in form of "NAME: VALUE\r\n"
157 public function toString()
159 return $this->getFieldName() . ': ' . $this->getFieldValue();