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 / AbstractDate.php
blob1882cf27855c5f718ba79d636187906bdedcf2df
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 use DateTime;
13 use DateTimeZone;
15 /**
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
31 /**
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;
37 const DATE_ANSIC = 2;
39 /**
40 * Date instance for this header
42 * @var DateTime
44 protected $date = null;
46 /**
47 * Date output format
49 * @var string
51 protected static $dateFormat = 'D, d M Y H:i:s \G\M\T';
53 /**
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
58 * @var array
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',
66 /**
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);
88 return $dateHeader;
91 /**
92 * Set date output format
94 * @param int $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
111 * @return string
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)) {
128 try {
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),
133 $e->getCode(),
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'));
142 $this->date = $date;
144 return $this;
148 * Return date for this header
150 * @return string
152 public function getDate()
154 return $this->date()->format(static::$dateFormat);
158 * Return date for this header as an instance of \DateTime
160 * @return DateTime
162 public function date()
164 if ($this->date === null) {
165 $this->date = new DateTime(null, new DateTimeZone('GMT'));
167 return $this->date;
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.
173 * @see \strcmp()
175 * @param string|DateTime $date
176 * @return int
177 * @throws Exception\InvalidArgumentException
179 public function compareTo($date)
181 if (is_string($date)) {
182 try {
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),
187 $e->getCode(),
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
204 * @return string
206 public function getFieldValue()
208 return $this->getDate();
212 * Return header line
214 * @return string
216 public function toString()
218 return $this->getFieldName() . ': ' . $this->getDate();
222 * Allow casting to string
224 * @return string
226 public function __toString()
228 return $this->toString();