setFieldName($fieldName); } if ($fieldValue !== null) { $this->setFieldValue($fieldValue); } } /** * Set header field name * * @param string $fieldName * @return GenericHeader * @throws Exception\InvalidArgumentException If the name does not match with RFC 2616 format. */ public function setFieldName($fieldName) { if (!is_string($fieldName) || empty($fieldName)) { throw new Exception\InvalidArgumentException('Header name must be a string'); } // Pre-filter to normalize valid characters, change underscore to dash $fieldName = str_replace('_', '-', $fieldName); /* * Following RFC 2616 section 4.2 * * message-header = field-name ":" [ field-value ] * field-name = token * * @see http://tools.ietf.org/html/rfc2616#section-2.2 for token definition. */ if (!preg_match('/^[!#-\'*+\-\.0-9A-Z\^-z|~]+$/', $fieldName)) { throw new Exception\InvalidArgumentException( 'Header name must be a valid RFC 2616 (section 4.2) field-name.' ); } $this->fieldName = $fieldName; return $this; } /** * Retrieve header field name * * @return string */ public function getFieldName() { return $this->fieldName; } /** * Set header field value * * @param string $fieldValue * @return GenericHeader */ public function setFieldValue($fieldValue) { $fieldValue = (string) $fieldValue; if (preg_match('/^\s+$/', $fieldValue)) { $fieldValue = ''; } $this->fieldValue = $fieldValue; return $this; } /** * Retrieve header field value * * @return string */ public function getFieldValue() { return $this->fieldValue; } /** * Cast to string as a well formed HTTP header line * * Returns in form of "NAME: VALUE\r\n" * * @return string */ public function toString() { return $this->getFieldName() . ': ' . $this->getFieldValue(); } }