downgrade to kirby v3
This commit is contained in:
@@ -2,12 +2,9 @@
|
||||
|
||||
namespace Kirby\Toolkit;
|
||||
|
||||
use DateInterval;
|
||||
use DateTime;
|
||||
use DateTimeInterface;
|
||||
use DateTimeZone;
|
||||
use Exception;
|
||||
use IntlDateFormatter;
|
||||
use Kirby\Exception\InvalidArgumentException;
|
||||
|
||||
/**
|
||||
@@ -29,15 +26,13 @@ class Date extends DateTime
|
||||
* @param string|int|\DateTimeInterface $datetime Datetime string, UNIX timestamp or object
|
||||
* @param \DateTimeZone|null $timezone Optional default timezone if `$datetime` is string
|
||||
*/
|
||||
public function __construct(
|
||||
string|int|DateTimeInterface $datetime = 'now',
|
||||
DateTimeZone|null $timezone = null
|
||||
) {
|
||||
public function __construct($datetime = 'now', ?DateTimeZone $timezone = null)
|
||||
{
|
||||
if (is_int($datetime) === true) {
|
||||
$datetime = date('r', $datetime);
|
||||
}
|
||||
|
||||
if ($datetime instanceof DateTimeInterface) {
|
||||
if (is_a($datetime, 'DateTimeInterface') === true) {
|
||||
$datetime = $datetime->format('r');
|
||||
}
|
||||
|
||||
@@ -46,6 +41,8 @@ class Date extends DateTime
|
||||
|
||||
/**
|
||||
* Returns the datetime in `YYYY-MM-DD hh:mm:ss` format with timezone
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString(): string
|
||||
{
|
||||
@@ -60,7 +57,7 @@ class Date extends DateTime
|
||||
*
|
||||
* @throws \Kirby\Exception\InvalidArgumentException If the unit name is invalid
|
||||
*/
|
||||
public function ceil(string $unit): static
|
||||
public function ceil(string $unit)
|
||||
{
|
||||
static::validateUnit($unit);
|
||||
|
||||
@@ -72,19 +69,22 @@ class Date extends DateTime
|
||||
/**
|
||||
* Returns the interval between the provided and the object's datetime
|
||||
*
|
||||
* @param string|int|\DateTimeInterface $datetime
|
||||
* @param \DateTimeZone|null $timezone Optional default timezone if `$datetime` is string
|
||||
* @return \DateInterval
|
||||
*/
|
||||
public function compare(
|
||||
string|int|DateTimeInterface $datetime = 'now',
|
||||
DateTimeZone|null $timezone = null
|
||||
): DateInterval {
|
||||
public function compare($datetime = 'now', ?DateTimeZone $timezone = null)
|
||||
{
|
||||
return $this->diff(new static($datetime, $timezone));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets or sets the day value
|
||||
*
|
||||
* @param int|null $day
|
||||
* @return int
|
||||
*/
|
||||
public function day(int|null $day = null): int
|
||||
public function day(?int $day = null): int
|
||||
{
|
||||
if ($day === null) {
|
||||
return (int)$this->format('d');
|
||||
@@ -102,42 +102,31 @@ class Date extends DateTime
|
||||
*
|
||||
* @throws \Kirby\Exception\InvalidArgumentException If the unit name is invalid
|
||||
*/
|
||||
public function floor(string $unit): static
|
||||
public function floor(string $unit)
|
||||
{
|
||||
static::validateUnit($unit);
|
||||
|
||||
$formats = [
|
||||
'year' => 'Y-01-01',
|
||||
'month' => 'Y-m-01',
|
||||
'day' => 'Y-m-d',
|
||||
'hour' => 'Y-m-d H:00:00',
|
||||
'minute' => 'Y-m-d H:i:00',
|
||||
'second' => 'Y-m-d H:i:s'
|
||||
'year' => 'Y-01-01P',
|
||||
'month' => 'Y-m-01P',
|
||||
'day' => 'Y-m-dP',
|
||||
'hour' => 'Y-m-d H:00:00P',
|
||||
'minute' => 'Y-m-d H:i:00P',
|
||||
'second' => 'Y-m-d H:i:sP'
|
||||
];
|
||||
|
||||
$flooredDate = $this->format($formats[$unit]);
|
||||
$this->set($flooredDate, $this->timezone());
|
||||
$flooredDate = date($formats[$unit], $this->timestamp());
|
||||
$this->set($flooredDate);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats the datetime value with a custom handler
|
||||
* or with the globally configured one
|
||||
*
|
||||
* @param 'date'|'intl'|'strftime'|null $handler Custom date handler or `null`
|
||||
* for the globally configured one
|
||||
*/
|
||||
public function formatWithHandler(
|
||||
string|IntlDateFormatter|null $format = null,
|
||||
string|null $handler = null
|
||||
): string|int|false {
|
||||
return Str::date($this->timestamp(), $format, $handler);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets or sets the hour value
|
||||
*
|
||||
* @param int|null $hour
|
||||
* @return int
|
||||
*/
|
||||
public function hour(int|null $hour = null): int
|
||||
public function hour(?int $hour = null): int
|
||||
{
|
||||
if ($hour === null) {
|
||||
return (int)$this->format('H');
|
||||
@@ -150,93 +139,102 @@ class Date extends DateTime
|
||||
/**
|
||||
* Checks if the object's datetime is the same as the given datetime
|
||||
*
|
||||
* @param string|int|\DateTimeInterface $datetime
|
||||
* @param \DateTimeZone|null $timezone Optional default timezone if `$datetime` is string
|
||||
* @return bool
|
||||
*/
|
||||
public function is(
|
||||
string|int|DateTimeInterface $datetime = 'now',
|
||||
DateTimeZone|null $timezone = null
|
||||
): bool {
|
||||
public function is($datetime = 'now', ?DateTimeZone $timezone = null): bool
|
||||
{
|
||||
return $this == new static($datetime, $timezone);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the object's datetime is after the given datetime
|
||||
*
|
||||
* @param string|int|\DateTimeInterface $datetime
|
||||
* @param \DateTimeZone|null $timezone Optional default timezone if `$datetime` is string
|
||||
* @return bool
|
||||
*/
|
||||
public function isAfter(
|
||||
string|int|DateTimeInterface $datetime = 'now',
|
||||
DateTimeZone|null $timezone = null
|
||||
): bool {
|
||||
public function isAfter($datetime = 'now', ?DateTimeZone $timezone = null): bool
|
||||
{
|
||||
return $this > new static($datetime, $timezone);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the object's datetime is before the given datetime
|
||||
*
|
||||
* @param string|int|\DateTimeInterface $datetime
|
||||
* @param \DateTimeZone|null $timezone Optional default timezone if `$datetime` is string
|
||||
* @return bool
|
||||
*/
|
||||
public function isBefore(
|
||||
string|int|DateTimeInterface $datetime = 'now',
|
||||
DateTimeZone|null $timezone = null
|
||||
): bool {
|
||||
public function isBefore($datetime = 'now', ?DateTimeZone $timezone = null): bool
|
||||
{
|
||||
return $this < new static($datetime, $timezone);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the object's datetime is between the given datetimes
|
||||
*
|
||||
* @param string|int|\DateTimeInterface $min
|
||||
* @param string|int|\DateTimeInterface $max
|
||||
* @return bool
|
||||
*/
|
||||
public function isBetween(
|
||||
string|int|DateTimeInterface $min,
|
||||
string|int|DateTimeInterface $max
|
||||
): bool {
|
||||
public function isBetween($min, $max): bool
|
||||
{
|
||||
return $this->isMin($min) === true && $this->isMax($max) === true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the object's datetime is at or before the given datetime
|
||||
*
|
||||
* @param string|int|\DateTimeInterface $datetime
|
||||
* @param \DateTimeZone|null $timezone Optional default timezone if `$datetime` is string
|
||||
* @return bool
|
||||
*/
|
||||
public function isMax(
|
||||
string|int|DateTimeInterface $datetime = 'now',
|
||||
DateTimeZone|null $timezone = null
|
||||
): bool {
|
||||
public function isMax($datetime = 'now', ?DateTimeZone $timezone = null): bool
|
||||
{
|
||||
return $this <= new static($datetime, $timezone);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the object's datetime is at or after the given datetime
|
||||
*
|
||||
* @param string|int|\DateTimeInterface $datetime
|
||||
* @param \DateTimeZone|null $timezone Optional default timezone if `$datetime` is string
|
||||
* @return bool
|
||||
*/
|
||||
public function isMin(
|
||||
string|int|DateTimeInterface $datetime = 'now',
|
||||
DateTimeZone|null $timezone = null
|
||||
): bool {
|
||||
public function isMin($datetime = 'now', ?DateTimeZone $timezone = null): bool
|
||||
{
|
||||
return $this >= new static($datetime, $timezone);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the microsecond value
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function microsecond(): int
|
||||
{
|
||||
return (int)$this->format('u');
|
||||
return $this->format('u');
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the millisecond value
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function millisecond(): int
|
||||
{
|
||||
return (int)$this->format('v');
|
||||
return $this->format('v');
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets or sets the minute value
|
||||
*
|
||||
* @param int|null $minute
|
||||
* @return int
|
||||
*/
|
||||
public function minute(int|null $minute = null): int
|
||||
public function minute(?int $minute = null): int
|
||||
{
|
||||
if ($minute === null) {
|
||||
return (int)$this->format('i');
|
||||
@@ -248,8 +246,11 @@ class Date extends DateTime
|
||||
|
||||
/**
|
||||
* Gets or sets the month value
|
||||
*
|
||||
* @param int|null $month
|
||||
* @return int
|
||||
*/
|
||||
public function month(int|null $month = null): int
|
||||
public function month(?int $month = null): int
|
||||
{
|
||||
if ($month === null) {
|
||||
return (int)$this->format('m');
|
||||
@@ -263,10 +264,10 @@ class Date extends DateTime
|
||||
* Returns the datetime which is nearest to the object's datetime
|
||||
*
|
||||
* @param string|int|\DateTimeInterface ...$datetime Datetime strings, UNIX timestamps or objects
|
||||
* @return string|int|\DateTimeInterface
|
||||
*/
|
||||
public function nearest(
|
||||
string|int|DateTimeInterface ...$datetime
|
||||
): string|int|DateTimeInterface {
|
||||
public function nearest(...$datetime)
|
||||
{
|
||||
$timestamp = $this->timestamp();
|
||||
$minDiff = PHP_INT_MAX;
|
||||
$nearest = null;
|
||||
@@ -289,8 +290,9 @@ class Date extends DateTime
|
||||
* Returns an instance of the current datetime
|
||||
*
|
||||
* @param \DateTimeZone|null $timezone
|
||||
* @return static
|
||||
*/
|
||||
public static function now(DateTimeZone|null $timezone = null): static
|
||||
public static function now(?DateTimeZone $timezone = null)
|
||||
{
|
||||
return new static('now', $timezone);
|
||||
}
|
||||
@@ -298,18 +300,20 @@ class Date extends DateTime
|
||||
/**
|
||||
* Tries to create an instance from the given string
|
||||
* or fails silently by returning `null` on error
|
||||
*
|
||||
* @param string|null $datetime
|
||||
* @param \DateTimeZone|null $timezone
|
||||
* @return static|null
|
||||
*/
|
||||
public static function optional(
|
||||
string|null $datetime = null,
|
||||
DateTimeZone|null $timezone = null
|
||||
): static|null {
|
||||
public static function optional(?string $datetime = null, ?DateTimeZone $timezone = null)
|
||||
{
|
||||
if (empty($datetime) === true) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
return new static($datetime, $timezone);
|
||||
} catch (Exception) {
|
||||
} catch (Exception $e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -323,7 +327,7 @@ class Date extends DateTime
|
||||
*
|
||||
* @throws \Kirby\Exception\InvalidArgumentException If the unit name or size is invalid
|
||||
*/
|
||||
public function round(string $unit, int $size = 1): static
|
||||
public function round(string $unit, int $size = 1)
|
||||
{
|
||||
static::validateUnit($unit);
|
||||
|
||||
@@ -360,12 +364,12 @@ class Date extends DateTime
|
||||
* by the defined step
|
||||
* @since 3.7.0
|
||||
*
|
||||
* @param string|null $date
|
||||
* @param int|array|null $step array of `unit` and `size` to round to nearest
|
||||
* @return int|null
|
||||
*/
|
||||
public static function roundedTimestamp(
|
||||
string|null $date = null,
|
||||
int|array|null $step = null
|
||||
): int|null {
|
||||
public static function roundedTimestamp(?string $date = null, $step = null): ?int
|
||||
{
|
||||
if ($date = static::optional($date)) {
|
||||
if ($step !== null) {
|
||||
$step = static::stepConfig($step, [
|
||||
@@ -383,8 +387,11 @@ class Date extends DateTime
|
||||
|
||||
/**
|
||||
* Gets or sets the second value
|
||||
*
|
||||
* @param int|null $second
|
||||
* @return int
|
||||
*/
|
||||
public function second(int|null $second = null): int
|
||||
public function second(?int $second = null): int
|
||||
{
|
||||
if ($second === null) {
|
||||
return (int)$this->format('s');
|
||||
@@ -400,10 +407,8 @@ class Date extends DateTime
|
||||
* @param string|int|\DateTimeInterface $datetime Datetime string, UNIX timestamp or object
|
||||
* @param \DateTimeZone|null $timezone Optional default timezone if `$datetime` is string
|
||||
*/
|
||||
public function set(
|
||||
string|int|DateTimeInterface $datetime,
|
||||
DateTimeZone|null $timezone = null
|
||||
): void {
|
||||
public function set($datetime, ?DateTimeZone $timezone = null)
|
||||
{
|
||||
$datetime = new static($datetime, $timezone);
|
||||
$this->setTimestamp($datetime->timestamp());
|
||||
}
|
||||
@@ -414,12 +419,15 @@ class Date extends DateTime
|
||||
* @param array|string|int|null $input Full array with `size` and/or `unit` keys, `unit`
|
||||
* string, `size` int or `null` for the default
|
||||
* @param array|null $default Default values to use if one or both values are not provided
|
||||
* @return array
|
||||
*/
|
||||
public static function stepConfig(
|
||||
// no type hint to use InvalidArgumentException at the end
|
||||
$input = null,
|
||||
array|null $default = ['size' => 1, 'unit' => 'day']
|
||||
): array {
|
||||
public static function stepConfig($input = null, ?array $default = null): array
|
||||
{
|
||||
$default ??= [
|
||||
'size' => 1,
|
||||
'unit' => 'day'
|
||||
];
|
||||
|
||||
if ($input === null) {
|
||||
return $default;
|
||||
}
|
||||
@@ -443,6 +451,8 @@ class Date extends DateTime
|
||||
|
||||
/**
|
||||
* Returns the time in `hh:mm:ss` format
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function time(): string
|
||||
{
|
||||
@@ -451,6 +461,8 @@ class Date extends DateTime
|
||||
|
||||
/**
|
||||
* Returns the UNIX timestamp
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function timestamp(): int
|
||||
{
|
||||
@@ -459,16 +471,21 @@ class Date extends DateTime
|
||||
|
||||
/**
|
||||
* Returns the timezone object
|
||||
*
|
||||
* @return \DateTimeZone
|
||||
*/
|
||||
public function timezone(): DateTimeZone|false
|
||||
public function timezone()
|
||||
{
|
||||
return $this->getTimezone();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an instance of the beginning of the current day
|
||||
*
|
||||
* @param \DateTimeZone|null $timezone
|
||||
* @return static
|
||||
*/
|
||||
public static function today(DateTimeZone|null $timezone = null): static
|
||||
public static function today(?DateTimeZone $timezone = null)
|
||||
{
|
||||
return new static('today', $timezone);
|
||||
}
|
||||
@@ -479,19 +496,25 @@ class Date extends DateTime
|
||||
*
|
||||
* @param string $mode `date`, `time` or `datetime`
|
||||
* @param bool $timezone Whether the timezone is printed as well
|
||||
* @return string
|
||||
*
|
||||
* @throws \Kirby\Exception\InvalidArgumentException If the mode is invalid
|
||||
*/
|
||||
public function toString(
|
||||
string $mode = 'datetime',
|
||||
bool $timezone = true
|
||||
): string {
|
||||
$format = match ($mode) {
|
||||
'date' => 'Y-m-d',
|
||||
'time' => 'H:i:s',
|
||||
'datetime' => 'Y-m-d H:i:s',
|
||||
default => throw new InvalidArgumentException('Invalid mode')
|
||||
};
|
||||
public function toString(string $mode = 'datetime', bool $timezone = true): string
|
||||
{
|
||||
switch ($mode) {
|
||||
case 'date':
|
||||
$format = 'Y-m-d';
|
||||
break;
|
||||
case 'time':
|
||||
$format = 'H:i:s';
|
||||
break;
|
||||
case 'datetime':
|
||||
$format = 'Y-m-d H:i:s';
|
||||
break;
|
||||
default:
|
||||
throw new InvalidArgumentException('Invalid mode');
|
||||
}
|
||||
|
||||
if ($timezone === true) {
|
||||
$format .= 'P';
|
||||
@@ -502,8 +525,11 @@ class Date extends DateTime
|
||||
|
||||
/**
|
||||
* Gets or sets the year value
|
||||
*
|
||||
* @param int|null $year
|
||||
* @return int
|
||||
*/
|
||||
public function year(int|null $year = null): int
|
||||
public function year(?int $year = null): int
|
||||
{
|
||||
if ($year === null) {
|
||||
return (int)$this->format('Y');
|
||||
@@ -516,6 +542,9 @@ class Date extends DateTime
|
||||
/**
|
||||
* Ensures that the provided string is a valid unit name
|
||||
*
|
||||
* @param string $unit
|
||||
* @return void
|
||||
*
|
||||
* @throws \Kirby\Exception\InvalidArgumentException
|
||||
*/
|
||||
protected static function validateUnit(string $unit): void
|
||||
|
||||
Reference in New Issue
Block a user