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
;
12 use Zend\Uri\Exception
as UriException
;
14 use Zend\Uri\UriFactory
;
15 use Zend\Uri\UriInterface
;
19 * Abstract Location Header
20 * Supports headers that have URI as value
21 * @see Zend\Http\Header\Location
22 * @see Zend\Http\Header\ContentLocation
23 * @see Zend\Http\Header\Referer
25 * Note for 'Location' header:
26 * While RFC 1945 requires an absolute URI, most of the browsers also support relative URI
27 * This class allows relative URIs, and let user retrieve URI instance if strict validation needed
29 abstract class AbstractLocation
implements HeaderInterface
36 protected $uri = null;
39 * Create location-based header from string
41 * @param string $headerLine
42 * @return AbstractLocation
43 * @throws Exception\InvalidArgumentException
45 public static function fromString($headerLine)
47 $locationHeader = new static();
49 // ZF-5520 - IIS bug, no space after colon
50 list($name, $uri) = GenericHeader
::splitHeaderLine($headerLine);
52 // check to ensure proper header type for this factory
53 if (strtolower($name) !== strtolower($locationHeader->getFieldName())) {
54 throw new Exception\
InvalidArgumentException(
55 'Invalid header line for "' . $locationHeader->getFieldName() . '" header string'
59 $locationHeader->setUri(trim($uri));
61 return $locationHeader;
65 * Set the URI/URL for this header, this can be a string or an instance of Zend\Uri\Http
67 * @param string|UriInterface $uri
68 * @return AbstractLocation
69 * @throws Exception\InvalidArgumentException
71 public function setUri($uri)
73 if (is_string($uri)) {
75 $uri = UriFactory
::factory($uri);
76 } catch (UriException\InvalidUriPartException
$e) {
77 throw new Exception\
InvalidArgumentException(
78 sprintf('Invalid URI passed as string (%s)', (string) $uri),
83 } elseif (!($uri instanceof UriInterface
)) {
84 throw new Exception\
InvalidArgumentException('URI must be an instance of Zend\Uri\Http or a string');
92 * Return the URI for this header
96 public function getUri()
98 if ($this->uri
instanceof UriInterface
) {
99 return $this->uri
->toString();
105 * Return the URI for this header as an instance of Zend\Uri\Http
107 * @return UriInterface
109 public function uri()
111 if ($this->uri
=== null ||
is_string($this->uri
)) {
112 $this->uri
= UriFactory
::factory($this->uri
);
118 * Get header value as URI string
122 public function getFieldValue()
124 return $this->getUri();
132 public function toString()
134 return $this->getFieldName() . ': ' . $this->getUri();
138 * Allow casting to string
142 public function __toString()
144 return $this->toString();