upgrade to laravel 7 and set branch to v2

This commit is contained in:
2020-12-25 11:22:15 +00:00
parent 516105c492
commit 0ddd298350
4638 changed files with 132501 additions and 190226 deletions
+27 -20
View File
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
@@ -28,10 +30,11 @@ use DateTimeZone;
* @property-read int $hour
* @property-read int $minute
* @property-read int $second
* @property-read int $timestamp seconds since the Unix Epoch
* @property-read DateTimeZone $timezone the current timezone
* @property-read DateTimeZone $tz alias of timezone
* @property-read int $micro
* @property-read int $microsecond
* @property-read int $timestamp seconds since the Unix Epoch
* @property-read \DateTimeZone $timezone the current timezone
* @property-read \DateTimeZone $tz alias of timezone
* @property-read int $dayOfWeek 1 (for Monday) through 7 (for Sunday)
* @property-read int $dayOfYear 0 through 365
* @property-read int $weekOfMonth 1 through 5
@@ -64,7 +67,7 @@ class Chronos extends DateTimeImmutable implements ChronosInterface
* There is a single test now for all date/time classes provided by Chronos.
* This aims to emulate stubbing out 'now' which is a single global fact.
*
* @var \Cake\Chronos\ChronosInterface
* @var \Cake\Chronos\ChronosInterface|null
*/
protected static $testNow;
@@ -81,7 +84,7 @@ class Chronos extends DateTimeImmutable implements ChronosInterface
* Please see the testing aids section (specifically static::setTestNow())
* for more on the possibility of this constructor returning a test instance.
*
* @param string|null $time Fixed or relative time
* @param \DateTime|\DateTimeImmutable|string|int|null $time Fixed or relative time
* @param \DateTimeZone|string|null $tz The timezone for the instance
*/
public function __construct($time = 'now', $tz = null)
@@ -90,10 +93,14 @@ class Chronos extends DateTimeImmutable implements ChronosInterface
$tz = $tz instanceof DateTimeZone ? $tz : new DateTimeZone($tz);
}
if ($time instanceof \DateTimeInterface) {
$time = $time->format('Y-m-d H:i:s.u');
}
static::$_lastErrors = [];
$testNow = static::getTestNow();
if ($testNow === null) {
parent::__construct($time === null ? 'now' : $time, $tz);
parent::__construct($time ?? 'now', $tz);
return;
}
@@ -106,13 +113,13 @@ class Chronos extends DateTimeImmutable implements ChronosInterface
}
$testNow = clone $testNow;
if ($relative) {
$testNow = $testNow->modify($time);
$relativetime = static::isTimeExpression($time);
if (!$relativetime && $tz !== $testNow->getTimezone()) {
$testNow = $testNow->setTimezone($tz ?? date_default_timezone_get());
}
$relativeTime = static::isTimeExpression($time);
if (!$relativeTime && $tz !== $testNow->getTimezone()) {
$testNow = $testNow->setTimezone($tz === null ? date_default_timezone_get() : $tz);
if ($relative) {
$testNow = $testNow->modify($time);
}
$time = $testNow->format('Y-m-d H:i:s.u');
@@ -124,7 +131,7 @@ class Chronos extends DateTimeImmutable implements ChronosInterface
*
* @return \Cake\Chronos\MutableDateTime
*/
public function toMutable()
public function toMutable(): MutableDateTime
{
return MutableDateTime::instance($this);
}
@@ -132,11 +139,11 @@ class Chronos extends DateTimeImmutable implements ChronosInterface
/**
* Get a copy of the instance
*
* @return $this
* @return static
*/
public function copy()
public function copy(): ChronosInterface
{
return $this;
return clone $this;
}
/**
@@ -157,7 +164,7 @@ class Chronos extends DateTimeImmutable implements ChronosInterface
* @param \Cake\Chronos\ChronosInterface|string|null $testNow The instance to use for all future instances.
* @return void
*/
public static function setTestNow($testNow = null)
public static function setTestNow($testNow = null): void
{
static::$testNow = is_string($testNow) ? static::parse($testNow) : $testNow;
}
@@ -166,9 +173,9 @@ class Chronos extends DateTimeImmutable implements ChronosInterface
* Get the ChronosInterface instance (real or mock) to be returned when a "now"
* instance is created.
*
* @return \Cake\Chronos\ChronosInterface The current instance used for testing
* @return \Cake\Chronos\ChronosInterface|null The current instance used for testing
*/
public static function getTestNow()
public static function getTestNow(): ?ChronosInterface
{
return static::$testNow;
}
@@ -179,7 +186,7 @@ class Chronos extends DateTimeImmutable implements ChronosInterface
*
* @return bool True if there is a test instance, otherwise false
*/
public static function hasTestNow()
public static function hasTestNow(): bool
{
return static::$testNow !== null;
}
@@ -189,7 +196,7 @@ class Chronos extends DateTimeImmutable implements ChronosInterface
*
* @return array
*/
public function __debugInfo()
public function __debugInfo(): array
{
$properties = [
'hasFixedNow' => static::hasTestNow(),
File diff suppressed because it is too large Load Diff
+112 -76
View File
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
@@ -12,6 +14,7 @@
*/
namespace Cake\Chronos;
use BadMethodCallException;
use DateInterval;
use InvalidArgumentException;
@@ -27,61 +30,61 @@ use InvalidArgumentException;
* @property int $hours Total hours of the current interval.
* @property int $minutes Total minutes of the current interval.
* @property int $seconds Total seconds of the current interval.
*
* @property int $microseconds Total microseconds of the current interval.
* @property-read int $dayzExcludeWeeks Total days remaining in the final week of the current instance (days % 7).
* @property-read int $daysExcludeWeeks alias of dayzExcludeWeeks
*
* @method static ChronosInterval years($years = 1) Create instance specifying a number of years.
* @method static ChronosInterval year($years = 1) Alias for years
* @method static ChronosInterval months($months = 1) Create instance specifying a number of months.
* @method static ChronosInterval month($months = 1) Alias for months
* @method static ChronosInterval weeks($weeks = 1) Create instance specifying a number of weeks.
* @method static ChronosInterval week($weeks = 1) Alias for weeks
* @method static ChronosInterval days($days = 1) Create instance specifying a number of days.
* @method static ChronosInterval dayz($days = 1) Alias for days
* @method static ChronosInterval day($days = 1) Alias for days
* @method static ChronosInterval hours($hours = 1) Create instance specifying a number of hours.
* @method static ChronosInterval hour($hours = 1) Alias for hours
* @method static ChronosInterval minutes($minutes = 1) Create instance specifying a number of minutes.
* @method static ChronosInterval minute($minutes = 1) Alias for minutes
* @method static ChronosInterval seconds($seconds = 1) Create instance specifying a number of seconds.
* @method static ChronosInterval second($seconds = 1) Alias for seconds
*
* @method ChronosInterval years() years($years = 1) Set the years portion of the current interval.
* @method ChronosInterval year() year($years = 1) Alias for years.
* @method ChronosInterval months() months($months = 1) Set the months portion of the current interval.
* @method ChronosInterval month() month($months = 1) Alias for months.
* @method ChronosInterval weeks() weeks($weeks = 1) Set the weeks portion of the current interval. Will overwrite dayz value.
* @method ChronosInterval week() week($weeks = 1) Alias for weeks.
* @method ChronosInterval days() days($days = 1) Set the days portion of the current interval.
* @method ChronosInterval dayz() dayz($days = 1) Alias for days.
* @method ChronosInterval day() day($days = 1) Alias for days.
* @method ChronosInterval hours() hours($hours = 1) Set the hours portion of the current interval.
* @method ChronosInterval hour() hour($hours = 1) Alias for hours.
* @method ChronosInterval minutes() minutes($minutes = 1) Set the minutes portion of the current interval.
* @method ChronosInterval minute() minute($minutes = 1) Alias for minutes.
* @method ChronosInterval seconds() seconds($seconds = 1) Set the seconds portion of the current interval.
* @method ChronosInterval second() second($seconds = 1) Alias for seconds.
* @method static \Cake\Chronos\ChronosInterval years($years = 1) Create instance specifying a number of years.
* @method static \Cake\Chronos\ChronosInterval year($years = 1) Alias for years
* @method static \Cake\Chronos\ChronosInterval months($months = 1) Create instance specifying a number of months.
* @method static \Cake\Chronos\ChronosInterval month($months = 1) Alias for months
* @method static \Cake\Chronos\ChronosInterval weeks($weeks = 1) Create instance specifying a number of weeks.
* @method static \Cake\Chronos\ChronosInterval week($weeks = 1) Alias for weeks
* @method static \Cake\Chronos\ChronosInterval days($days = 1) Create instance specifying a number of days.
* @method static \Cake\Chronos\ChronosInterval dayz($days = 1) Alias for days
* @method static \Cake\Chronos\ChronosInterval day($days = 1) Alias for days
* @method static \Cake\Chronos\ChronosInterval hours($hours = 1) Create instance specifying a number of hours.
* @method static \Cake\Chronos\ChronosInterval hour($hours = 1) Alias for hours
* @method static \Cake\Chronos\ChronosInterval minutes($minutes = 1) Create instance specifying a number of minutes.
* @method static \Cake\Chronos\ChronosInterval minute($minutes = 1) Alias for minutes
* @method static \Cake\Chronos\ChronosInterval seconds($seconds = 1) Create instance specifying a number of seconds.
* @method static \Cake\Chronos\ChronosInterval second($seconds = 1) Alias for seconds
* @method static \Cake\Chronos\ChronosInterval microseconds($microseconds = 1) Create instance specifying a number of microseconds.
* @method static \Cake\Chronos\ChronosInterval microsecond($microseconds = 1) Alias for microseconds
*/
class ChronosInterval extends DateInterval
{
/**
* Interval spec period designators
* @var string
*/
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';
public const PERIOD_PREFIX = 'P';
/**
* Before PHP 5.4.20/5.5.4 instead of `false` days will be set to -99999 when the interval instance
* was created by DateTime:diff().
* @var string
*/
const PHP_DAYS_FALSE = -99999;
public const PERIOD_YEARS = 'Y';
/**
* @var string
*/
public const PERIOD_MONTHS = 'M';
/**
* @var string
*/
public const PERIOD_DAYS = 'D';
/**
* @var string
*/
public const PERIOD_TIME_PREFIX = 'T';
/**
* @var string
*/
public const PERIOD_HOURS = 'H';
/**
* @var string
*/
public const PERIOD_MINUTES = 'M';
/**
* @var string
*/
public const PERIOD_SECONDS = 'S';
/**
* Determine if the interval was created via DateTime:diff() or not.
@@ -89,9 +92,9 @@ class ChronosInterval extends DateInterval
* @param \DateInterval $interval The interval to check.
* @return bool
*/
protected static function wasCreatedFromDiff(DateInterval $interval)
protected static function wasCreatedFromDiff(DateInterval $interval): bool
{
return ($interval->days !== false && $interval->days !== static::PHP_DAYS_FALSE);
return $interval->days !== false;
}
/**
@@ -104,9 +107,18 @@ class ChronosInterval extends DateInterval
* @param int|null $hours The hours to use.
* @param int|null $minutes The minutes to use.
* @param int|null $seconds The seconds to use.
* @param int|null $microseconds The microseconds to use.
*/
public function __construct($years = 1, $months = null, $weeks = null, $days = null, $hours = null, $minutes = null, $seconds = null)
{
public function __construct(
?int $years = 1,
?int $months = null,
?int $weeks = null,
?int $days = null,
?int $hours = null,
?int $minutes = null,
?int $seconds = null,
?int $microseconds = null
) {
$spec = static::PERIOD_PREFIX;
$spec .= $years > 0 ? $years . static::PERIOD_YEARS : '';
@@ -116,7 +128,7 @@ class ChronosInterval extends DateInterval
$specDays += $weeks > 0 ? $weeks * ChronosInterface::DAYS_PER_WEEK : 0;
$specDays += $days > 0 ? $days : 0;
$spec .= ($specDays > 0) ? $specDays . static::PERIOD_DAYS : '';
$spec .= $specDays > 0 ? $specDays . static::PERIOD_DAYS : '';
if ($spec === static::PERIOD_PREFIX) {
$spec .= '0' . static::PERIOD_YEARS;
@@ -130,6 +142,10 @@ class ChronosInterval extends DateInterval
}
parent::__construct($spec);
if ($microseconds > 0) {
$this->f = $microseconds / 1000000;
}
}
/**
@@ -145,11 +161,20 @@ class ChronosInterval extends DateInterval
* @param int|null $hours The hours to use.
* @param int|null $minutes The minutes to use.
* @param int|null $seconds The seconds to use.
* @param int|null $microseconds The microseconds to use.
* @return static
*/
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 create(
?int $years = 1,
?int $months = null,
?int $weeks = null,
?int $days = null,
?int $hours = null,
?int $minutes = null,
?int $seconds = null,
?int $microseconds = null
): self {
return new static($years, $months, $weeks, $days, $hours, $minutes, $seconds, $microseconds);
}
/**
@@ -168,7 +193,7 @@ class ChronosInterval extends DateInterval
* @param array $args Contains the value to use.
* @return static
*/
public static function __callStatic($name, $args)
public static function __callStatic(string $name, array $args): self
{
$arg = count($args) === 0 ? 1 : $args[0];
@@ -201,7 +226,13 @@ class ChronosInterval extends DateInterval
case 'seconds':
case 'second':
return new static(null, null, null, null, null, null, $arg);
case 'microseconds':
case 'microsecond':
return new static(null, null, null, null, null, null, null, $arg);
}
throw new BadMethodCallException(sprintf('Cannot create interval with `%s` units', $name));
}
/**
@@ -213,15 +244,16 @@ class ChronosInterval extends DateInterval
* @throws \InvalidArgumentException
* @return static
*/
public static function instance(DateInterval $di)
public static function instance(DateInterval $di): self
{
if (static::wasCreatedFromDiff($di)) {
throw new InvalidArgumentException(
"Can not instance a DateInterval object created from DateTime::diff()."
'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->f = $di->f;
$instance->invert = $di->invert;
$instance->days = $di->days;
@@ -235,7 +267,7 @@ class ChronosInterval extends DateInterval
* @throws \InvalidArgumentException
* @return int
*/
public function __get($name)
public function __get(string $name)
{
switch ($name) {
case 'years':
@@ -256,22 +288,16 @@ class ChronosInterval extends DateInterval
case 'seconds':
return $this->s;
case 'microseconds':
return (int)($this->f * 1000000);
case 'weeks':
return (int)floor($this->d / ChronosInterface::DAYS_PER_WEEK);
case 'daysExcludeWeeks':
case 'dayzExcludeWeeks':
return $this->dayz % ChronosInterface::DAYS_PER_WEEK;
case 'days':
return $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));
}
@@ -285,7 +311,7 @@ class ChronosInterval extends DateInterval
* @return void
* @throws \InvalidArgumentException
*/
public function __set($name, $val)
public function __set(string $name, $val): void
{
switch ($name) {
case 'years':
@@ -317,6 +343,10 @@ class ChronosInterval extends DateInterval
$this->s = $val;
break;
case 'microseconds':
$this->f = $val / 1000000;
break;
case 'invert':
$this->invert = $val;
break;
@@ -328,9 +358,9 @@ class ChronosInterval extends DateInterval
*
* @param int $weeks Number of weeks to set
* @param int $days Number of days to set
* @return static
* @return $this
*/
public function weeksAndDays($weeks, $days)
public function weeksAndDays(int $weeks, int $days)
{
$this->dayz = ($weeks * ChronosInterface::DAYS_PER_WEEK) + $days;
@@ -346,9 +376,9 @@ class ChronosInterval extends DateInterval
* @param string $name The property name to augment. Accepts plural forms in addition
* to singular ones.
* @param array $args The value to set.
* @return static
* @return $this
*/
public function __call($name, $args)
public function __call(string $name, array $args)
{
$arg = count($args) === 0 ? 1 : $args[0];
@@ -388,6 +418,11 @@ class ChronosInterval extends DateInterval
case 'second':
$this->seconds = $arg;
break;
case 'microseconds':
case 'microsecond':
$this->microseconds = $arg;
break;
}
return $this;
@@ -397,11 +432,11 @@ class ChronosInterval extends DateInterval
* Add the passed interval to the current instance
*
* @param \DateInterval $interval The interval to add.
* @return static
* @return $this
*/
public function add(DateInterval $interval)
{
$sign = ($interval->invert === 1) ? -1 : 1;
$sign = $interval->invert === 1 ? -1 : 1;
if (static::wasCreatedFromDiff($interval)) {
$this->dayz = $this->dayz + ($interval->days * $sign);
@@ -412,6 +447,7 @@ class ChronosInterval extends DateInterval
$this->hours = $this->hours + ($interval->h * $sign);
$this->minutes = $this->minutes + ($interval->i * $sign);
$this->seconds = $this->seconds + ($interval->s * $sign);
$this->microseconds = $this->microseconds + (int)($interval->f * 1000000 * $sign);
}
return $this;
@@ -422,7 +458,7 @@ class ChronosInterval extends DateInterval
*
* @return string Interval as string representation
*/
public function __toString()
public function __toString(): string
{
// equivalence
$oneMinuteInSeconds = 60;
+29 -30
View File
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
@@ -29,10 +31,11 @@ use DateTimeZone;
* @property-read int $hour
* @property-read int $minute
* @property-read int $second
* @property-read int $timestamp seconds since the Unix Epoch
* @property-read DateTimeZone $timezone the current timezone
* @property-read DateTimeZone $tz alias of timezone
* @property-read int $micro
* @property-read int $microsecond
* @property-read int $timestamp seconds since the Unix Epoch
* @property-read \DateTimeZone $timezone the current timezone
* @property-read \DateTimeZone $tz alias of timezone
* @property-read int $dayOfWeek 1 (for Monday) through 7 (for Sunday)
* @property-read int $dayOfYear 0 through 365
* @property-read int $weekOfMonth 1 through 5
@@ -69,51 +72,47 @@ class Date extends DateTimeImmutable implements ChronosInterface
/**
* Create a new Immutable Date instance.
*
* You can specify the timezone for the $time parameter. This timezone will
* not be used in any future modifications to the Date instance.
*
* The $timezone parameter is ignored if $time is a DateTimeInterface
* instance.
*
* Please see the testing aids section (specifically static::setTestNow())
* for more on the possibility of this constructor returning a test instance.
*
* Date instances lack time components, however due to limitations in PHP's
* internal Datetime object the time will always be set to 00:00:00, and the
* timezone will always be UTC. Normalizing the timezone allows for
* timezone will always be the server local time. Normalizing the timezone allows for
* subtraction/addition to have deterministic results.
*
* @param string|null|\DateTimeInterface $time Fixed or relative time
* @param \DateTime|\DateTimeImmutable|string|int|null $time Fixed or relative time
* @param \DateTimeZone|string|null $tz The timezone in which the date is taken
*/
public function __construct($time = 'now')
public function __construct($time = 'now', $tz = null)
{
$tz = new DateTimeZone('UTC');
$testNow = Chronos::getTestNow();
if ($testNow === null) {
$time = $this->stripTime($time);
parent::__construct($time, $tz);
return;
if ($tz !== null) {
$tz = $tz instanceof DateTimeZone ? $tz : new DateTimeZone($tz);
}
$relative = static::hasRelativeKeywords($time);
if (!empty($time) && $time !== 'now' && !$relative) {
$time = $this->stripTime($time);
parent::__construct($time, $tz);
$testNow = Chronos::getTestNow();
if ($testNow === null || !static::isRelativeOnly($time)) {
$time = $this->stripTime($time, $tz);
parent::__construct($time);
return;
}
$testNow = clone $testNow;
if ($relative) {
$time = $this->stripRelativeTime($time);
if (strlen($time) > 0) {
$testNow = $testNow->modify($time);
}
}
if ($tz !== $testNow->getTimezone()) {
$testNow = $testNow->setTimezone($tz === null ? date_default_timezone_get() : $tz);
$testNow = $testNow->setTimezone($tz ?? date_default_timezone_get());
}
if (!empty($time)) {
$testNow = $testNow->modify($time);
}
$time = $testNow->format('Y-m-d 00:00:00');
parent::__construct($time, $tz);
parent::__construct($time);
}
/**
@@ -121,7 +120,7 @@ class Date extends DateTimeImmutable implements ChronosInterface
*
* @return \Cake\Chronos\MutableDate
*/
public function toMutable()
public function toMutable(): MutableDate
{
return MutableDate::instance($this);
}
@@ -131,7 +130,7 @@ class Date extends DateTimeImmutable implements ChronosInterface
*
* @return array
*/
public function __debugInfo()
public function __debugInfo(): array
{
$properties = [
'hasFixedNow' => static::hasTestNow(),
+14 -10
View File
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
@@ -18,9 +20,8 @@ namespace Cake\Chronos;
* Provides a swappable component for other libraries to leverage.
* when localizing or customizing the difference output.
*/
class DifferenceFormatter
class DifferenceFormatter implements DifferenceFormatterInterface
{
/**
* The text translator object
*
@@ -33,7 +34,7 @@ class DifferenceFormatter
*
* @param \Cake\Chronos\Translator|null $translate The text translator object.
*/
public function __construct($translate = null)
public function __construct(?Translator $translate = null)
{
$this->translate = $translate ?: new Translator();
}
@@ -47,8 +48,11 @@ class DifferenceFormatter
* @return string The difference between the two days in a human readable format
* @see \Cake\Chronos\ChronosInterface::diffForHumans
*/
public function diffForHumans(ChronosInterface $date, ChronosInterface $other = null, $absolute = false)
{
public function diffForHumans(
ChronosInterface $date,
?ChronosInterface $other = null,
bool $absolute = false
): string {
$isNow = $other === null;
if ($isNow) {
$other = $date->now($date->tz);
@@ -56,15 +60,15 @@ class DifferenceFormatter
$diffInterval = $date->diff($other);
switch (true) {
case ($diffInterval->y > 0):
case $diffInterval->y > 0:
$unit = 'year';
$count = $diffInterval->y;
break;
case ($diffInterval->m > 0):
case $diffInterval->m > 0:
$unit = 'month';
$count = $diffInterval->m;
break;
case ($diffInterval->d > 0):
case $diffInterval->d > 0:
$unit = 'day';
$count = $diffInterval->d;
if ($count >= ChronosInterface::DAYS_PER_WEEK) {
@@ -72,11 +76,11 @@ class DifferenceFormatter
$count = (int)($count / ChronosInterface::DAYS_PER_WEEK);
}
break;
case ($diffInterval->h > 0):
case $diffInterval->h > 0:
$unit = 'hour';
$count = $diffInterval->h;
break;
case ($diffInterval->i > 0):
case $diffInterval->i > 0:
$unit = 'minute';
$count = $diffInterval->i;
break;
@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
/**
* 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)
* @link http://cakephp.org CakePHP(tm) Project
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Chronos;
/**
* Interface for formatting differences in text.
*/
interface DifferenceFormatterInterface
{
/**
* Get the difference in a human readable format.
*
* @param \Cake\Chronos\ChronosInterface $date The datetime to start with.
* @param \Cake\Chronos\ChronosInterface|null $other The datetime to compare against.
* @param bool $absolute Removes time difference modifiers ago, after, etc.
* @return string The difference between the two days in a human readable format.
* @see \Cake\Chronos\ChronosInterface::diffForHumans
*/
public function diffForHumans(
ChronosInterface $date,
?ChronosInterface $other = null,
bool $absolute = false
): string;
}
+29 -30
View File
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
@@ -28,10 +30,11 @@ use DateTimeZone;
* @property-read int $hour
* @property-read int $minute
* @property-read int $second
* @property-read int $timestamp seconds since the Unix Epoch
* @property-read DateTimeZone $timezone the current timezone
* @property-read DateTimeZone $tz alias of timezone
* @property-read int $micro
* @property-read int $microsecond
* @property-read int $timestamp seconds since the Unix Epoch
* @property-read \DateTimeZone $timezone the current timezone
* @property-read \DateTimeZone $tz alias of timezone
* @property-read int $dayOfWeek 1 (for Monday) through 7 (for Sunday)
* @property-read int $dayOfYear 0 through 365
* @property-read int $weekOfMonth 1 through 5
@@ -68,51 +71,47 @@ class MutableDate extends DateTime implements ChronosInterface
/**
* Create a new mutable Date instance.
*
* You can specify the timezone for the $time parameter. This timezone will
* not be used in any future modifications to the Date instance.
*
* The $timezone parameter is ignored if $time is a DateTimeInterface
* instance.
*
* Please see the testing aids section (specifically static::setTestNow())
* for more on the possibility of this constructor returning a test instance.
*
* Date instances lack time components, however due to limitations in PHP's
* internal Datetime object the time will always be set to 00:00:00, and the
* timezone will always be UTC. Normalizing the timezone allows for
* timezone will always be server local timezone. Normalizing the timezone allows for
* subtraction/addition to have deterministic results.
*
* @param string|null|\DateTimeInterface $time Fixed or relative time
* @param \DateTime|\DateTimeImmutable|string|int|null $time Fixed or relative time
* @param \DateTimeZone|string|null $tz The timezone in which the date is taken
*/
public function __construct($time = 'now')
public function __construct($time = 'now', $tz = null)
{
$tz = new DateTimeZone('UTC');
$testNow = Chronos::getTestNow();
if ($testNow === null) {
$time = $this->stripTime($time);
parent::__construct($time, $tz);
return;
if ($tz !== null) {
$tz = $tz instanceof DateTimeZone ? $tz : new DateTimeZone($tz);
}
$relative = static::hasRelativeKeywords($time);
if (!empty($time) && $time !== 'now' && !$relative) {
$time = $this->stripTime($time);
parent::__construct($time, $tz);
$testNow = Chronos::getTestNow();
if ($testNow === null || !static::isRelativeOnly($time)) {
$time = $this->stripTime($time, $tz);
parent::__construct($time);
return;
}
$testNow = clone $testNow;
if ($relative) {
$time = $this->stripRelativeTime($time);
if (strlen($time) > 0) {
$testNow = $testNow->modify($time);
}
}
if ($tz !== $testNow->getTimezone()) {
$testNow = $testNow->setTimezone($tz === null ? date_default_timezone_get() : $tz);
$testNow = $testNow->setTimezone($tz ?? date_default_timezone_get());
}
if (!empty($time)) {
$testNow = $testNow->modify($time);
}
$time = $testNow->format('Y-m-d 00:00:00');
parent::__construct($time, $tz);
parent::__construct($time);
}
/**
@@ -120,7 +119,7 @@ class MutableDate extends DateTime implements ChronosInterface
*
* @return \Cake\Chronos\Date
*/
public function toImmutable()
public function toImmutable(): Date
{
return Date::instance($this);
}
@@ -130,7 +129,7 @@ class MutableDate extends DateTime implements ChronosInterface
*
* @return array
*/
public function __debugInfo()
public function __debugInfo(): array
{
$properties = [
'hasFixedNow' => static::hasTestNow(),
+29 -21
View File
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
@@ -22,17 +24,18 @@ use InvalidArgumentException;
* This object can be mutated in place using any setter method,
* or __set().
*
* @property int $year
* @property int $yearIso
* @property int $month
* @property int $day
* @property int $hour
* @property int $minute
* @property int $second
* @property int $timestamp seconds since the Unix Epoch
* @property DateTimeZone|string $timezone the current timezone
* @property DateTimeZone|string $tz alias of timezone
* @property int $micro
* @property-read int $year
* @property-read int $yearIso
* @property-read int $month
* @property-read int $day
* @property-read int $hour
* @property-read int $minute
* @property-read int $second
* @property-read int $micro
* @property-read int $microsecond
* @property-read int $timestamp seconds since the Unix Epoch
* @property-read \DateTimeZone|string $timezone the current timezone
* @property-read \DateTimeZone|string $tz alias of timezone
* @property-read int $dayOfWeek 1 (for Monday) through 7 (for Sunday)
* @property-read int $dayOfYear 0 through 365
* @property-read int $weekOfMonth 1 through 5
@@ -73,7 +76,7 @@ class MutableDateTime extends DateTime implements ChronosInterface
* Please see the testing aids section (specifically static::setTestNow())
* for more on the possibility of this constructor returning a test instance.
*
* @param string|null $time Fixed or relative time
* @param \DateTime|\DateTimeImmutable|string|int|null $time Fixed or relative time
* @param \DateTimeZone|string|null $tz The timezone for the instance
*/
public function __construct($time = 'now', $tz = null)
@@ -82,9 +85,13 @@ class MutableDateTime extends DateTime implements ChronosInterface
$tz = $tz instanceof DateTimeZone ? $tz : new DateTimeZone($tz);
}
if ($time instanceof \DateTimeInterface) {
$time = $time->format('Y-m-d H:i:s.u');
}
$testNow = Chronos::getTestNow();
if ($testNow === null) {
parent::__construct($time === null ? 'now' : $time, $tz);
parent::__construct($time ?? 'now', $tz);
return;
}
@@ -97,14 +104,15 @@ class MutableDateTime extends DateTime implements ChronosInterface
}
$testNow = clone $testNow;
$relativetime = static::isTimeExpression($time);
if (!$relativetime && $tz !== $testNow->getTimezone()) {
$testNow = $testNow->setTimezone($tz ?? date_default_timezone_get());
}
if ($relative) {
$testNow = $testNow->modify($time);
}
$relativetime = static::isTimeExpression($time);
if (!$relativetime && $tz !== $testNow->getTimezone()) {
$testNow = $testNow->setTimezone($tz === null ? date_default_timezone_get() : $tz);
}
$time = $testNow->format('Y-m-d H:i:s.u');
parent::__construct($time, $tz);
}
@@ -112,9 +120,9 @@ class MutableDateTime extends DateTime implements ChronosInterface
/**
* Create a new immutable instance from current mutable instance.
*
* @return Chronos
* @return \Cake\Chronos\Chronos
*/
public function toImmutable()
public function toImmutable(): Chronos
{
return Chronos::instance($this);
}
@@ -127,7 +135,7 @@ class MutableDateTime extends DateTime implements ChronosInterface
* @throws \InvalidArgumentException
* @return void
*/
public function __set($name, $value)
public function __set(string $name, $value): void
{
switch ($name) {
case 'year':
@@ -173,7 +181,7 @@ class MutableDateTime extends DateTime implements ChronosInterface
*
* @return array
*/
public function __debugInfo()
public function __debugInfo(): array
{
$properties = [
'hasFixedNow' => static::hasTestNow(),
+47 -47
View File
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
@@ -34,7 +36,7 @@ trait ComparisonTrait
*
* @return array
*/
public static function getWeekendDays()
public static function getWeekendDays(): array
{
return static::$weekendDays;
}
@@ -45,7 +47,7 @@ trait ComparisonTrait
* @param array $days Which days are 'weekends'.
* @return void
*/
public static function setWeekendDays($days)
public static function setWeekendDays(array $days): void
{
static::$weekendDays = $days;
}
@@ -56,7 +58,7 @@ trait ComparisonTrait
* @param \Cake\Chronos\ChronosInterface $dt The instance to compare with.
* @return bool
*/
public function eq(ChronosInterface $dt)
public function eq(ChronosInterface $dt): bool
{
return $this == $dt;
}
@@ -78,7 +80,7 @@ trait ComparisonTrait
* @param \Cake\Chronos\ChronosInterface $dt The instance to compare with.
* @return bool
*/
public function ne(ChronosInterface $dt)
public function ne(ChronosInterface $dt): bool
{
return !$this->eq($dt);
}
@@ -100,7 +102,7 @@ trait ComparisonTrait
* @param \Cake\Chronos\ChronosInterface $dt The instance to compare with.
* @return bool
*/
public function gt(ChronosInterface $dt)
public function gt(ChronosInterface $dt): bool
{
return $this > $dt;
}
@@ -122,7 +124,7 @@ trait ComparisonTrait
* @param \Cake\Chronos\ChronosInterface $dt The instance to compare with.
* @return bool
*/
public function gte(ChronosInterface $dt)
public function gte(ChronosInterface $dt): bool
{
return $this >= $dt;
}
@@ -144,7 +146,7 @@ trait ComparisonTrait
* @param \Cake\Chronos\ChronosInterface $dt The instance to compare with.
* @return bool
*/
public function lt(ChronosInterface $dt)
public function lt(ChronosInterface $dt): bool
{
return $this < $dt;
}
@@ -166,7 +168,7 @@ trait ComparisonTrait
* @param \Cake\Chronos\ChronosInterface $dt The instance to compare with.
* @return bool
*/
public function lte(ChronosInterface $dt)
public function lte(ChronosInterface $dt): bool
{
return $this <= $dt;
}
@@ -190,7 +192,7 @@ trait ComparisonTrait
* @param bool $equal Indicates if a > and < comparison should be used or <= or >=
* @return bool
*/
public function between(ChronosInterface $dt1, ChronosInterface $dt2, $equal = true)
public function between(ChronosInterface $dt1, ChronosInterface $dt2, bool $equal = true): bool
{
if ($dt1->gt($dt2)) {
$temp = $dt1;
@@ -212,7 +214,7 @@ trait ComparisonTrait
* @param \Cake\Chronos\ChronosInterface $dt2 The instance to compare with.
* @return \Cake\Chronos\ChronosInterface
*/
public function closest(ChronosInterface $dt1, ChronosInterface $dt2)
public function closest(ChronosInterface $dt1, ChronosInterface $dt2): ChronosInterface
{
return $this->diffInSeconds($dt1) < $this->diffInSeconds($dt2) ? $dt1 : $dt2;
}
@@ -224,7 +226,7 @@ trait ComparisonTrait
* @param \Cake\Chronos\ChronosInterface $dt2 The instance to compare with.
* @return \Cake\Chronos\ChronosInterface
*/
public function farthest(ChronosInterface $dt1, ChronosInterface $dt2)
public function farthest(ChronosInterface $dt1, ChronosInterface $dt2): ChronosInterface
{
return $this->diffInSeconds($dt1) > $this->diffInSeconds($dt2) ? $dt1 : $dt2;
}
@@ -235,9 +237,9 @@ trait ComparisonTrait
* @param \Cake\Chronos\ChronosInterface|null $dt The instance to compare with.
* @return \Cake\Chronos\ChronosInterface
*/
public function min(ChronosInterface $dt = null)
public function min(?ChronosInterface $dt = null): ChronosInterface
{
$dt = ($dt === null) ? static::now($this->tz) : $dt;
$dt = $dt ?? static::now($this->tz);
return $this->lt($dt) ? $this : $dt;
}
@@ -248,9 +250,9 @@ trait ComparisonTrait
* @param \Cake\Chronos\ChronosInterface|null $dt The instance to compare with.
* @return \Cake\Chronos\ChronosInterface
*/
public function max(ChronosInterface $dt = null)
public function max(?ChronosInterface $dt = null): ChronosInterface
{
$dt = ($dt === null) ? static::now($this->tz) : $dt;
$dt = $dt ?? static::now($this->tz);
return $this->gt($dt) ? $this : $dt;
}
@@ -260,7 +262,7 @@ trait ComparisonTrait
*
* @return bool
*/
public function isWeekday()
public function isWeekday(): bool
{
return !$this->isWeekend();
}
@@ -270,7 +272,7 @@ trait ComparisonTrait
*
* @return bool
*/
public function isWeekend()
public function isWeekend(): bool
{
return in_array($this->dayOfWeek, self::$weekendDays, true);
}
@@ -280,7 +282,7 @@ trait ComparisonTrait
*
* @return bool
*/
public function isYesterday()
public function isYesterday(): bool
{
return $this->toDateString() === static::yesterday($this->tz)->toDateString();
}
@@ -290,7 +292,7 @@ trait ComparisonTrait
*
* @return bool
*/
public function isToday()
public function isToday(): bool
{
return $this->toDateString() === static::now($this->tz)->toDateString();
}
@@ -300,7 +302,7 @@ trait ComparisonTrait
*
* @return bool
*/
public function isTomorrow()
public function isTomorrow(): bool
{
return $this->toDateString() === static::tomorrow($this->tz)->toDateString();
}
@@ -310,7 +312,7 @@ trait ComparisonTrait
*
* @return bool
*/
public function isNextWeek()
public function isNextWeek(): bool
{
return $this->format('W o') === static::now($this->tz)->addWeek()->format('W o');
}
@@ -320,7 +322,7 @@ trait ComparisonTrait
*
* @return bool
*/
public function isLastWeek()
public function isLastWeek(): bool
{
return $this->format('W o') === static::now($this->tz)->subWeek()->format('W o');
}
@@ -330,7 +332,7 @@ trait ComparisonTrait
*
* @return bool
*/
public function isNextMonth()
public function isNextMonth(): bool
{
return $this->format('m Y') === static::now($this->tz)->addMonth()->format('m Y');
}
@@ -340,7 +342,7 @@ trait ComparisonTrait
*
* @return bool
*/
public function isLastMonth()
public function isLastMonth(): bool
{
return $this->format('m Y') === static::now($this->tz)->subMonth()->format('m Y');
}
@@ -350,7 +352,7 @@ trait ComparisonTrait
*
* @return bool
*/
public function isNextYear()
public function isNextYear(): bool
{
return $this->year === static::now($this->tz)->addYear()->year;
}
@@ -360,7 +362,7 @@ trait ComparisonTrait
*
* @return bool
*/
public function isLastYear()
public function isLastYear(): bool
{
return $this->year === static::now($this->tz)->subYear()->year;
}
@@ -370,7 +372,7 @@ trait ComparisonTrait
*
* @return bool
*/
public function isFuture()
public function isFuture(): bool
{
return $this->gt(static::now($this->tz));
}
@@ -380,7 +382,7 @@ trait ComparisonTrait
*
* @return bool
*/
public function isPast()
public function isPast(): bool
{
return $this->lt(static::now($this->tz));
}
@@ -390,7 +392,7 @@ trait ComparisonTrait
*
* @return bool
*/
public function isLeapYear()
public function isLeapYear(): bool
{
return $this->format('L') === '1';
}
@@ -401,7 +403,7 @@ trait ComparisonTrait
* @param \Cake\Chronos\ChronosInterface $dt The instance to check against.
* @return bool
*/
public function isSameDay(ChronosInterface $dt)
public function isSameDay(ChronosInterface $dt): bool
{
return $this->toDateString() === $dt->toDateString();
}
@@ -411,7 +413,7 @@ trait ComparisonTrait
*
* @return bool
*/
public function isSunday()
public function isSunday(): bool
{
return $this->dayOfWeek === ChronosInterface::SUNDAY;
}
@@ -421,7 +423,7 @@ trait ComparisonTrait
*
* @return bool
*/
public function isMonday()
public function isMonday(): bool
{
return $this->dayOfWeek === ChronosInterface::MONDAY;
}
@@ -431,7 +433,7 @@ trait ComparisonTrait
*
* @return bool
*/
public function isTuesday()
public function isTuesday(): bool
{
return $this->dayOfWeek === ChronosInterface::TUESDAY;
}
@@ -441,7 +443,7 @@ trait ComparisonTrait
*
* @return bool
*/
public function isWednesday()
public function isWednesday(): bool
{
return $this->dayOfWeek === ChronosInterface::WEDNESDAY;
}
@@ -451,7 +453,7 @@ trait ComparisonTrait
*
* @return bool
*/
public function isThursday()
public function isThursday(): bool
{
return $this->dayOfWeek === ChronosInterface::THURSDAY;
}
@@ -461,7 +463,7 @@ trait ComparisonTrait
*
* @return bool
*/
public function isFriday()
public function isFriday(): bool
{
return $this->dayOfWeek === ChronosInterface::FRIDAY;
}
@@ -471,7 +473,7 @@ trait ComparisonTrait
*
* @return bool
*/
public function isSaturday()
public function isSaturday(): bool
{
return $this->dayOfWeek === ChronosInterface::SATURDAY;
}
@@ -481,7 +483,7 @@ trait ComparisonTrait
*
* @return bool
*/
public function isThisWeek()
public function isThisWeek(): bool
{
return static::now($this->getTimezone())->format('W o') === $this->format('W o');
}
@@ -491,7 +493,7 @@ trait ComparisonTrait
*
* @return bool
*/
public function isThisMonth()
public function isThisMonth(): bool
{
return static::now($this->getTimezone())->format('m Y') === $this->format('m Y');
}
@@ -501,7 +503,7 @@ trait ComparisonTrait
*
* @return bool
*/
public function isThisYear()
public function isThisYear(): bool
{
return static::now($this->getTimezone())->format('Y') === $this->format('Y');
}
@@ -512,11 +514,9 @@ trait ComparisonTrait
* @param \Cake\Chronos\ChronosInterface|null $dt The instance to compare with or null to use current day.
* @return bool
*/
public function isBirthday(ChronosInterface $dt = null)
public function isBirthday(?ChronosInterface $dt = null): bool
{
if ($dt === null) {
$dt = static::now($this->tz);
}
$dt = $dt ?? static::now($this->tz);
return $this->format('md') === $dt->format('md');
}
@@ -528,7 +528,7 @@ trait ComparisonTrait
* Example of valid types: 6 hours, 2 days, 1 minute.
* @return bool
*/
public function wasWithinLast($timeInterval)
public function wasWithinLast($timeInterval): bool
{
$now = new static();
$interval = $now->copy()->modify('-' . $timeInterval);
@@ -544,7 +544,7 @@ trait ComparisonTrait
* Example of valid types: 6 hours, 2 days, 1 minute.
* @return bool
*/
public function isWithinNext($timeInterval)
public function isWithinNext($timeInterval): bool
{
$now = new static();
$interval = $now->copy()->modify('+' . $timeInterval);
@@ -558,7 +558,7 @@ trait ComparisonTrait
*
* @return bool
*/
public function isMutable()
public function isMutable(): bool
{
return $this instanceof DateTime;
}
+5 -1
View File
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
@@ -12,6 +14,8 @@
*/
namespace Cake\Chronos\Traits;
use Cake\Chronos\ChronosInterface;
/**
* Provides methods for copying datetime objects.
*
@@ -24,7 +28,7 @@ trait CopyTrait
*
* @return static
*/
public function copy()
public function copy(): ChronosInterface
{
return static::instance($this);
}
+41 -35
View File
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
@@ -15,8 +17,8 @@ namespace Cake\Chronos\Traits;
use Cake\Chronos\ChronosInterface;
use Cake\Chronos\ChronosInterval;
use Cake\Chronos\DifferenceFormatter;
use Cake\Chronos\DifferenceFormatterInterface;
use DatePeriod;
use DateTimeImmutable;
use DateTimeInterface;
/**
@@ -33,7 +35,7 @@ trait DifferenceTrait
/**
* Instance of the diff formatting object.
*
* @var \Cake\Chronos\DifferenceFormatter
* @var \Cake\Chronos\DifferenceFormatterInterface
*/
protected static $diffFormatter;
@@ -44,11 +46,11 @@ trait DifferenceTrait
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffInYears(ChronosInterface $dt = null, $abs = true)
public function diffInYears(?ChronosInterface $dt = null, bool $abs = true): int
{
$dt = $dt === null ? static::now($this->tz) : $dt;
$diff = $this->diff($dt ?? static::now($this->tz), $abs);
return (int)$this->diff($dt, $abs)->format('%r%y');
return $diff->invert ? -$diff->y : $diff->y;
}
/**
@@ -58,11 +60,12 @@ trait DifferenceTrait
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffInMonths(ChronosInterface $dt = null, $abs = true)
public function diffInMonths(?ChronosInterface $dt = null, bool $abs = true): int
{
$dt = $dt === null ? static::now($this->tz) : $dt;
$diff = $this->diff($dt ?? static::now($this->tz), $abs);
$months = $diff->y * ChronosInterface::MONTHS_PER_YEAR + $diff->m;
return $this->diffInYears($dt, $abs) * ChronosInterface::MONTHS_PER_YEAR + (int)$this->diff($dt, $abs)->format('%r%m');
return $diff->invert ? -$months : $months;
}
/**
@@ -72,7 +75,7 @@ trait DifferenceTrait
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffInWeeks(ChronosInterface $dt = null, $abs = true)
public function diffInWeeks(?ChronosInterface $dt = null, bool $abs = true): int
{
return (int)($this->diffInDays($dt, $abs) / ChronosInterface::DAYS_PER_WEEK);
}
@@ -84,11 +87,11 @@ trait DifferenceTrait
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffInDays(ChronosInterface $dt = null, $abs = true)
public function diffInDays(?ChronosInterface $dt = null, bool $abs = true): int
{
$dt = $dt === null ? static::now($this->tz) : $dt;
$diff = $this->diff($dt ?? static::now($this->tz), $abs);
return (int)$this->diff($dt, $abs)->format('%r%a');
return $diff->invert ? -$diff->days : $diff->days;
}
/**
@@ -99,7 +102,7 @@ trait DifferenceTrait
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffInDaysFiltered(callable $callback, ChronosInterface $dt = null, $abs = true)
public function diffInDaysFiltered(callable $callback, ?ChronosInterface $dt = null, bool $abs = true): int
{
return $this->diffFiltered(ChronosInterval::day(), $callback, $dt, $abs);
}
@@ -112,7 +115,7 @@ trait DifferenceTrait
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffInHoursFiltered(callable $callback, ChronosInterface $dt = null, $abs = true)
public function diffInHoursFiltered(callable $callback, ?ChronosInterface $dt = null, bool $abs = true): int
{
return $this->diffFiltered(ChronosInterval::hour(), $callback, $dt, $abs);
}
@@ -126,17 +129,16 @@ trait DifferenceTrait
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffFiltered(ChronosInterval $ci, callable $callback, ChronosInterface $dt = null, $abs = true)
{
public function diffFiltered(
ChronosInterval $ci,
callable $callback,
?ChronosInterface $dt = null,
bool $abs = true
): int {
$start = $this;
$end = $dt === null ? static::now($this->tz) : $dt;
$end = $dt ?? static::now($this->tz);
$inverse = false;
if (defined('HHVM_VERSION')) {
$start = new DateTimeImmutable($this->toIso8601String());
$end = new DateTimeImmutable($end->toIso8601String());
}
if ($end < $start) {
$start = $end;
$end = $this;
@@ -160,7 +162,7 @@ trait DifferenceTrait
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffInWeekdays(ChronosInterface $dt = null, $abs = true)
public function diffInWeekdays(?ChronosInterface $dt = null, bool $abs = true): int
{
return $this->diffInDaysFiltered(function (ChronosInterface $date) {
return $date->isWeekday();
@@ -174,7 +176,7 @@ trait DifferenceTrait
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffInWeekendDays(ChronosInterface $dt = null, $abs = true)
public function diffInWeekendDays(?ChronosInterface $dt = null, bool $abs = true): int
{
return $this->diffInDaysFiltered(function (ChronosInterface $date) {
return $date->isWeekend();
@@ -188,9 +190,13 @@ trait DifferenceTrait
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffInHours(ChronosInterface $dt = null, $abs = true)
public function diffInHours(?ChronosInterface $dt = null, bool $abs = true): int
{
return (int)($this->diffInSeconds($dt, $abs) / ChronosInterface::SECONDS_PER_MINUTE / ChronosInterface::MINUTES_PER_HOUR);
return (int)(
$this->diffInSeconds($dt, $abs)
/ ChronosInterface::SECONDS_PER_MINUTE
/ ChronosInterface::MINUTES_PER_HOUR
);
}
/**
@@ -200,7 +206,7 @@ trait DifferenceTrait
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffInMinutes(ChronosInterface $dt = null, $abs = true)
public function diffInMinutes(?ChronosInterface $dt = null, bool $abs = true): int
{
return (int)($this->diffInSeconds($dt, $abs) / ChronosInterface::SECONDS_PER_MINUTE);
}
@@ -212,9 +218,9 @@ trait DifferenceTrait
* @param bool $abs Get the absolute of the difference
* @return int
*/
public function diffInSeconds(ChronosInterface $dt = null, $abs = true)
public function diffInSeconds(?ChronosInterface $dt = null, bool $abs = true): int
{
$dt = ($dt === null) ? static::now($this->tz) : $dt;
$dt = $dt ?? static::now($this->tz);
$value = $dt->getTimestamp() - $this->getTimestamp();
return $abs ? abs($value) : $value;
@@ -225,7 +231,7 @@ trait DifferenceTrait
*
* @return int
*/
public function secondsSinceMidnight()
public function secondsSinceMidnight(): int
{
return $this->diffInSeconds($this->copy()->startOfDay());
}
@@ -235,7 +241,7 @@ trait DifferenceTrait
*
* @return int
*/
public function secondsUntilEndOfDay()
public function secondsUntilEndOfDay(): int
{
return $this->diffInSeconds($this->copy()->endOfDay());
}
@@ -276,7 +282,7 @@ trait DifferenceTrait
* @param bool $absolute removes time difference modifiers ago, after, etc
* @return string
*/
public function diffForHumans(ChronosInterface $other = null, $absolute = false)
public function diffForHumans(?ChronosInterface $other = null, bool $absolute = false): string
{
return static::diffFormatter()->diffForHumans($this, $other, $absolute);
}
@@ -284,10 +290,10 @@ trait DifferenceTrait
/**
* Get the difference formatter instance or overwrite the current one.
*
* @param \Cake\Chronos\DifferenceFormatter|null $formatter The formatter instance when setting.
* @return \Cake\Chronos\DifferenceFormatter The formatter instance.
* @param \Cake\Chronos\DifferenceFormatterInterface|null $formatter The formatter instance when setting.
* @return \Cake\Chronos\DifferenceFormatterInterface The formatter instance.
*/
public static function diffFormatter($formatter = null)
public static function diffFormatter(?DifferenceFormatterInterface $formatter = null): DifferenceFormatterInterface
{
if ($formatter === null) {
if (static::$diffFormatter === null) {
+69 -40
View File
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
@@ -12,6 +14,7 @@
*/
namespace Cake\Chronos\Traits;
use Cake\Chronos\ChronosInterface;
use DateTimeInterface;
use DateTimeZone;
use InvalidArgumentException;
@@ -34,7 +37,7 @@ trait FactoryTrait
* @param \DateTimeInterface $dt The datetime instance to convert.
* @return static
*/
public static function instance(DateTimeInterface $dt)
public static function instance(DateTimeInterface $dt): ChronosInterface
{
if ($dt instanceof static) {
return clone $dt;
@@ -49,11 +52,11 @@ trait FactoryTrait
* ChronosInterface::parse('Monday next week')->fn() rather than
* (new Chronos('Monday next week'))->fn()
*
* @param \DateTimeInterface|string $time The strtotime compatible string to parse
* @param \DateTimeInterface|string|int $time The strtotime compatible string to parse
* @param \DateTimeZone|string|null $tz The DateTimeZone object or timezone name.
* @return static
*/
public static function parse($time = 'now', $tz = null)
public static function parse($time = 'now', $tz = null): ChronosInterface
{
return new static($time, $tz);
}
@@ -64,7 +67,7 @@ trait FactoryTrait
* @param \DateTimeZone|string|null $tz The DateTimeZone object or timezone name.
* @return static
*/
public static function now($tz = null)
public static function now($tz = null): ChronosInterface
{
return new static('now', $tz);
}
@@ -75,7 +78,7 @@ trait FactoryTrait
* @param \DateTimeZone|string|null $tz The timezone to use.
* @return static
*/
public static function today($tz = null)
public static function today($tz = null): ChronosInterface
{
return new static('midnight', $tz);
}
@@ -86,7 +89,7 @@ trait FactoryTrait
* @param \DateTimeZone|string|null $tz The DateTimeZone object or timezone name the new instance should use.
* @return static
*/
public static function tomorrow($tz = null)
public static function tomorrow($tz = null): ChronosInterface
{
return new static('tomorrow, midnight', $tz);
}
@@ -97,7 +100,7 @@ trait FactoryTrait
* @param \DateTimeZone|string|null $tz The DateTimeZone object or timezone name the new instance should use.
* @return static
*/
public static function yesterday($tz = null)
public static function yesterday($tz = null): ChronosInterface
{
return new static('yesterday, midnight', $tz);
}
@@ -107,7 +110,7 @@ trait FactoryTrait
*
* @return \Cake\Chronos\ChronosInterface
*/
public static function maxValue()
public static function maxValue(): ChronosInterface
{
return static::createFromTimestampUTC(PHP_INT_MAX);
}
@@ -117,7 +120,7 @@ trait FactoryTrait
*
* @return \Cake\Chronos\ChronosInterface
*/
public static function minValue()
public static function minValue(): ChronosInterface
{
$max = PHP_INT_SIZE === 4 ? PHP_INT_MAX : PHP_INT_MAX / 10;
@@ -131,9 +134,9 @@ trait FactoryTrait
* will be used.
*
* If $hour is null it will be set to its now() value and the default values
* for $minute and $second will be their now() values.
* If $hour is not null then the default values for $minute and $second
* will be 0.
* for $minute, $second and $microsecond will be their now() values.
* If $hour is not null then the default values for $minute, $second
* and $microsecond will be 0.
*
* @param int|null $year The year to create an instance with.
* @param int|null $month The month to create an instance with.
@@ -141,25 +144,41 @@ trait FactoryTrait
* @param int|null $hour The hour to create an instance with.
* @param int|null $minute The minute to create an instance with.
* @param int|null $second The second to create an instance with.
* @param int|null $microsecond The microsecond to create an instance with.
* @param \DateTimeZone|string|null $tz The DateTimeZone object or timezone name the new instance should use.
* @return static
*/
public static function create($year = null, $month = null, $day = null, $hour = null, $minute = null, $second = null, $tz = null)
{
$year = ($year === null) ? date('Y') : $year;
$month = ($month === null) ? date('n') : $month;
$day = ($day === null) ? date('j') : $day;
public static function create(
?int $year = null,
?int $month = null,
?int $day = null,
?int $hour = null,
?int $minute = null,
?int $second = null,
?int $microsecond = null,
$tz = null
): ChronosInterface {
$now = static::now();
$year = $year ?? (int)$now->format('Y');
$month = $month ?? $now->format('m');
$day = $day ?? $now->format('d');
if ($hour === null) {
$hour = date('G');
$minute = ($minute === null) ? date('i') : $minute;
$second = ($second === null) ? date('s') : $second;
$hour = $now->format('H');
$minute = $minute ?? $now->format('i');
$second = $second ?? $now->format('s');
$microsecond = $microsecond ?? $now->format('u');
} else {
$minute = ($minute === null) ? 0 : $minute;
$second = ($second === null) ? 0 : $second;
$minute = $minute ?? 0;
$second = $second ?? 0;
$microsecond = $microsecond ?? 0;
}
$instance = static::createFromFormat('Y-n-j G:i:s', sprintf('%s-%s-%s %s:%02s:%02s', 0, $month, $day, $hour, $minute, $second), $tz);
$instance = static::createFromFormat(
'Y-m-d H:i:s.u',
sprintf('%s-%s-%s %s:%02s:%02s.%06s', 0, $month, $day, $hour, $minute, $second, $microsecond),
$tz
);
return $instance->addYears($year);
}
@@ -167,15 +186,19 @@ trait FactoryTrait
/**
* Create a ChronosInterface instance from just a date. The time portion is set to now.
*
* @param int $year The year to create an instance with.
* @param int $month The month to create an instance with.
* @param int $day The day to create an instance with.
* @param int|null $year The year to create an instance with.
* @param int|null $month The month to create an instance with.
* @param int|null $day The day to create an instance with.
* @param \DateTimeZone|string|null $tz The DateTimeZone object or timezone name the new instance should use.
* @return static
*/
public static function createFromDate($year = null, $month = null, $day = null, $tz = null)
{
return static::create($year, $month, $day, null, null, null, $tz);
public static function createFromDate(
?int $year = null,
?int $month = null,
?int $day = null,
$tz = null
): ChronosInterface {
return static::create($year, $month, $day, null, null, null, null, $tz);
}
/**
@@ -184,12 +207,18 @@ trait FactoryTrait
* @param int|null $hour The hour to create an instance with.
* @param int|null $minute The minute to create an instance with.
* @param int|null $second The second to create an instance with.
* @param int|null $microsecond The microsecond to create an instance with.
* @param \DateTimeZone|string|null $tz The DateTimeZone object or timezone name the new instance should use.
* @return static
*/
public static function createFromTime($hour = null, $minute = null, $second = null, $tz = null)
{
return static::create(null, null, null, $hour, $minute, $second, $tz);
public static function createFromTime(
?int $hour = null,
?int $minute = null,
?int $second = null,
?int $microsecond = null,
$tz = null
): ChronosInterface {
return static::create(null, null, null, $hour, $minute, $second, $microsecond, $tz);
}
/**
@@ -201,7 +230,7 @@ trait FactoryTrait
* @return static
* @throws \InvalidArgumentException
*/
public static function createFromFormat($format, $time, $tz = null)
public static function createFromFormat($format, $time, $tz = null): ChronosInterface
{
if ($tz !== null) {
$dt = parent::createFromFormat($format, $time, static::safeCreateDateTimeZone($tz));
@@ -210,11 +239,11 @@ trait FactoryTrait
}
$errors = parent::getLastErrors();
if ($dt == false) {
if (!$dt) {
throw new InvalidArgumentException(implode(PHP_EOL, $errors['errors']));
}
$dt = static::instance($dt);
$dt = new static($dt->format('Y-m-d H:i:s.u'), $dt->getTimezone());
static::$_lastErrors = $errors;
return $dt;
@@ -242,7 +271,7 @@ trait FactoryTrait
* @param (int|string)[] $values Array of date and time values.
* @return static
*/
public static function createFromArray($values)
public static function createFromArray(array $values): ChronosInterface
{
$values += ['hour' => 0, 'minute' => 0, 'second' => 0, 'microsecond' => 0, 'timezone' => null];
@@ -282,7 +311,7 @@ trait FactoryTrait
* @param \DateTimeZone|string|null $tz The DateTimeZone object or timezone name the new instance should use.
* @return static
*/
public static function createFromTimestamp($timestamp, $tz = null)
public static function createFromTimestamp(int $timestamp, $tz = null): ChronosInterface
{
return static::now($tz)->setTimestamp($timestamp);
}
@@ -293,7 +322,7 @@ trait FactoryTrait
* @param int $timestamp The UTC timestamp to create an instance from.
* @return static
*/
public static function createFromTimestampUTC($timestamp)
public static function createFromTimestampUTC(int $timestamp): ChronosInterface
{
return new static('@' . $timestamp);
}
@@ -305,7 +334,7 @@ trait FactoryTrait
* @return \DateTimeZone
* @throws \InvalidArgumentException
*/
protected static function safeCreateDateTimeZone($object)
protected static function safeCreateDateTimeZone($object): DateTimeZone
{
if ($object === null) {
return new DateTimeZone(date_default_timezone_get());
@@ -324,7 +353,7 @@ trait FactoryTrait
*
* @return array
*/
public static function getLastErrors()
public static function getLastErrors(): array
{
if (empty(static::$_lastErrors)) {
return parent::getLastErrors();
+27 -25
View File
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
@@ -27,7 +29,7 @@ trait FormattingTrait
*
* @return void
*/
public static function resetToStringFormat()
public static function resetToStringFormat(): void
{
static::setToStringFormat(ChronosInterface::DEFAULT_TO_STRING_FORMAT);
}
@@ -38,7 +40,7 @@ trait FormattingTrait
* @param string $format The format to use in future __toString() calls.
* @return void
*/
public static function setToStringFormat($format)
public static function setToStringFormat($format): void
{
static::$toStringFormat = $format;
}
@@ -48,7 +50,7 @@ trait FormattingTrait
*
* @return string
*/
public function __toString()
public function __toString(): string
{
return $this->format(static::$toStringFormat);
}
@@ -58,7 +60,7 @@ trait FormattingTrait
*
* @return string
*/
public function toDateString()
public function toDateString(): string
{
return $this->format('Y-m-d');
}
@@ -68,7 +70,7 @@ trait FormattingTrait
*
* @return string
*/
public function toFormattedDateString()
public function toFormattedDateString(): string
{
return $this->format('M j, Y');
}
@@ -78,7 +80,7 @@ trait FormattingTrait
*
* @return string
*/
public function toTimeString()
public function toTimeString(): string
{
return $this->format('H:i:s');
}
@@ -88,7 +90,7 @@ trait FormattingTrait
*
* @return string
*/
public function toDateTimeString()
public function toDateTimeString(): string
{
return $this->format('Y-m-d H:i:s');
}
@@ -98,7 +100,7 @@ trait FormattingTrait
*
* @return string
*/
public function toDayDateTimeString()
public function toDayDateTimeString(): string
{
return $this->format('D, M j, Y g:i A');
}
@@ -108,7 +110,7 @@ trait FormattingTrait
*
* @return string
*/
public function toAtomString()
public function toAtomString(): string
{
return $this->format(DateTime::ATOM);
}
@@ -118,7 +120,7 @@ trait FormattingTrait
*
* @return string
*/
public function toCookieString()
public function toCookieString(): string
{
return $this->format(DateTime::COOKIE);
}
@@ -128,7 +130,7 @@ trait FormattingTrait
*
* @return string
*/
public function toIso8601String()
public function toIso8601String(): string
{
return $this->format(DateTime::ATOM);
}
@@ -138,7 +140,7 @@ trait FormattingTrait
*
* @return string
*/
public function toRfc822String()
public function toRfc822String(): string
{
return $this->format(DateTime::RFC822);
}
@@ -148,7 +150,7 @@ trait FormattingTrait
*
* @return string
*/
public function toRfc850String()
public function toRfc850String(): string
{
return $this->format(DateTime::RFC850);
}
@@ -158,7 +160,7 @@ trait FormattingTrait
*
* @return string
*/
public function toRfc1036String()
public function toRfc1036String(): string
{
return $this->format(DateTime::RFC1036);
}
@@ -168,7 +170,7 @@ trait FormattingTrait
*
* @return string
*/
public function toRfc1123String()
public function toRfc1123String(): string
{
return $this->format(DateTime::RFC1123);
}
@@ -178,7 +180,7 @@ trait FormattingTrait
*
* @return string
*/
public function toRfc2822String()
public function toRfc2822String(): string
{
return $this->format(DateTime::RFC2822);
}
@@ -188,7 +190,7 @@ trait FormattingTrait
*
* @return string
*/
public function toRfc3339String()
public function toRfc3339String(): string
{
return $this->format(DateTime::RFC3339);
}
@@ -198,7 +200,7 @@ trait FormattingTrait
*
* @return string
*/
public function toRssString()
public function toRssString(): string
{
return $this->format(DateTime::RSS);
}
@@ -208,7 +210,7 @@ trait FormattingTrait
*
* @return string
*/
public function toW3cString()
public function toW3cString(): string
{
return $this->format(DateTime::W3C);
}
@@ -218,7 +220,7 @@ trait FormattingTrait
*
* @return string UNIX timestamp
*/
public function toUnixString()
public function toUnixString(): string
{
return $this->format('U');
}
@@ -229,9 +231,9 @@ trait FormattingTrait
* @param bool $range Range.
* @return int|array 1, 2, 3, or 4 quarter of year or array if $range true
*/
public function toQuarter($range = false)
public function toQuarter(bool $range = false)
{
$quarter = ceil($this->format('m') / 3);
$quarter = (int)ceil($this->format('m') / 3);
if ($range === false) {
return $quarter;
}
@@ -244,15 +246,15 @@ trait FormattingTrait
return [$year . '-04-01', $year . '-06-30'];
case 3:
return [$year . '-07-01', $year . '-09-30'];
case 4:
default:
return [$year . '-10-01', $year . '-12-31'];
}
}
/**
* @return int
* @inheritDoc
*/
public function toWeek()
public function toWeek(): int
{
return (int)$this->format('W');
}
+23 -26
View File
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
@@ -11,6 +13,8 @@
*/
namespace Cake\Chronos\Traits;
use Cake\Chronos\ChronosInterface;
use DateTimeImmutable;
use DateTimeInterface;
/**
@@ -27,29 +31,26 @@ trait FrozenTimeTrait
*
* Used to ensure constructed objects always lack time.
*
* @param string|int|\DateTimeInterface $time The input time. Integer values will be assumed
* @param \DateTime|\DateTimeImmutable|string|int|null $time The input time. Integer values will be assumed
* to be in UTC. The 'now' and '' values will use the current local time.
* @param \DateTimeZone|null $tz The timezone in which the date is taken
* @return string The date component of $time.
*/
protected function stripTime($time)
protected function stripTime($time, $tz): string
{
if (is_int($time) || ctype_digit($time)) {
if (is_int($time)) {
return gmdate('Y-m-d 00:00:00', $time);
}
if ($time instanceof DateTimeInterface) {
$time = $time->format('Y-m-d 00:00:00');
}
if (substr($time, 0, 1) === '@') {
return gmdate('Y-m-d 00:00:00', substr($time, 1));
}
if ($time === null || $time === 'now' || $time === '') {
return date('Y-m-d 00:00:00');
}
if ($this->hasRelativeKeywords($time)) {
return date('Y-m-d 00:00:00', strtotime($time));
if (is_string($time) && substr($time, 0, 1) === '@') {
return gmdate('Y-m-d 00:00:00', (int)substr($time, 1));
}
return preg_replace('/\d{1,2}:\d{1,2}:\d{1,2}(?:\.\d+)?/', '00:00:00', $time);
if (!($time instanceof DateTimeInterface)) {
$time = new DateTimeImmutable($time ?? 'now', $tz);
}
return $time->format('Y-m-d 00:00:00');
}
/**
@@ -58,7 +59,7 @@ trait FrozenTimeTrait
* @param string $time The input expression
* @return string The output expression with no time modifiers.
*/
protected function stripRelativeTime($time)
protected function stripRelativeTime(string $time): string
{
return preg_replace('/([-+]\s*\d+\s(?:minutes|seconds|hours|microseconds))/', '', $time);
}
@@ -74,13 +75,9 @@ trait FrozenTimeTrait
* @param int $microseconds The microseconds to set (ignored)
* @return static A modified Date instance.
*/
public function setTime($hours, $minutes, $seconds = null, $microseconds = null)
public function setTime($hours, $minutes, $seconds = null, $microseconds = null): ChronosInterface
{
if (CHRONOS_SUPPORTS_MICROSECONDS) {
return parent::setTime(0, 0, 0, 0);
}
return parent::setTime(0, 0, 0);
return parent::setTime(0, 0, 0, 0);
}
/**
@@ -91,7 +88,7 @@ trait FrozenTimeTrait
* @param \DateInterval $interval The interval to modify this date by.
* @return static A modified Date instance
*/
public function add($interval)
public function add($interval): ChronosInterface
{
return parent::add($interval)->setTime(0, 0, 0);
}
@@ -104,7 +101,7 @@ trait FrozenTimeTrait
* @param \DateInterval $interval The interval to modify this date by.
* @return static A modified Date instance
*/
public function sub($interval)
public function sub($interval): ChronosInterface
{
return parent::sub($interval)->setTime(0, 0, 0);
}
@@ -157,7 +154,7 @@ trait FrozenTimeTrait
* @param int $value The timestamp value to set.
* @return static
*/
public function setTimestamp($value)
public function setTimestamp($value): ChronosInterface
{
return parent::setTimestamp($value)->setTime(0, 0, 0);
}
@@ -171,7 +168,7 @@ trait FrozenTimeTrait
* @param string $relative The relative change to make.
* @return static A new date with the applied date changes.
*/
public function modify($relative)
public function modify($relative): ChronosInterface
{
if (preg_match('/hour|minute|second/', $relative)) {
return $this;
+12 -6
View File
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
@@ -38,13 +40,14 @@ use InvalidArgumentException;
* @property-read int $quarter
* @property-read int $offset
* @property-read int $offsetHours
* @property-read boolean $dst
* @property-read boolean $local
* @property-read boolean $utc
* @property-read bool $dst
* @property-read bool $local
* @property-read bool $utc
* @property-read \DateTimeZone $timezone
* @property-read \DateTimeZone $tz
* @property-read string $timezoneName
* @property-read string $tzName
* @property-read string $dayOfWeekName
*/
trait MagicPropertyTrait
{
@@ -52,10 +55,10 @@ trait MagicPropertyTrait
* Get a part of the ChronosInterface object
*
* @param string $name The property name to read.
* @return mixed The property value.
* @return string|int|bool|\DateTimeZone The property value.
* @throws \InvalidArgumentException
*/
public function __get($name)
public function __get(string $name)
{
static $formats = [
'year' => 'Y',
@@ -78,6 +81,9 @@ trait MagicPropertyTrait
case isset($formats[$name]):
return (int)$this->format($formats[$name]);
case $name === 'dayOfWeekName':
return $this->format('l');
case $name === 'weekOfMonth':
return (int)ceil($this->day / ChronosInterface::DAYS_PER_WEEK);
@@ -119,7 +125,7 @@ trait MagicPropertyTrait
* @param string $name The property name to check.
* @return bool Whether or not the property exists.
*/
public function __isset($name)
public function __isset(string $name): bool
{
try {
$this->__get($name);
+245 -201
View File
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
@@ -13,8 +15,6 @@
namespace Cake\Chronos\Traits;
use Cake\Chronos\ChronosInterface;
use DateTime;
use DateTimeImmutable;
/**
* Provides a suite of modifier methods.
@@ -28,7 +28,6 @@ use DateTimeImmutable;
*/
trait ModifierTrait
{
/**
* Names of days of the week.
*
@@ -63,7 +62,7 @@ trait ModifierTrait
*
* @return int
*/
public static function getWeekStartsAt()
public static function getWeekStartsAt(): int
{
return static::$weekStartsAt;
}
@@ -74,7 +73,7 @@ trait ModifierTrait
* @param int $day The day the week starts with.
* @return void
*/
public static function setWeekStartsAt($day)
public static function setWeekStartsAt(int $day): void
{
static::$weekStartsAt = $day;
}
@@ -84,7 +83,7 @@ trait ModifierTrait
*
* @return int
*/
public static function getWeekEndsAt()
public static function getWeekEndsAt(): int
{
return static::$weekEndsAt;
}
@@ -95,7 +94,7 @@ trait ModifierTrait
* @param int $day The day the week ends with.
* @return void
*/
public static function setWeekEndsAt($day)
public static function setWeekEndsAt(int $day): void
{
static::$weekEndsAt = $day;
}
@@ -109,9 +108,8 @@ trait ModifierTrait
* @param int $month The month to set.
* @param int $day The day to set.
* @return static
* @see https://bugs.php.net/bug.php?id=63863
*/
public function setDate($year, $month, $day)
public function setDate($year, $month, $day): ChronosInterface
{
return $this->modify('+0 day')->setDateParent($year, $month, $day);
}
@@ -125,7 +123,7 @@ trait ModifierTrait
* @param int $day The day to set.
* @return static
*/
private function setDateParent($year, $month, $day)
private function setDateParent(int $year, int $month, int $day): ChronosInterface
{
return parent::setDate($year, $month, $day);
}
@@ -141,8 +139,14 @@ trait ModifierTrait
* @param int $second The second to set.
* @return static
*/
public function setDateTime($year, $month, $day, $hour, $minute, $second = 0)
{
public function setDateTime(
int $year,
int $month,
int $day,
int $hour,
int $minute,
int $second = 0
): ChronosInterface {
return $this->setDate($year, $month, $day)->setTime($hour, $minute, $second);
}
@@ -152,14 +156,14 @@ trait ModifierTrait
* @param string $time Time as string.
* @return static
*/
public function setTimeFromTimeString($time)
public function setTimeFromTimeString(string $time): ChronosInterface
{
$time = explode(":", $time);
$hour = (int)$time[0];
$minute = isset($time[1]) ? (int)$time[1] : 0;
$second = isset($time[2]) ? (int)$time[2] : 0;
$time = explode(':', $time);
$hour = $time[0];
$minute = $time[1] ?? 0;
$second = $time[2] ?? 0;
return $this->setTime($hour, $minute, $second);
return $this->setTime((int)$hour, (int)$minute, (int)$second);
}
/**
@@ -168,7 +172,7 @@ trait ModifierTrait
* @param int $value The timestamp value to set.
* @return static
*/
public function timestamp($value)
public function timestamp(int $value): ChronosInterface
{
return $this->setTimestamp($value);
}
@@ -179,7 +183,7 @@ trait ModifierTrait
* @param int $value The year value.
* @return static
*/
public function year($value)
public function year(int $value): ChronosInterface
{
return $this->setDate($value, $this->month, $this->day);
}
@@ -190,7 +194,7 @@ trait ModifierTrait
* @param int $value The month value.
* @return static
*/
public function month($value)
public function month(int $value): ChronosInterface
{
return $this->setDate($this->year, $value, $this->day);
}
@@ -201,7 +205,7 @@ trait ModifierTrait
* @param int $value The day value.
* @return static
*/
public function day($value)
public function day(int $value): ChronosInterface
{
return $this->setDate($this->year, $this->month, $value);
}
@@ -212,7 +216,7 @@ trait ModifierTrait
* @param int $value The hour value.
* @return static
*/
public function hour($value)
public function hour(int $value): ChronosInterface
{
return $this->setTime($value, $this->minute, $this->second);
}
@@ -223,7 +227,7 @@ trait ModifierTrait
* @param int $value The minute value.
* @return static
*/
public function minute($value)
public function minute(int $value): ChronosInterface
{
return $this->setTime($this->hour, $value, $this->second);
}
@@ -234,7 +238,7 @@ trait ModifierTrait
* @param int $value The seconds value.
* @return static
*/
public function second($value)
public function second(int $value): ChronosInterface
{
return $this->setTime($this->hour, $this->minute, $value);
}
@@ -243,45 +247,128 @@ trait ModifierTrait
* Add years to the instance. Positive $value travel forward while
* negative $value travel into the past.
*
* @param int $value The number of years to add.
* @return static
*/
public function addYears($value)
{
return $this->modify((int)$value . ' year');
}
/**
* Add a year to the instance
* If the new date does not exist, the last day of the month is used
* instead instead of overflowing into the next month.
*
* ### Example:
*
* ```
* (new Chronos('2015-01-03'))->addYears(1); // Results in 2016-01-03
*
* (new Chronos('2012-02-29'))->addYears(1); // Results in 2013-02-28
* ```
*
* @param int $value The number of years to add.
* @return static
*/
public function addYear($value = 1)
public function addYears(int $value): ChronosInterface
{
$month = $this->month;
$date = $this->modify($value . ' year');
if ($date->month !== $month) {
return $date->modify('last day of previous month');
}
return $date;
}
/**
* Add a year to the instance.
*
* Has the same behavior as `addYears()`.
*
* @param int $value The number of years to add.
* @return static
*/
public function addYear(int $value = 1): ChronosInterface
{
return $this->addYears($value);
}
/**
* Remove a year from the instance
* Remove years from the instance.
*
* Has the same behavior as `addYears()`.
*
* @param int $value The number of years to remove.
* @return static
*/
public function subYear($value = 1)
public function subYears(int $value): ChronosInterface
{
return $this->addYears(-1 * $value);
}
/**
* Remove a year from the instance
*
* Has the same behavior as `addYears()`.
*
* @param int $value The number of years to remove.
* @return static
*/
public function subYear(int $value = 1): ChronosInterface
{
return $this->subYears($value);
}
/**
* Remove years from the instance.
* Add years with overflowing to the instance. Positive $value
* travels forward while negative $value travels into the past.
*
* If the new date does not exist, the days overflow into the next month.
*
* ### Example:
*
* ```
* (new Chronos('2012-02-29'))->addYearsWithOverflow(1); // Results in 2013-03-01
* ```
*
* @param int $value The number of years to add.
* @return static
*/
public function addYearsWithOverflow(int $value): ChronosInterface
{
return $this->modify($value . ' year');
}
/**
* Add a year with overflow to the instance.
*
* Has the same behavior as `addYearsWithOverflow()`.
*
* @param int $value The number of years to add.
* @return static
*/
public function addYearWithOverflow(int $value = 1): ChronosInterface
{
return $this->addYearsWithOverflow($value);
}
/**
* Remove years with overflow from the instance
*
* Has the same behavior as `addYeasrWithOverflow()`.
*
* @param int $value The number of years to remove.
* @return static
*/
public function subYears($value)
public function subYearsWithOverflow(int $value): ChronosInterface
{
return $this->addYears(-1 * $value);
return $this->addYearsWithOverflow(-1 * $value);
}
/**
* Remove a year with overflow from the instance.
*
* Has the same behavior as `addYearsWithOverflow()`.
*
* @param int $value The number of years to remove.
* @return static
*/
public function subYearWithOverflow(int $value = 1): ChronosInterface
{
return $this->subYearsWithOverflow($value);
}
/**
@@ -303,10 +390,10 @@ trait ModifierTrait
* @param int $value The number of months to add.
* @return static
*/
public function addMonths($value)
public function addMonths(int $value): ChronosInterface
{
$day = $this->day;
$date = $this->modify((int)$value . ' month');
$date = $this->modify($value . ' month');
if ($date->day !== $day) {
return $date->modify('last day of previous month');
@@ -318,22 +405,12 @@ trait ModifierTrait
/**
* Add a month to the instance
*
* When adding or subtracting months, if the resulting time is a date
* that does not exist, the result of this operation will always be the
* last day of the intended month.
*
* ### Example:
*
* ```
* (new Chronos('2015-01-03'))->addMonth(); // Results in 2015-02-03
*
* (new Chronos('2015-01-31'))->addMonth(); // Results in 2015-02-28
* ```
* Has the same behavior as `addMonths()`.
*
* @param int $value The number of months to add.
* @return static
*/
public function addMonth($value = 1)
public function addMonth(int $value = 1): ChronosInterface
{
return $this->addMonths($value);
}
@@ -341,22 +418,12 @@ trait ModifierTrait
/**
* Remove a month from the instance
*
* When adding or subtracting months, if the resulting time is a date
* that does not exist, the result of this operation will always be the
* last day of the intended month.
*
* ### Example:
*
* ```
* (new Chronos('2015-03-01'))->subMonth(); // Results in 2015-02-01
*
* (new Chronos('2015-03-31'))->subMonth(); // Results in 2015-02-28
* ```
* Has the same behavior as `addMonths()`.
*
* @param int $value The number of months to remove.
* @return static
*/
public function subMonth($value = 1)
public function subMonth(int $value = 1): ChronosInterface
{
return $this->subMonths($value);
}
@@ -364,22 +431,12 @@ trait ModifierTrait
/**
* Remove months from the instance
*
* When adding or subtracting months, if the resulting time is a date
* that does not exist, the result of this operation will always be the
* last day of the intended month.
*
* ### Example:
*
* ```
* (new Chronos('2015-03-01'))->subMonths(1); // Results in 2015-02-01
*
* (new Chronos('2015-03-31'))->subMonths(1); // Results in 2015-02-28
* ```
* Has the same behavior as `addMonths()`.
*
* @param int $value The number of months to remove.
* @return static
*/
public function subMonths($value)
public function subMonths(int $value): ChronosInterface
{
return $this->addMonths(-1 * $value);
}
@@ -388,47 +445,61 @@ trait ModifierTrait
* Add months with overflowing to the instance. Positive $value
* travels forward while negative $value travels into the past.
*
* @param int $value The number of months to add.
* @return static
*/
public function addMonthsWithOverflow($value)
{
return $this->modify((int)$value . ' month');
}
/**
* Add a month with overflow to the instance
* If the new date does not exist, the days overflow into the next month.
*
* ### Example:
*
* ```
* (new Chronos('2012-01-30'))->addMonthsWithOverflow(1); // Results in 2013-03-01
* ```
*
* @param int $value The number of months to add.
* @return static
*/
public function addMonthWithOverflow($value = 1)
public function addMonthsWithOverflow(int $value): ChronosInterface
{
return $this->modify((int)$value . ' month');
return $this->modify($value . ' month');
}
/**
* Remove a month with overflow from the instance
* Add a month with overflow to the instance.
*
* Has the same behavior as `addMonthsWithOverflow()`.
*
* @param int $value The number of months to add.
* @return static
*/
public function addMonthWithOverflow(int $value = 1): ChronosInterface
{
return $this->modify($value . ' month');
}
/**
* Remove months with overflow from the instance.
*
* Has the same behavior as `addMonthsWithOverflow()`.
*
* @param int $value The number of months to remove.
* @return static
*/
public function subMonthWithOverflow($value = 1)
{
return $this->subMonthsWithOverflow($value);
}
/**
* Remove months with overflow from the instance
*
* @param int $value The number of months to remove.
* @return static
*/
public function subMonthsWithOverflow($value)
public function subMonthsWithOverflow(int $value): ChronosInterface
{
return $this->addMonthsWithOverflow(-1 * $value);
}
/**
* Remove a month with overflow from the instance.
*
* Has the same behavior as `addMonthsWithOverflow()`.
*
* @param int $value The number of months to remove.
* @return static
*/
public function subMonthWithOverflow(int $value = 1): ChronosInterface
{
return $this->subMonthsWithOverflow($value);
}
/**
* Add days to the instance. Positive $value travels forward while
* negative $value travels into the past.
@@ -436,10 +507,8 @@ trait ModifierTrait
* @param int $value The number of days to add.
* @return static
*/
public function addDays($value)
public function addDays(int $value): ChronosInterface
{
$value = (int)$value;
return $this->modify("$value day");
}
@@ -449,10 +518,8 @@ trait ModifierTrait
* @param int $value The number of days to add.
* @return static
*/
public function addDay($value = 1)
public function addDay(int $value = 1): ChronosInterface
{
$value = (int)$value;
return $this->modify("$value day");
}
@@ -462,10 +529,8 @@ trait ModifierTrait
* @param int $value The number of days to remove.
* @return static
*/
public function subDay($value = 1)
public function subDay(int $value = 1): ChronosInterface
{
$value = (int)$value;
return $this->modify("-$value day");
}
@@ -475,10 +540,8 @@ trait ModifierTrait
* @param int $value The number of days to remove.
* @return static
*/
public function subDays($value)
public function subDays(int $value): ChronosInterface
{
$value = (int)$value;
return $this->modify("-$value day");
}
@@ -489,7 +552,7 @@ trait ModifierTrait
* @param int $value The number of weekdays to add.
* @return static
*/
public function addWeekdays($value)
public function addWeekdays(int $value): ChronosInterface
{
return $this->modify((int)$value . ' weekdays ' . $this->format('H:i:s'));
}
@@ -500,7 +563,7 @@ trait ModifierTrait
* @param int $value The number of weekdays to add.
* @return static
*/
public function addWeekday($value = 1)
public function addWeekday(int $value = 1): ChronosInterface
{
return $this->addWeekdays($value);
}
@@ -511,10 +574,8 @@ trait ModifierTrait
* @param int $value The number of weekdays to remove.
* @return static
*/
public function subWeekdays($value)
public function subWeekdays(int $value): ChronosInterface
{
$value = (int)$value;
return $this->modify("$value weekdays ago, " . $this->format('H:i:s'));
}
@@ -524,7 +585,7 @@ trait ModifierTrait
* @param int $value The number of weekdays to remove.
* @return static
*/
public function subWeekday($value = 1)
public function subWeekday(int $value = 1): ChronosInterface
{
return $this->subWeekdays($value);
}
@@ -536,10 +597,8 @@ trait ModifierTrait
* @param int $value The number of weeks to add.
* @return static
*/
public function addWeeks($value)
public function addWeeks(int $value): ChronosInterface
{
$value = (int)$value;
return $this->modify("$value week");
}
@@ -549,10 +608,8 @@ trait ModifierTrait
* @param int $value The number of weeks to add.
* @return static
*/
public function addWeek($value = 1)
public function addWeek(int $value = 1): ChronosInterface
{
$value = (int)$value;
return $this->modify("$value week");
}
@@ -562,10 +619,8 @@ trait ModifierTrait
* @param int $value The number of weeks to remove.
* @return static
*/
public function subWeek($value = 1)
public function subWeek(int $value = 1): ChronosInterface
{
$value = (int)$value;
return $this->modify("-$value week");
}
@@ -575,10 +630,8 @@ trait ModifierTrait
* @param int $value The number of weeks to remove.
* @return static
*/
public function subWeeks($value)
public function subWeeks(int $value): ChronosInterface
{
$value = (int)$value;
return $this->modify("-$value week");
}
@@ -589,10 +642,8 @@ trait ModifierTrait
* @param int $value The number of hours to add.
* @return static
*/
public function addHours($value)
public function addHours(int $value): ChronosInterface
{
$value = (int)$value;
return $this->modify("$value hour");
}
@@ -602,10 +653,8 @@ trait ModifierTrait
* @param int $value The number of hours to add.
* @return static
*/
public function addHour($value = 1)
public function addHour(int $value = 1): ChronosInterface
{
$value = (int)$value;
return $this->modify("$value hour");
}
@@ -615,10 +664,8 @@ trait ModifierTrait
* @param int $value The number of hours to remove.
* @return static
*/
public function subHour($value = 1)
public function subHour(int $value = 1): ChronosInterface
{
$value = (int)$value;
return $this->modify("-$value hour");
}
@@ -628,10 +675,8 @@ trait ModifierTrait
* @param int $value The number of hours to remove.
* @return static
*/
public function subHours($value)
public function subHours(int $value): ChronosInterface
{
$value = (int)$value;
return $this->modify("-$value hour");
}
@@ -642,10 +687,8 @@ trait ModifierTrait
* @param int $value The number of minutes to add.
* @return static
*/
public function addMinutes($value)
public function addMinutes(int $value): ChronosInterface
{
$value = (int)$value;
return $this->modify("$value minute");
}
@@ -655,10 +698,8 @@ trait ModifierTrait
* @param int $value The number of minutes to add.
* @return static
*/
public function addMinute($value = 1)
public function addMinute(int $value = 1): ChronosInterface
{
$value = (int)$value;
return $this->modify("$value minute");
}
@@ -668,10 +709,8 @@ trait ModifierTrait
* @param int $value The number of minutes to remove.
* @return static
*/
public function subMinute($value = 1)
public function subMinute(int $value = 1): ChronosInterface
{
$value = (int)$value;
return $this->modify("-$value minute");
}
@@ -681,10 +720,8 @@ trait ModifierTrait
* @param int $value The number of minutes to remove.
* @return static
*/
public function subMinutes($value)
public function subMinutes(int $value): ChronosInterface
{
$value = (int)$value;
return $this->modify("-$value minute");
}
@@ -695,10 +732,8 @@ trait ModifierTrait
* @param int $value The number of seconds to add.
* @return static
*/
public function addSeconds($value)
public function addSeconds(int $value): ChronosInterface
{
$value = (int)$value;
return $this->modify("$value second");
}
@@ -708,10 +743,8 @@ trait ModifierTrait
* @param int $value The number of seconds to add.
* @return static
*/
public function addSecond($value = 1)
public function addSecond(int $value = 1): ChronosInterface
{
$value = (int)$value;
return $this->modify("$value second");
}
@@ -721,10 +754,8 @@ trait ModifierTrait
* @param int $value The number of seconds to remove.
* @return static
*/
public function subSecond($value = 1)
public function subSecond(int $value = 1): ChronosInterface
{
$value = (int)$value;
return $this->modify("-$value second");
}
@@ -734,10 +765,8 @@ trait ModifierTrait
* @param int $value The number of seconds to remove.
* @return static
*/
public function subSeconds($value)
public function subSeconds(int $value): ChronosInterface
{
$value = (int)$value;
return $this->modify("-$value second");
}
@@ -746,7 +775,7 @@ trait ModifierTrait
*
* @return static
*/
public function startOfDay()
public function startOfDay(): ChronosInterface
{
return $this->modify('midnight');
}
@@ -756,7 +785,7 @@ trait ModifierTrait
*
* @return static
*/
public function endOfDay()
public function endOfDay(): ChronosInterface
{
return $this->modify('23:59:59');
}
@@ -766,7 +795,7 @@ trait ModifierTrait
*
* @return static
*/
public function startOfMonth()
public function startOfMonth(): ChronosInterface
{
return $this->modify('first day of this month midnight');
}
@@ -776,7 +805,7 @@ trait ModifierTrait
*
* @return static
*/
public function endOfMonth()
public function endOfMonth(): ChronosInterface
{
return $this->modify('last day of this month, 23:59:59');
}
@@ -786,7 +815,7 @@ trait ModifierTrait
*
* @return static
*/
public function startOfYear()
public function startOfYear(): ChronosInterface
{
return $this->modify('first day of january midnight');
}
@@ -796,7 +825,7 @@ trait ModifierTrait
*
* @return static
*/
public function endOfYear()
public function endOfYear(): ChronosInterface
{
return $this->modify('last day of december, 23:59:59');
}
@@ -806,7 +835,7 @@ trait ModifierTrait
*
* @return static
*/
public function startOfDecade()
public function startOfDecade(): ChronosInterface
{
$year = $this->year - $this->year % ChronosInterface::YEARS_PER_DECADE;
@@ -818,7 +847,7 @@ trait ModifierTrait
*
* @return static
*/
public function endOfDecade()
public function endOfDecade(): ChronosInterface
{
$year = $this->year - $this->year % ChronosInterface::YEARS_PER_DECADE + ChronosInterface::YEARS_PER_DECADE - 1;
@@ -830,9 +859,11 @@ trait ModifierTrait
*
* @return static
*/
public function startOfCentury()
public function startOfCentury(): ChronosInterface
{
$year = $this->startOfYear()->year(($this->year - 1) - ($this->year - 1) % ChronosInterface::YEARS_PER_CENTURY + 1)->year;
$year = $this->startOfYear()
->year($this->year - 1 - ($this->year - 1) % ChronosInterface::YEARS_PER_CENTURY + 1)
->year;
return $this->modify("first day of january $year, midnight");
}
@@ -842,9 +873,16 @@ trait ModifierTrait
*
* @return static
*/
public function endOfCentury()
public function endOfCentury(): ChronosInterface
{
$year = $this->endOfYear()->year(($this->year - 1) - ($this->year - 1) % ChronosInterface::YEARS_PER_CENTURY + ChronosInterface::YEARS_PER_CENTURY)->year;
$y = $this->year - 1
- ($this->year - 1)
% ChronosInterface::YEARS_PER_CENTURY
+ ChronosInterface::YEARS_PER_CENTURY;
$year = $this->endOfYear()
->year($y)
->year;
return $this->modify("last day of december $year, 23:59:59");
}
@@ -854,7 +892,7 @@ trait ModifierTrait
*
* @return static
*/
public function startOfWeek()
public function startOfWeek(): ChronosInterface
{
$dt = $this;
if ($dt->dayOfWeek !== static::$weekStartsAt) {
@@ -869,7 +907,7 @@ trait ModifierTrait
*
* @return static
*/
public function endOfWeek()
public function endOfWeek(): ChronosInterface
{
$dt = $this;
if ($dt->dayOfWeek !== static::$weekEndsAt) {
@@ -888,7 +926,7 @@ trait ModifierTrait
* @param int|null $dayOfWeek The day of the week to move to.
* @return mixed
*/
public function next($dayOfWeek = null)
public function next(?int $dayOfWeek = null)
{
if ($dayOfWeek === null) {
$dayOfWeek = $this->dayOfWeek;
@@ -908,7 +946,7 @@ trait ModifierTrait
* @param int|null $dayOfWeek The day of the week to move to.
* @return mixed
*/
public function previous($dayOfWeek = null)
public function previous(?int $dayOfWeek = null)
{
if ($dayOfWeek === null) {
$dayOfWeek = $this->dayOfWeek;
@@ -928,7 +966,7 @@ trait ModifierTrait
* @param int|null $dayOfWeek The day of the week to move to.
* @return mixed
*/
public function firstOfMonth($dayOfWeek = null)
public function firstOfMonth(?int $dayOfWeek = null)
{
$day = $dayOfWeek === null ? 'day' : static::$days[$dayOfWeek];
@@ -944,7 +982,7 @@ trait ModifierTrait
* @param int|null $dayOfWeek The day of the week to move to.
* @return mixed
*/
public function lastOfMonth($dayOfWeek = null)
public function lastOfMonth(?int $dayOfWeek = null)
{
$day = $dayOfWeek === null ? 'day' : static::$days[$dayOfWeek];
@@ -961,13 +999,13 @@ trait ModifierTrait
* @param int $dayOfWeek The day of the week to move to.
* @return mixed
*/
public function nthOfMonth($nth, $dayOfWeek)
public function nthOfMonth(int $nth, int $dayOfWeek)
{
$dt = $this->copy()->firstOfMonth();
$check = $dt->format('Y-m');
$dt = $dt->modify("+$nth " . static::$days[$dayOfWeek]);
return ($dt->format('Y-m') === $check) ? $dt : false;
return $dt->format('Y-m') === $check ? $dt : false;
}
/**
@@ -979,9 +1017,12 @@ trait ModifierTrait
* @param int|null $dayOfWeek The day of the week to move to.
* @return mixed
*/
public function firstOfQuarter($dayOfWeek = null)
public function firstOfQuarter(?int $dayOfWeek = null)
{
return $this->day(1)->month($this->quarter * ChronosInterface::MONTHS_PER_QUARTER - 2)->firstOfMonth($dayOfWeek);
return $this
->day(1)
->month($this->quarter * ChronosInterface::MONTHS_PER_QUARTER - 2)
->firstOfMonth($dayOfWeek);
}
/**
@@ -993,9 +1034,12 @@ trait ModifierTrait
* @param int|null $dayOfWeek The day of the week to move to.
* @return mixed
*/
public function lastOfQuarter($dayOfWeek = null)
public function lastOfQuarter(?int $dayOfWeek = null)
{
return $this->day(1)->month($this->quarter * ChronosInterface::MONTHS_PER_QUARTER)->lastOfMonth($dayOfWeek);
return $this
->day(1)
->month($this->quarter * ChronosInterface::MONTHS_PER_QUARTER)
->lastOfMonth($dayOfWeek);
}
/**
@@ -1008,14 +1052,14 @@ trait ModifierTrait
* @param int $dayOfWeek The day of the week to move to.
* @return mixed
*/
public function nthOfQuarter($nth, $dayOfWeek)
public function nthOfQuarter(int $nth, int $dayOfWeek)
{
$dt = $this->copy()->day(1)->month($this->quarter * ChronosInterface::MONTHS_PER_QUARTER);
$lastMonth = $dt->month;
$year = $dt->year;
$dt = $dt->firstOfQuarter()->modify("+$nth" . static::$days[$dayOfWeek]);
return ($lastMonth < $dt->month || $year !== $dt->year) ? false : $dt;
return $lastMonth < $dt->month || $year !== $dt->year ? false : $dt;
}
/**
@@ -1027,7 +1071,7 @@ trait ModifierTrait
* @param int|null $dayOfWeek The day of the week to move to.
* @return mixed
*/
public function firstOfYear($dayOfWeek = null)
public function firstOfYear(?int $dayOfWeek = null)
{
$day = $dayOfWeek === null ? 'day' : static::$days[$dayOfWeek];
@@ -1043,7 +1087,7 @@ trait ModifierTrait
* @param int|null $dayOfWeek The day of the week to move to.
* @return mixed
*/
public function lastOfYear($dayOfWeek = null)
public function lastOfYear(?int $dayOfWeek = null)
{
$day = $dayOfWeek === null ? 'day' : static::$days[$dayOfWeek];
@@ -1060,7 +1104,7 @@ trait ModifierTrait
* @param int $dayOfWeek The day of the week to move to.
* @return mixed
*/
public function nthOfYear($nth, $dayOfWeek)
public function nthOfYear(int $nth, int $dayOfWeek)
{
$dt = $this->copy()->firstOfYear()->modify("+$nth " . static::$days[$dayOfWeek]);
@@ -1073,9 +1117,9 @@ trait ModifierTrait
* @param \Cake\Chronos\ChronosInterface|null $dt The instance to compare with.
* @return static
*/
public function average(ChronosInterface $dt = null)
public function average(?ChronosInterface $dt = null): ChronosInterface
{
$dt = ($dt === null) ? static::now($this->tz) : $dt;
$dt = $dt ?? static::now($this->tz);
return $this->addSeconds((int)($this->diffInSeconds($dt, false) / 2));
}
+31 -4
View File
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
@@ -18,6 +20,11 @@ namespace Cake\Chronos\Traits;
*/
trait RelativeKeywordTrait
{
/**
* Regex for relative period.
*
* @var string
*/
protected static $relativePattern = '/this|next|last|tomorrow|yesterday|midnight|today|[+-]|first|last|ago/i';
/**
@@ -29,7 +36,7 @@ trait RelativeKeywordTrait
private static function isTimeExpression($time)
{
// Just a time
if (preg_match('/^[0-2]?[0-9]:[0-5][0-9](?::[0-5][0-9])?$/', $time)) {
if (is_string($time) && preg_match('/^[0-2]?[0-9]:[0-5][0-9](?::[0-5][0-9](?:\.[0-9]{1,6})?)?$/', $time)) {
return true;
}
@@ -40,19 +47,39 @@ trait RelativeKeywordTrait
* Determine if there is a relative keyword in the time string, this is to
* create dates relative to now for test instances. e.g.: next tuesday
*
* @param string $time The time string to check.
* @param string|null $time The time string to check.
* @return bool true if there is a keyword, otherwise false
*/
public static function hasRelativeKeywords($time)
public static function hasRelativeKeywords(?string $time): bool
{
if (self::isTimeExpression($time)) {
return true;
}
// skip common format with a '-' in it
if (preg_match('/[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}/', $time) !== 1) {
if ($time && preg_match('/[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}/', $time) !== 1) {
return preg_match(static::$relativePattern, $time) > 0;
}
return false;
}
/**
* Determines if there is no fixed date in the time string.
*
* @param \DateTimeInterface|string|null $time The time string to check
* @return bool true if doesn't contain a fixed date
*/
private static function isRelativeOnly($time): bool
{
if ($time === null) {
return true;
}
if (!is_string($time)) {
return false;
}
// must not contain fixed date before relative keywords or time expression
return preg_match('/[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}/', $time) !== 1;
}
}
+5 -4
View File
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
@@ -22,7 +24,6 @@ use Cake\Chronos\ChronosInterface;
*/
trait TestingAidTrait
{
/**
* Set the test now used by Date and Time classes provided by Chronos
*
@@ -30,7 +31,7 @@ trait TestingAidTrait
* @param \Cake\Chronos\ChronosInterface|string|null $testNow The instance to use for all future instances.
* @return void
*/
public static function setTestNow($testNow = null)
public static function setTestNow($testNow = null): void
{
Chronos::setTestNow($testNow);
}
@@ -41,7 +42,7 @@ trait TestingAidTrait
* @see \Cake\Chronos\Chronos::getTestNow()
* @return \Cake\Chronos\ChronosInterface|null the current instance used for testing or null.
*/
public static function getTestNow()
public static function getTestNow(): ?ChronosInterface
{
return Chronos::getTestNow();
}
@@ -52,7 +53,7 @@ trait TestingAidTrait
* @see \Cake\Chronos\Chronos::hasTestNow()
* @return bool True if there is a test instance, otherwise false
*/
public static function hasTestNow()
public static function hasTestNow(): bool
{
return Chronos::hasTestNow();
}
+8 -11
View File
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
@@ -13,6 +15,8 @@
*/
namespace Cake\Chronos\Traits;
use Cake\Chronos\ChronosInterface;
/**
* Methods for modifying/reading timezone data.
*/
@@ -24,7 +28,7 @@ trait TimezoneTrait
* @param \DateTimeZone|string $value The DateTimeZone object or timezone name to use.
* @return static
*/
public function timezone($value)
public function timezone($value): ChronosInterface
{
return $this->setTimezone($value);
}
@@ -35,7 +39,7 @@ trait TimezoneTrait
* @param \DateTimeZone|string $value The DateTimeZone object or timezone name to use.
* @return static
*/
public function tz($value)
public function tz($value): ChronosInterface
{
return $this->setTimezone($value);
}
@@ -46,15 +50,8 @@ trait TimezoneTrait
* @param \DateTimeZone|string $value The DateTimeZone object or timezone name to use.
* @return static
*/
public function setTimezone($value)
public function setTimezone($value): ChronosInterface
{
$date = parent::setTimezone(static::safeCreateDateTimeZone($value));
// https://bugs.php.net/bug.php?id=72338
// this is workaround for this bug
// Needed for PHP below 7.0 version
$date->getTimestamp();
return $date;
return parent::setTimezone(static::safeCreateDateTimeZone($value));
}
}
+5 -3
View File
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
@@ -48,7 +50,7 @@ class Translator
* @param string $key The key to check.
* @return bool Whether or not the key exists.
*/
public function exists($key)
public function exists(string $key): bool
{
return isset(static::$strings[$key]);
}
@@ -61,7 +63,7 @@ class Translator
* @param array $vars Additional context variables.
* @return string The translated message or ''.
*/
public function plural($key, $count, array $vars = [])
public function plural(string $key, int $count, array $vars = []): string
{
if ($count === 1) {
return $this->singular($key, $vars);
@@ -77,7 +79,7 @@ class Translator
* @param array $vars Additional context variables.
* @return string The translated message or ''.
*/
public function singular($key, array $vars = [])
public function singular(string $key, array $vars = []): string
{
if (isset(static::$strings[$key])) {
$varKeys = array_keys($vars);
+5 -2
View File
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
@@ -10,9 +12,10 @@
* @link http://cakephp.org CakePHP(tm) Project
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
define('CHRONOS_SUPPORTS_MICROSECONDS', version_compare(PHP_VERSION, '7.1.0', '>='));
if (!class_exists('Carbon\Carbon')) {
// Check if the interface alias exists and don't redeclare it in case we are in
// a preloaded context.
if (!\class_exists('Carbon\Carbon') && !\interface_exists('Carbon\CarbonInterface', false)) {
// Create class aliases for Carbon so applications
// can upgrade more easily.
class_alias('Cake\Chronos\Chronos', 'Carbon\MutableDateTime');