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 / Http / Header / GenericHeader.php
blob80b0bb9ee09838090b2fea2c3242c6f3012222c1
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\Http\Header;
12 /**
13 * Content-Location Header
16 class GenericHeader implements HeaderInterface
18 /**
19 * @var string
21 protected $fieldName = null;
23 /**
24 * @var string
26 protected $fieldValue = null;
28 /**
29 * Factory to generate a header object from a string
31 * @static
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);
39 return $header;
42 /**
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]);
58 return $parts;
61 /**
62 * Constructor
64 * @param null|string $fieldName
65 * @param null|string $fieldValue
67 public function __construct($fieldName = null, $fieldValue = null)
69 if ($fieldName) {
70 $this->setFieldName($fieldName);
73 if ($fieldValue !== null) {
74 $this->setFieldValue($fieldValue);
78 /**
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 ]
98 * field-name = token
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;
109 return $this;
113 * Retrieve header field name
115 * @return string
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)) {
133 $fieldValue = '';
136 $this->fieldValue = $fieldValue;
137 return $this;
141 * Retrieve header field value
143 * @return string
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"
155 * @return string
157 public function toString()
159 return $this->getFieldName() . ': ' . $this->getFieldValue();