Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 8 |
CRAP | |
12.50% |
4 / 32 |
GenericHeader | |
0.00% |
0 / 1 |
|
0.00% |
0 / 8 |
165.73 | |
12.50% |
4 / 32 |
fromString($headerLine) | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 3 |
|||
splitHeaderLine($headerLine) | |
0.00% |
0 / 1 |
2.03 | |
80.00% |
4 / 5 |
|||
__construct($fieldName = null, $fieldValue = null) | |
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 7 |
|||
setFieldName($fieldName) | |
0.00% |
0 / 1 |
20 | |
0.00% |
0 / 8 |
|||
getFieldName() | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
setFieldValue($fieldValue) | |
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 6 |
|||
getFieldValue() | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
toString() | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
<?php | |
/** | |
* Zend Framework (http://framework.zend.com/) | |
* | |
* @link http://github.com/zendframework/zf2 for the canonical source repository | |
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) | |
* @license http://framework.zend.com/license/new-bsd New BSD License | |
*/ | |
namespace Zend\Http\Header; | |
/** | |
* Content-Location Header | |
* | |
*/ | |
class GenericHeader implements HeaderInterface | |
{ | |
/** | |
* @var string | |
*/ | |
protected $fieldName = null; | |
/** | |
* @var string | |
*/ | |
protected $fieldValue = null; | |
/** | |
* Factory to generate a header object from a string | |
* | |
* @static | |
* @param string $headerLine | |
* @return GenericHeader | |
*/ | |
public static function fromString($headerLine) | |
{ | |
list($fieldName, $fieldValue) = GenericHeader::splitHeaderLine($headerLine); | |
$header = new static($fieldName, $fieldValue); | |
return $header; | |
} | |
/** | |
* Splits the header line in `name` and `value` parts. | |
* | |
* @param string $headerLine | |
* @return string[] `name` in the first index and `value` in the second. | |
* @throws Exception\InvalidArgumentException If header does not match with the format ``name:value`` | |
*/ | |
public static function splitHeaderLine($headerLine) | |
{ | |
$parts = explode(':', $headerLine, 2); | |
if (count($parts) !== 2) { | |
throw new Exception\InvalidArgumentException('Header must match with the format "name:value"'); | |
} | |
$parts[1] = ltrim($parts[1]); | |
return $parts; | |
} | |
/** | |
* Constructor | |
* | |
* @param null|string $fieldName | |
* @param null|string $fieldValue | |
*/ | |
public function __construct($fieldName = null, $fieldValue = null) | |
{ | |
if ($fieldName) { | |
$this->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(); | |
} | |
} |