Created
February 5, 2012 20:42
-
-
Save lstrojny/1747838 to your computer and use it in GitHub Desktop.
Immutable DateTime
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace InterNations\Component\Date\Value; | |
use DateTime as DateTimeBase; | |
use DateTimeZone; | |
use DateInterval; | |
class DateTime extends DateTimeBase | |
{ | |
protected $frozen = true; | |
public function __construct($dateString = null, DateTimeZone $timezone = null) | |
{ | |
if ($timezone) { | |
parent::__construct($dateString, $timezone); | |
} else { | |
parent::__construct($dateString); | |
} | |
} | |
public static function now($microtime = false, DateTimeZone $timezone = null) | |
{ | |
if ($microtime) { | |
return new static(date('Y-m-d H:i:s' . substr(microtime(true), 10)), $timezone); | |
} | |
return new static('now', $timezone); | |
} | |
public static function createFromFormat($format, $time, $timezone = null) | |
{ | |
if ($timezone) { | |
$date = parent::createFromFormat($format, $time, $timezone); | |
} else { | |
$date = parent::createFromFormat($format, $time); | |
} | |
if (false === $date) { | |
return false; | |
} | |
return new static($date->format('Y-m-d H:i:s.u'), $timezone); | |
} | |
public function modify($dateString) | |
{ | |
return $this->_guard(__FUNCTION__, array($dateString)); | |
} | |
public function add($interval) | |
{ | |
return $this->_guard('add', func_get_args()); | |
} | |
public function sub($interval) | |
{ | |
return $this->_guard('sub', func_get_args()); | |
} | |
public function setTimezone($timezone) | |
{ | |
return $this->_guard('setTimezone', func_get_args()); | |
} | |
public function setTime($hour, $minute, $second = null) | |
{ | |
return $this->_guard('setTime', func_get_args()); | |
} | |
public function setDate($year, $month, $day) | |
{ | |
return $this->_guard('setDate', func_get_args()); | |
} | |
public function setISODate($year, $week, $day = 1) | |
{ | |
return $this->_guard('setISODate', func_get_args()); | |
} | |
public function setTimestamp($timestamp) | |
{ | |
return $this->_guard('setTimestamp', func_get_args()); | |
} | |
public function diff($date, $absolute = false) | |
{ | |
return $this->_guard('diff', func_get_args()); | |
} | |
public function isFuture(DateTime $compareDate = null) | |
{ | |
if (!$compareDate) { | |
$compareDate = self::now(false, $this->getTimezone()); | |
} | |
return $this > $compareDate; | |
} | |
public function isPast(DateTime $compareDate = null) | |
{ | |
if (!$compareDate) { | |
$compareDate = self::now(false, $this->getTimezone()); | |
} | |
return $this < $compareDate; | |
} | |
protected function _guard($method, array $arguments) | |
{ | |
if ($this->frozen) { | |
$date = clone $this; | |
$date->frozen = false; | |
call_user_func_array(array($date, $method), $arguments); | |
$date->frozen = true; | |
return $date; | |
} else { | |
return call_user_func_array(array('parent', $method), $arguments); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment