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
;
16 * Abstract Date/Time Header
17 * Supports headers that have date/time as value
19 * @see Zend\Http\Header\Date
20 * @see Zend\Http\Header\Expires
21 * @see Zend\Http\Header\IfModifiedSince
22 * @see Zend\Http\Header\IfUnmodifiedSince
23 * @see Zend\Http\Header\LastModified
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 AbstractDate
implements HeaderInterface
32 * Date formats according to RFC 2616
33 * @link http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3
35 const DATE_RFC1123
= 0;
36 const DATE_RFC1036
= 1;
40 * Date instance for this header
44 protected $date = null;
51 protected static $dateFormat = 'D, d M Y H:i:s \G\M\T';
54 * Date formats defined by RFC 2616. RFC 1123 date is required
55 * RFC 1036 and ANSI C formats are provided for compatibility with old servers/clients
56 * @link http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3
60 protected static $dateFormats = array(
61 self
::DATE_RFC1123
=> 'D, d M Y H:i:s \G\M\T',
62 self
::DATE_RFC1036
=> 'D, d M y H:i:s \G\M\T',
63 self
::DATE_ANSIC
=> 'D M j H:i:s Y',
67 * Create date-based header from string
69 * @param string $headerLine
70 * @return AbstractDate
71 * @throws Exception\InvalidArgumentException
73 public static function fromString($headerLine)
75 $dateHeader = new static();
77 list($name, $date) = GenericHeader
::splitHeaderLine($headerLine);
79 // check to ensure proper header type for this factory
80 if (strtolower($name) !== strtolower($dateHeader->getFieldName())) {
81 throw new Exception\
InvalidArgumentException(
82 'Invalid header line for "' . $dateHeader->getFieldName() . '" header string'
86 $dateHeader->setDate($date);
92 * Set date output format
95 * @throws Exception\InvalidArgumentException
97 public static function setDateFormat($format)
99 if (!isset(static::$dateFormats[$format])) {
100 throw new Exception\
InvalidArgumentException(
101 "No constant defined for provided date format: {$format}"
105 static::$dateFormat = static::$dateFormats[$format];
109 * Return current date output format
113 public static function getDateFormat()
115 return static::$dateFormat;
119 * Set the date for this header, this can be a string or an instance of \DateTime
121 * @param string|DateTime $date
122 * @return AbstractDate
123 * @throws Exception\InvalidArgumentException
125 public function setDate($date)
127 if (is_string($date)) {
129 $date = new DateTime($date, new DateTimeZone('GMT'));
130 } catch (\Exception
$e) {
131 throw new Exception\
InvalidArgumentException(
132 sprintf('Invalid date passed as string (%s)', (string) $date),
137 } elseif (!($date instanceof DateTime
)) {
138 throw new Exception\
InvalidArgumentException('Date must be an instance of \DateTime or a string');
141 $date->setTimezone(new DateTimeZone('GMT'));
148 * Return date for this header
152 public function getDate()
154 return $this->date()->format(static::$dateFormat);
158 * Return date for this header as an instance of \DateTime
162 public function date()
164 if ($this->date
=== null) {
165 $this->date
= new DateTime(null, new DateTimeZone('GMT'));
171 * Compare provided date to date for this header
172 * Returns < 0 if date in header is less than $date; > 0 if it's greater, and 0 if they are equal.
175 * @param string|DateTime $date
177 * @throws Exception\InvalidArgumentException
179 public function compareTo($date)
181 if (is_string($date)) {
183 $date = new DateTime($date, new DateTimeZone('GMT'));
184 } catch (\Exception
$e) {
185 throw new Exception\
InvalidArgumentException(
186 sprintf('Invalid Date passed as string (%s)', (string) $date),
191 } elseif (!($date instanceof DateTime
)) {
192 throw new Exception\
InvalidArgumentException('Date must be an instance of \DateTime or a string');
195 $dateTimestamp = $date->getTimestamp();
196 $thisTimestamp = $this->date()->getTimestamp();
198 return ($thisTimestamp === $dateTimestamp) ?
0 : (($thisTimestamp > $dateTimestamp) ?
1 : -1);
202 * Get header value as formatted date
206 public function getFieldValue()
208 return $this->getDate();
216 public function toString()
218 return $this->getFieldName() . ': ' . $this->getDate();
222 * Allow casting to string
226 public function __toString()
228 return $this->toString();