Вход Регистрация
Файл: concrete5.7.5.6/concrete/vendor/zendframework/zend-http/src/Header/AbstractDate.php
Строк: 221
<?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 ZendHttpHeader;

use 
DateTime;
use 
DateTimeZone;

/**
 * Abstract Date/Time Header
 * Supports headers that have date/time as value
 *
 * @see ZendHttpHeaderDate
 * @see ZendHttpHeaderExpires
 * @see ZendHttpHeaderIfModifiedSince
 * @see ZendHttpHeaderIfUnmodifiedSince
 * @see ZendHttpHeaderLastModified
 *
 * Note for 'Location' header:
 * While RFC 1945 requires an absolute URI, most of the browsers also support relative URI
 * This class allows relative URIs, and let user retrieve URI instance if strict validation needed
 */
abstract class AbstractDate implements HeaderInterface
{
    
/**
     * Date formats according to RFC 2616
     * @link http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3
     */
    
const DATE_RFC1123 0;
    const 
DATE_RFC1036 1;
    const 
DATE_ANSIC   2;

    
/**
     * Date instance for this header
     *
     * @var DateTime
     */
    
protected $date null;

    
/**
     * Date output format
     *
     * @var string
     */
    
protected static $dateFormat 'D, d M Y H:i:s GMT';

    
/**
     * Date formats defined by RFC 2616. RFC 1123 date is required
     * RFC 1036 and ANSI C formats are provided for compatibility with old servers/clients
     * @link http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3
     *
     * @var array
     */
    
protected static $dateFormats = array(
        
self::DATE_RFC1123 => 'D, d M Y H:i:s GMT',
        
self::DATE_RFC1036 => 'D, d M y H:i:s GMT',
        
self::DATE_ANSIC   => 'D M j H:i:s Y',
    );

    
/**
     * Create date-based header from string
     *
     * @param string $headerLine
     * @return AbstractDate
     * @throws ExceptionInvalidArgumentException
     */
    
public static function fromString($headerLine)
    {
        
$dateHeader = new static();

        list(
$name$date) = GenericHeader::splitHeaderLine($headerLine);

        
// check to ensure proper header type for this factory
        
if (strtolower($name) !== strtolower($dateHeader->getFieldName())) {
            throw new 
ExceptionInvalidArgumentException(
                
'Invalid header line for "' $dateHeader->getFieldName() . '" header string'
            
);
        }

        
$dateHeader->setDate($date);

        return 
$dateHeader;
    }

    
/**
     * Set date output format
     *
     * @param int $format
     * @throws ExceptionInvalidArgumentException
     */
    
public static function setDateFormat($format)
    {
        if (!isset(static::
$dateFormats[$format])) {
            throw new 
ExceptionInvalidArgumentException(
                
"No constant defined for provided date format: {$format}"
            
);
        }

        static::
$dateFormat = static::$dateFormats[$format];
    }

    
/**
     * Return current date output format
     *
     * @return string
     */
    
public static function getDateFormat()
    {
        return static::
$dateFormat;
    }

    
/**
     * Set the date for this header, this can be a string or an instance of DateTime
     *
     * @param string|DateTime $date
     * @return AbstractDate
     * @throws ExceptionInvalidArgumentException
     */
    
public function setDate($date)
    {
        if (
is_string($date)) {
            try {
                
$date = new DateTime($date, new DateTimeZone('GMT'));
            } catch (
Exception $e) {
                throw new 
ExceptionInvalidArgumentException(
                    
sprintf('Invalid date passed as string (%s)', (string) $date),
                    
$e->getCode(),
                    
$e
                
);
            }
        } elseif (!(
$date instanceof DateTime)) {
            throw new 
ExceptionInvalidArgumentException('Date must be an instance of DateTime or a string');
        }

        
$date->setTimezone(new DateTimeZone('GMT'));
        
$this->date $date;

        return 
$this;
    }

    
/**
     * Return date for this header
     *
     * @return string
     */
    
public function getDate()
    {
        return 
$this->date()->format(static::$dateFormat);
    }

    
/**
     * Return date for this header as an instance of DateTime
     *
     * @return DateTime
     */
    
public function date()
    {
        if (
$this->date === null) {
            
$this->date = new DateTime(null, new DateTimeZone('GMT'));
        }
        return 
$this->date;
    }

    
/**
     * Compare provided date to date for this header
     * Returns < 0 if date in header is less than $date; > 0 if it's greater, and 0 if they are equal.
     * @see strcmp()
     *
     * @param string|DateTime $date
     * @return int
     * @throws ExceptionInvalidArgumentException
     */
    
public function compareTo($date)
    {
        if (
is_string($date)) {
            try {
                
$date = new DateTime($date, new DateTimeZone('GMT'));
            } catch (
Exception $e) {
                throw new 
ExceptionInvalidArgumentException(
                    
sprintf('Invalid Date passed as string (%s)', (string) $date),
                    
$e->getCode(),
                    
$e
                
);
            }
        } elseif (!(
$date instanceof DateTime)) {
            throw new 
ExceptionInvalidArgumentException('Date must be an instance of DateTime or a string');
        }

        
$dateTimestamp $date->getTimestamp();
        
$thisTimestamp $this->date()->getTimestamp();

        return (
$thisTimestamp === $dateTimestamp) ? : (($thisTimestamp $dateTimestamp) ? : -1);
    }

    
/**
     * Get header value as formatted date
     *
     * @return string
     */
    
public function getFieldValue()
    {
        return 
$this->getDate();
    }

    
/**
     * Return header line
     *
     * @return string
     */
    
public function toString()
    {
        return 
$this->getFieldName() . ': ' $this->getDate();
    }

    
/**
     * Allow casting to string
     *
     * @return string
     */
    
public function __toString()
    {
        return 
$this->toString();
    }
}
Онлайн: 1
Реклама