<?php
/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @copyright Copyright (c) Brian Nesbitt <brian@nesbot.com>
* @link http://cakephp.org CakePHP(tm) Project
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Chronos;
use DateInterval;
use InvalidArgumentException;
class ChronosInterval extends DateInterval
{
const PERIOD_PREFIX = 'P';
const PERIOD_YEARS = 'Y';
const PERIOD_MONTHS = 'M';
const PERIOD_DAYS = 'D';
const PERIOD_TIME_PREFIX = 'T';
const PERIOD_HOURS = 'H';
const PERIOD_MINUTES = 'M';
const PERIOD_SECONDS = 'S';
const PHP_DAYS_FALSE = -99999;
protected $isHHVM = false;
protected static function wasCreatedFromDiff(DateInterval $interval)
{
return ($interval->days !== false && $interval->days !== static::PHP_DAYS_FALSE);
}
public function __construct($years = 1, $months = null, $weeks = null, $days = null, $hours = null, $minutes = null, $seconds = null)
{
$this->isHHVM = defined('HHVM_VERSION');
$spec = static::PERIOD_PREFIX;
$spec .= $years > 0 ? $years . static::PERIOD_YEARS : '';
$spec .= $months > 0 ? $months . static::PERIOD_MONTHS : '';
$specDays = 0;
$specDays += $weeks > 0 ? $weeks * ChronosInterface::DAYS_PER_WEEK : 0;
$specDays += $days > 0 ? $days : 0;
$spec .= ($specDays > 0) ? $specDays . static::PERIOD_DAYS : '';
if ($spec === static::PERIOD_PREFIX) {
$spec .= '0' . static::PERIOD_YEARS;
}
if ($hours > 0 || $minutes > 0 || $seconds > 0) {
$spec .= static::PERIOD_TIME_PREFIX;
$spec .= $hours > 0 ? $hours . static::PERIOD_HOURS : '';
$spec .= $minutes > 0 ? $minutes . static::PERIOD_MINUTES : '';
$spec .= $seconds > 0 ? $seconds . static::PERIOD_SECONDS : '';
}
parent::__construct($spec);
}
public static function create($years = 1, $months = null, $weeks = null, $days = null, $hours = null, $minutes = null, $seconds = null)
{
return new static($years, $months, $weeks, $days, $hours, $minutes, $seconds);
}
public static function __callStatic($name, $args)
{
$arg = count($args) === 0 ? 1 : $args[0];
switch ($name) {
case 'years':
case 'year':
return new static($arg);
case 'months':
case 'month':
return new static(null, $arg);
case 'weeks':
case 'week':
return new static(null, null, $arg);
case 'days':
case 'dayz':
case 'day':
return new static(null, null, null, $arg);
case 'hours':
case 'hour':
return new static(null, null, null, null, $arg);
case 'minutes':
case 'minute':
return new static(null, null, null, null, null, $arg);
case 'seconds':
case 'second':
return new static(null, null, null, null, null, null, $arg);
}
}
public static function instance(DateInterval $di)
{
if (static::wasCreatedFromDiff($di)) {
throw new InvalidArgumentException(
"Can not instance a DateInterval object created from DateTime::diff()."
);
}
$instance = new static($di->y, $di->m, 0, $di->d, $di->h, $di->i, $di->s);
$instance->invert = $di->invert;
$instance->days = $di->days;
return $instance;
}
public function __get($name)
{
switch ($name) {
case 'years':
return $this->isHHVM ? parent::__get('y') : $this->y;
case 'months':
return $this->isHHVM ? parent::__get('m') : $this->m;
case 'dayz':
return $this->isHHVM ? parent::__get('d') : $this->d;
case 'hours':
return $this->isHHVM ? parent::__get('h') : $this->h;
case 'minutes':
return $this->isHHVM ? parent::__get('i') : $this->i;
case 'seconds':
return $this->isHHVM ? parent::__get('s') : $this->s;
case 'weeks':
return (int)floor(($this->isHHVM ? parent::__get('d') : $this->d) / ChronosInterface::DAYS_PER_WEEK);
case 'daysExcludeWeeks':
case 'dayzExcludeWeeks':
return $this->dayz % ChronosInterface::DAYS_PER_WEEK;
case 'days':
return $this->isHHVM ? parent::__get('days') : $this->days;
case 'y':
case 'm':
case 'd':
case 'h':
case 'i':
case 's':
case 'invert':
return parent::__get($name);
default:
throw new InvalidArgumentException(sprintf("Unknown getter '%s'", $name));
}
}
public function __set($name, $val)
{
switch ($name) {
case 'years':
$this->isHHVM ? parent::__set('y', $val) : $this->y = $val;
break;
case 'months':
$this->isHHVM ? parent::__set('m', $val) : $this->m = $val;
break;
case 'weeks':
$val = $val * ChronosInterface::DAYS_PER_WEEK;
$this->isHHVM ? parent::__set('d', $val) : $this->d = $val;
break;
case 'dayz':
$this->isHHVM ? parent::__set('d', $val) : $this->d = $val;
break;
case 'hours':
$this->isHHVM ? parent::__set('h', $val) : $this->h = $val;
break;
case 'minutes':
$this->isHHVM ? parent::__set('i', $val) : $this->i = $val;
break;
case 'seconds':
$this->isHHVM ? parent::__set('s', $val) : $this->s = $val;
break;
case 'invert':
$this->isHHVM ? parent::__set('invert', $val) : $this->invert = $val;
break;
default:
if ($this->isHHVM) {
parent::__set($name, $val);
}
}
}
public function weeksAndDays($weeks, $days)
{
$this->dayz = ($weeks * ChronosInterface::DAYS_PER_WEEK) + $days;
return $this;
}
public function __call($name, $args)
{
$arg = count($args) === 0 ? 1 : $args[0];
switch ($name) {
case 'years':
case 'year':
$this->years = $arg;
break;
case 'months':
case 'month':
$this->months = $arg;
break;
case 'weeks':
case 'week':
$this->dayz = $arg * ChronosInterface::DAYS_PER_WEEK;
break;
case 'days':
case 'dayz':
case 'day':
$this->dayz = $arg;
break;
case 'hours':
case 'hour':
$this->hours = $arg;
break;
case 'minutes':
case 'minute':
$this->minutes = $arg;
break;
case 'seconds':
case 'second':
$this->seconds = $arg;
break;
}
return $this;
}
public function add(DateInterval $interval)
{
$sign = ($interval->invert === 1) ? -1 : 1;
if (static::wasCreatedFromDiff($interval)) {
$this->dayz = $this->dayz + ($interval->days * $sign);
} else {
$this->years = $this->years + ($interval->y * $sign);
$this->months = $this->months + ($interval->m * $sign);
$this->dayz = $this->dayz + ($interval->d * $sign);
$this->hours = $this->hours + ($interval->h * $sign);
$this->minutes = $this->minutes + ($interval->i * $sign);
$this->seconds = $this->seconds + ($interval->s * $sign);
}
return $this;
}
public function __toString()
{
$oneMinuteInSeconds = 60;
$oneHourInSeconds = $oneMinuteInSeconds * 60;
$oneDayInSeconds = $oneHourInSeconds * 24;
$oneMonthInDays = 365 / 12;
$oneMonthInSeconds = $oneDayInSeconds * $oneMonthInDays;
$oneYearInSeconds = 12 * $oneMonthInSeconds;
$ySecs = $this->y * $oneYearInSeconds;
$mSecs = $this->m * $oneMonthInSeconds;
$dSecs = $this->d * $oneDayInSeconds;
$hSecs = $this->h * $oneHourInSeconds;
$iSecs = $this->i * $oneMinuteInSeconds;
$sSecs = $this->s;
$totalSecs = $ySecs + $mSecs + $dSecs + $hSecs + $iSecs + $sSecs;
$y = null;
$m = null;
$d = null;
$h = null;
$i = null;
if ($totalSecs >= $oneYearInSeconds) {
$y = floor($totalSecs / $oneYearInSeconds);
$totalSecs = $totalSecs - $y * $oneYearInSeconds;
}
if ($totalSecs >= $oneMonthInSeconds) {
$m = floor($totalSecs / $oneMonthInSeconds);
$totalSecs = $totalSecs - $m * $oneMonthInSeconds;
}
if ($totalSecs >= $oneDayInSeconds) {
$d = floor($totalSecs / $oneDayInSeconds);
$totalSecs = $totalSecs - $d * $oneDayInSeconds;
}
if ($totalSecs >= $oneHourInSeconds) {
$h = floor($totalSecs / $oneHourInSeconds);
$totalSecs = $totalSecs - $h * $oneHourInSeconds;
}
if ($totalSecs >= $oneMinuteInSeconds) {
$i = floor($totalSecs / $oneMinuteInSeconds);
$totalSecs = $totalSecs - $i * $oneMinuteInSeconds;
}
$s = $totalSecs;
$date = array_filter([
static::PERIOD_YEARS => $y,
static::PERIOD_MONTHS => $m,
static::PERIOD_DAYS => $d,
]);
$time = array_filter([
static::PERIOD_HOURS => $h,
static::PERIOD_MINUTES => $i,
static::PERIOD_SECONDS => $s,
]);
$specString = static::PERIOD_PREFIX;
foreach ($date as $key => $value) {
$specString .= $value . $key;
}
if (count($time) > 0) {
$specString .= static::PERIOD_TIME_PREFIX;
foreach ($time as $key => $value) {
$specString .= $value . $key;
}
}
if ($specString === static::PERIOD_PREFIX) {
return 'PT0S';
}
return $this->invert === 1 ? '-' . $specString : $specString;
}
}