Aggiornato Composer
This commit is contained in:
445
vendor/nesbot/carbon/src/Carbon/CarbonInterval.php
vendored
445
vendor/nesbot/carbon/src/Carbon/CarbonInterval.php
vendored
@@ -15,6 +15,7 @@ use Carbon\Exceptions\BadFluentConstructorException;
|
||||
use Carbon\Exceptions\BadFluentSetterException;
|
||||
use Carbon\Exceptions\InvalidCastException;
|
||||
use Carbon\Exceptions\InvalidIntervalException;
|
||||
use Carbon\Exceptions\OutOfRangeException;
|
||||
use Carbon\Exceptions\ParseErrorException;
|
||||
use Carbon\Exceptions\UnitNotConfiguredException;
|
||||
use Carbon\Exceptions\UnknownGetterException;
|
||||
@@ -22,15 +23,20 @@ use Carbon\Exceptions\UnknownSetterException;
|
||||
use Carbon\Exceptions\UnknownUnitException;
|
||||
use Carbon\Traits\IntervalRounding;
|
||||
use Carbon\Traits\IntervalStep;
|
||||
use Carbon\Traits\MagicParameter;
|
||||
use Carbon\Traits\Mixin;
|
||||
use Carbon\Traits\Options;
|
||||
use Carbon\Traits\ToStringFormat;
|
||||
use Closure;
|
||||
use DateInterval;
|
||||
use DateMalformedIntervalStringException;
|
||||
use DateTimeInterface;
|
||||
use DateTimeZone;
|
||||
use Exception;
|
||||
use InvalidArgumentException;
|
||||
use ReflectionException;
|
||||
use ReturnTypeWillChange;
|
||||
use RuntimeException;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
@@ -46,7 +52,7 @@ use Throwable;
|
||||
* @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 int $milliseconds Total microseconds of the current interval.
|
||||
* @property int $milliseconds Total milliseconds of the current interval.
|
||||
* @property int $microExcludeMilli Remaining microseconds without the milliseconds.
|
||||
* @property int $dayzExcludeWeeks Total days remaining in the final week of the current instance (days % 7).
|
||||
* @property int $daysExcludeWeeks alias of dayzExcludeWeeks
|
||||
@@ -184,10 +190,12 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
|
||||
{
|
||||
use IntervalRounding;
|
||||
use IntervalStep;
|
||||
use MagicParameter;
|
||||
use Mixin {
|
||||
Mixin::mixin as baseMixin;
|
||||
}
|
||||
use Options;
|
||||
use ToStringFormat;
|
||||
|
||||
/**
|
||||
* Interval spec period designators
|
||||
@@ -241,6 +249,11 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
|
||||
*/
|
||||
private static $flipCascadeFactors;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private static $floatSettersEnabled = false;
|
||||
|
||||
/**
|
||||
* The registered macros.
|
||||
*
|
||||
@@ -294,7 +307,12 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
|
||||
*/
|
||||
public static function getCascadeFactors()
|
||||
{
|
||||
return static::$cascadeFactors ?: [
|
||||
return static::$cascadeFactors ?: static::getDefaultCascadeFactors();
|
||||
}
|
||||
|
||||
protected static function getDefaultCascadeFactors(): array
|
||||
{
|
||||
return [
|
||||
'milliseconds' => [Carbon::MICROSECONDS_PER_MILLISECOND, 'microseconds'],
|
||||
'seconds' => [Carbon::MILLISECONDS_PER_SECOND, 'milliseconds'],
|
||||
'minutes' => [Carbon::SECONDS_PER_MINUTE, 'seconds'],
|
||||
@@ -337,6 +355,19 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
|
||||
static::$cascadeFactors = $cascadeFactors;
|
||||
}
|
||||
|
||||
/**
|
||||
* This option allow you to opt-in for the Carbon 3 behavior where float
|
||||
* values will no longer be cast to integer (so truncated).
|
||||
*
|
||||
* ⚠️ This settings will be applied globally, which mean your whole application
|
||||
* code including the third-party dependencies that also may use Carbon will
|
||||
* adopt the new behavior.
|
||||
*/
|
||||
public static function enableFloatSetters(bool $floatSettersEnabled = true): void
|
||||
{
|
||||
self::$floatSettersEnabled = $floatSettersEnabled;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////
|
||||
//////////////////////////// CONSTRUCTORS /////////////////////////
|
||||
///////////////////////////////////////////////////////////////////
|
||||
@@ -344,14 +375,14 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
|
||||
/**
|
||||
* Create a new CarbonInterval instance.
|
||||
*
|
||||
* @param int|null $years
|
||||
* @param int|null $months
|
||||
* @param int|null $weeks
|
||||
* @param int|null $days
|
||||
* @param int|null $hours
|
||||
* @param int|null $minutes
|
||||
* @param int|null $seconds
|
||||
* @param int|null $microseconds
|
||||
* @param Closure|DateInterval|string|int|null $years
|
||||
* @param int|float|null $months
|
||||
* @param int|float|null $weeks
|
||||
* @param int|float|null $days
|
||||
* @param int|float|null $hours
|
||||
* @param int|float|null $minutes
|
||||
* @param int|float|null $seconds
|
||||
* @param int|float|null $microseconds
|
||||
*
|
||||
* @throws Exception when the interval_spec (passed as $years) cannot be parsed as an interval.
|
||||
*/
|
||||
@@ -371,8 +402,9 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
|
||||
}
|
||||
|
||||
$spec = $years;
|
||||
$isStringSpec = (\is_string($spec) && !preg_match('/^[\d.]/', $spec));
|
||||
|
||||
if (!\is_string($spec) || (float) $years || preg_match('/^[0-9.]/', $years)) {
|
||||
if (!$isStringSpec || (float) $years) {
|
||||
$spec = static::PERIOD_PREFIX;
|
||||
|
||||
$spec .= $years > 0 ? $years.static::PERIOD_YEARS : '';
|
||||
@@ -397,7 +429,74 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
|
||||
}
|
||||
}
|
||||
|
||||
parent::__construct($spec);
|
||||
try {
|
||||
parent::__construct($spec);
|
||||
} catch (Throwable $exception) {
|
||||
try {
|
||||
parent::__construct('PT0S');
|
||||
|
||||
if ($isStringSpec) {
|
||||
if (!preg_match('/^P
|
||||
(?:(?<year>[+-]?\d*(?:\.\d+)?)Y)?
|
||||
(?:(?<month>[+-]?\d*(?:\.\d+)?)M)?
|
||||
(?:(?<week>[+-]?\d*(?:\.\d+)?)W)?
|
||||
(?:(?<day>[+-]?\d*(?:\.\d+)?)D)?
|
||||
(?:T
|
||||
(?:(?<hour>[+-]?\d*(?:\.\d+)?)H)?
|
||||
(?:(?<minute>[+-]?\d*(?:\.\d+)?)M)?
|
||||
(?:(?<second>[+-]?\d*(?:\.\d+)?)S)?
|
||||
)?
|
||||
$/x', $spec, $match)) {
|
||||
throw new InvalidArgumentException("Invalid duration: $spec");
|
||||
}
|
||||
|
||||
$years = (float) ($match['year'] ?? 0);
|
||||
$this->assertSafeForInteger('year', $years);
|
||||
$months = (float) ($match['month'] ?? 0);
|
||||
$this->assertSafeForInteger('month', $months);
|
||||
$weeks = (float) ($match['week'] ?? 0);
|
||||
$this->assertSafeForInteger('week', $weeks);
|
||||
$days = (float) ($match['day'] ?? 0);
|
||||
$this->assertSafeForInteger('day', $days);
|
||||
$hours = (float) ($match['hour'] ?? 0);
|
||||
$this->assertSafeForInteger('hour', $hours);
|
||||
$minutes = (float) ($match['minute'] ?? 0);
|
||||
$this->assertSafeForInteger('minute', $minutes);
|
||||
$seconds = (float) ($match['second'] ?? 0);
|
||||
$this->assertSafeForInteger('second', $seconds);
|
||||
}
|
||||
|
||||
$totalDays = (($weeks * static::getDaysPerWeek()) + $days);
|
||||
$this->assertSafeForInteger('days total (including weeks)', $totalDays);
|
||||
|
||||
$this->y = (int) $years;
|
||||
$this->m = (int) $months;
|
||||
$this->d = (int) $totalDays;
|
||||
$this->h = (int) $hours;
|
||||
$this->i = (int) $minutes;
|
||||
$this->s = (int) $seconds;
|
||||
|
||||
if (
|
||||
((float) $this->y) !== $years ||
|
||||
((float) $this->m) !== $months ||
|
||||
((float) $this->d) !== $totalDays ||
|
||||
((float) $this->h) !== $hours ||
|
||||
((float) $this->i) !== $minutes ||
|
||||
((float) $this->s) !== $seconds
|
||||
) {
|
||||
$this->add(static::fromString(
|
||||
($years - $this->y).' years '.
|
||||
($months - $this->m).' months '.
|
||||
($totalDays - $this->d).' days '.
|
||||
($hours - $this->h).' hours '.
|
||||
($minutes - $this->i).' minutes '.
|
||||
($seconds - $this->s).' seconds '
|
||||
));
|
||||
}
|
||||
} catch (Throwable $secondException) {
|
||||
throw $secondException instanceof OutOfRangeException ? $secondException : $exception;
|
||||
}
|
||||
}
|
||||
|
||||
if ($microseconds !== null) {
|
||||
$this->f = $microseconds / Carbon::MICROSECONDS_PER_SECOND;
|
||||
@@ -410,7 +509,7 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
|
||||
* @param string $source
|
||||
* @param string $target
|
||||
*
|
||||
* @return int|null
|
||||
* @return int|float|null
|
||||
*/
|
||||
public static function getFactor($source, $target)
|
||||
{
|
||||
@@ -438,7 +537,7 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
|
||||
* @param string $source
|
||||
* @param string $target
|
||||
*
|
||||
* @return int|null
|
||||
* @return int|float|null
|
||||
*/
|
||||
public static function getFactorWithDefault($source, $target)
|
||||
{
|
||||
@@ -465,7 +564,7 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
|
||||
/**
|
||||
* Returns current config for days per week.
|
||||
*
|
||||
* @return int
|
||||
* @return int|float
|
||||
*/
|
||||
public static function getDaysPerWeek()
|
||||
{
|
||||
@@ -475,7 +574,7 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
|
||||
/**
|
||||
* Returns current config for hours per day.
|
||||
*
|
||||
* @return int
|
||||
* @return int|float
|
||||
*/
|
||||
public static function getHoursPerDay()
|
||||
{
|
||||
@@ -485,7 +584,7 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
|
||||
/**
|
||||
* Returns current config for minutes per hour.
|
||||
*
|
||||
* @return int
|
||||
* @return int|float
|
||||
*/
|
||||
public static function getMinutesPerHour()
|
||||
{
|
||||
@@ -495,7 +594,7 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
|
||||
/**
|
||||
* Returns current config for seconds per minute.
|
||||
*
|
||||
* @return int
|
||||
* @return int|float
|
||||
*/
|
||||
public static function getSecondsPerMinute()
|
||||
{
|
||||
@@ -505,7 +604,7 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
|
||||
/**
|
||||
* Returns current config for microseconds per second.
|
||||
*
|
||||
* @return int
|
||||
* @return int|float
|
||||
*/
|
||||
public static function getMillisecondsPerSecond()
|
||||
{
|
||||
@@ -515,7 +614,7 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
|
||||
/**
|
||||
* Returns current config for microseconds per second.
|
||||
*
|
||||
* @return int
|
||||
* @return int|float
|
||||
*/
|
||||
public static function getMicrosecondsPerMillisecond()
|
||||
{
|
||||
@@ -673,6 +772,23 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluate the PHP generated by var_export() and recreate the exported CarbonInterval instance.
|
||||
*
|
||||
* @param array $dump data as exported by var_export()
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
#[ReturnTypeWillChange]
|
||||
public static function __set_state($dump)
|
||||
{
|
||||
/** @noinspection PhpVoidFunctionResultUsedInspection */
|
||||
/** @var DateInterval $dateInterval */
|
||||
$dateInterval = parent::__set_state($dump);
|
||||
|
||||
return static::instance($dateInterval);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the current context from inside a macro callee or a new one if static.
|
||||
*
|
||||
@@ -767,6 +883,8 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
|
||||
case 'year':
|
||||
case 'years':
|
||||
case 'y':
|
||||
case 'yr':
|
||||
case 'yrs':
|
||||
$years += $intValue;
|
||||
|
||||
break;
|
||||
@@ -780,6 +898,7 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
|
||||
case 'month':
|
||||
case 'months':
|
||||
case 'mo':
|
||||
case 'mos':
|
||||
$months += $intValue;
|
||||
|
||||
break;
|
||||
@@ -882,7 +1001,7 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
|
||||
return static::fromString(Carbon::translateTimeString($interval, $locale ?: static::getLocale(), 'en'));
|
||||
}
|
||||
|
||||
private static function castIntervalToClass(DateInterval $interval, string $className)
|
||||
private static function castIntervalToClass(DateInterval $interval, string $className, array $skip = [])
|
||||
{
|
||||
$mainClass = DateInterval::class;
|
||||
|
||||
@@ -891,7 +1010,7 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
|
||||
}
|
||||
|
||||
$microseconds = $interval->f;
|
||||
$instance = new $className(static::getDateIntervalSpec($interval));
|
||||
$instance = new $className(static::getDateIntervalSpec($interval, false, $skip));
|
||||
|
||||
if ($microseconds) {
|
||||
$instance->f = $microseconds;
|
||||
@@ -940,12 +1059,19 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
|
||||
* set the $days field.
|
||||
*
|
||||
* @param DateInterval $interval
|
||||
* @param bool $skipCopy set to true to return the passed object
|
||||
* (without copying it) if it's already of the
|
||||
* current class
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public static function instance(DateInterval $interval)
|
||||
public static function instance(DateInterval $interval, array $skip = [], bool $skipCopy = false)
|
||||
{
|
||||
return self::castIntervalToClass($interval, static::class);
|
||||
if ($skipCopy && $interval instanceof static) {
|
||||
return $interval;
|
||||
}
|
||||
|
||||
return self::castIntervalToClass($interval, static::class, $skip);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -956,17 +1082,20 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
|
||||
*
|
||||
* @param mixed|int|DateInterval|string|Closure|null $interval interval or number of the given $unit
|
||||
* @param string|null $unit if specified, $interval must be an integer
|
||||
* @param bool $skipCopy set to true to return the passed object
|
||||
* (without copying it) if it's already of the
|
||||
* current class
|
||||
*
|
||||
* @return static|null
|
||||
*/
|
||||
public static function make($interval, $unit = null)
|
||||
public static function make($interval, $unit = null, bool $skipCopy = false)
|
||||
{
|
||||
if ($unit) {
|
||||
$interval = "$interval ".Carbon::pluralUnit($unit);
|
||||
}
|
||||
|
||||
if ($interval instanceof DateInterval) {
|
||||
return static::instance($interval);
|
||||
return static::instance($interval, [], $skipCopy);
|
||||
}
|
||||
|
||||
if ($interval instanceof Closure) {
|
||||
@@ -984,7 +1113,7 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
|
||||
{
|
||||
$interval = preg_replace('/\s+/', ' ', trim($interval));
|
||||
|
||||
if (preg_match('/^P[T0-9]/', $interval)) {
|
||||
if (preg_match('/^P[T\d]/', $interval)) {
|
||||
return new static($interval);
|
||||
}
|
||||
|
||||
@@ -992,8 +1121,14 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
|
||||
return static::fromString($interval);
|
||||
}
|
||||
|
||||
/** @var static $interval */
|
||||
$interval = static::createFromDateString($interval);
|
||||
// @codeCoverageIgnoreStart
|
||||
try {
|
||||
/** @var static $interval */
|
||||
$interval = static::createFromDateString($interval);
|
||||
} catch (DateMalformedIntervalStringException $e) {
|
||||
return null;
|
||||
}
|
||||
// @codeCoverageIgnoreEnd
|
||||
|
||||
return !$interval || $interval->isEmpty() ? null : $interval;
|
||||
}
|
||||
@@ -1081,11 +1216,11 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
|
||||
return (int) round($this->f * Carbon::MICROSECONDS_PER_SECOND) % Carbon::MICROSECONDS_PER_MILLISECOND;
|
||||
|
||||
case 'weeks':
|
||||
return (int) ($this->d / static::getDaysPerWeek());
|
||||
return (int) ($this->d / (int) static::getDaysPerWeek());
|
||||
|
||||
case 'daysExcludeWeeks':
|
||||
case 'dayzExcludeWeeks':
|
||||
return $this->d % static::getDaysPerWeek();
|
||||
return $this->d % (int) static::getDaysPerWeek();
|
||||
|
||||
case 'locale':
|
||||
return $this->getTranslatorLocale();
|
||||
@@ -1126,43 +1261,63 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
|
||||
foreach ($properties as $key => $value) {
|
||||
switch (Carbon::singularUnit(rtrim($key, 'z'))) {
|
||||
case 'year':
|
||||
$this->checkIntegerValue($key, $value);
|
||||
$this->y = $value;
|
||||
$this->handleDecimalPart('year', $value, $this->y);
|
||||
|
||||
break;
|
||||
|
||||
case 'month':
|
||||
$this->checkIntegerValue($key, $value);
|
||||
$this->m = $value;
|
||||
$this->handleDecimalPart('month', $value, $this->m);
|
||||
|
||||
break;
|
||||
|
||||
case 'week':
|
||||
$this->d = $value * static::getDaysPerWeek();
|
||||
$this->checkIntegerValue($key, $value);
|
||||
$days = $value * (int) static::getDaysPerWeek();
|
||||
$this->assertSafeForInteger('days total (including weeks)', $days);
|
||||
$this->d = $days;
|
||||
$this->handleDecimalPart('day', $days, $this->d);
|
||||
|
||||
break;
|
||||
|
||||
case 'day':
|
||||
$this->checkIntegerValue($key, $value);
|
||||
$this->d = $value;
|
||||
$this->handleDecimalPart('day', $value, $this->d);
|
||||
|
||||
break;
|
||||
|
||||
case 'daysexcludeweek':
|
||||
case 'dayzexcludeweek':
|
||||
$this->d = $this->weeks * static::getDaysPerWeek() + $value;
|
||||
$this->checkIntegerValue($key, $value);
|
||||
$days = $this->weeks * (int) static::getDaysPerWeek() + $value;
|
||||
$this->assertSafeForInteger('days total (including weeks)', $days);
|
||||
$this->d = $days;
|
||||
$this->handleDecimalPart('day', $days, $this->d);
|
||||
|
||||
break;
|
||||
|
||||
case 'hour':
|
||||
$this->checkIntegerValue($key, $value);
|
||||
$this->h = $value;
|
||||
$this->handleDecimalPart('hour', $value, $this->h);
|
||||
|
||||
break;
|
||||
|
||||
case 'minute':
|
||||
$this->checkIntegerValue($key, $value);
|
||||
$this->i = $value;
|
||||
$this->handleDecimalPart('minute', $value, $this->i);
|
||||
|
||||
break;
|
||||
|
||||
case 'second':
|
||||
$this->checkIntegerValue($key, $value);
|
||||
$this->s = $value;
|
||||
$this->handleDecimalPart('second', $value, $this->s);
|
||||
|
||||
break;
|
||||
|
||||
@@ -1355,11 +1510,15 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
|
||||
}
|
||||
|
||||
if (preg_match('/^(?<method>add|sub)(?<unit>[A-Z].*)$/', $method, $match)) {
|
||||
return $this->{$match['method']}($parameters[0], $match['unit']);
|
||||
$value = $this->getMagicParameter($parameters, 0, Carbon::pluralUnit($match['unit']), 0);
|
||||
|
||||
return $this->{$match['method']}($value, $match['unit']);
|
||||
}
|
||||
|
||||
$value = $this->getMagicParameter($parameters, 0, Carbon::pluralUnit($method), 1);
|
||||
|
||||
try {
|
||||
$this->set($method, \count($parameters) === 0 ? 1 : $parameters[0]);
|
||||
$this->set($method, $value);
|
||||
} catch (UnknownSetterException $exception) {
|
||||
if ($this->localStrictModeEnabled ?? Carbon::isStrictModeEnabled()) {
|
||||
throw new BadFluentSetterException($method, 0, $exception);
|
||||
@@ -1410,9 +1569,9 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
|
||||
$minimumUnit = 's';
|
||||
$skip = [];
|
||||
extract($this->getForHumansInitialVariables($syntax, $short));
|
||||
$skip = array_filter((array) $skip, static function ($value) {
|
||||
$skip = array_map('strtolower', array_filter((array) $skip, static function ($value) {
|
||||
return \is_string($value) && $value !== '';
|
||||
});
|
||||
}));
|
||||
|
||||
if ($syntax === null) {
|
||||
$syntax = CarbonInterface::DIFF_ABSOLUTE;
|
||||
@@ -1435,11 +1594,9 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
|
||||
];
|
||||
}
|
||||
|
||||
if ($altNumbers) {
|
||||
if ($altNumbers !== true) {
|
||||
$language = new Language($this->locale);
|
||||
$altNumbers = \in_array($language->getCode(), (array) $altNumbers);
|
||||
}
|
||||
if ($altNumbers && $altNumbers !== true) {
|
||||
$language = new Language($this->locale);
|
||||
$altNumbers = \in_array($language->getCode(), (array) $altNumbers, true);
|
||||
}
|
||||
|
||||
if (\is_array($join)) {
|
||||
@@ -1620,18 +1777,23 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
|
||||
$unit = $short ? 's' : 'second';
|
||||
$isFuture = $this->invert === 1;
|
||||
$transId = $relativeToNow ? ($isFuture ? 'from_now' : 'ago') : ($isFuture ? 'after' : 'before');
|
||||
$declensionMode = null;
|
||||
|
||||
/** @var \Symfony\Component\Translation\Translator $translator */
|
||||
$translator = $this->getLocalTranslator();
|
||||
|
||||
$handleDeclensions = function ($unit, $count) use ($interpolations, $transId, $translator, $altNumbers, $absolute) {
|
||||
$handleDeclensions = function ($unit, $count, $index = 0, $parts = 1) use ($interpolations, $transId, $translator, $altNumbers, $absolute, &$declensionMode) {
|
||||
if (!$absolute) {
|
||||
// Some languages have special pluralization for past and future tense.
|
||||
$key = $unit.'_'.$transId;
|
||||
$result = $this->translate($key, $interpolations, $count, $translator, $altNumbers);
|
||||
$declensionMode = $declensionMode ?? $this->translate($transId.'_mode');
|
||||
|
||||
if ($result !== $key) {
|
||||
return $result;
|
||||
if ($this->needsDeclension($declensionMode, $index, $parts)) {
|
||||
// Some languages have special pluralization for past and future tense.
|
||||
$key = $unit.'_'.$transId;
|
||||
$result = $this->translate($key, $interpolations, $count, $translator, $altNumbers);
|
||||
|
||||
if ($result !== $key) {
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1696,17 +1858,17 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
|
||||
}
|
||||
}
|
||||
|
||||
$transChoice = function ($short, $unitData) use ($absolute, $handleDeclensions, $translator, $aUnit, $altNumbers, $interpolations) {
|
||||
$transChoice = function ($short, $unitData, $index, $parts) use ($absolute, $handleDeclensions, $translator, $aUnit, $altNumbers, $interpolations) {
|
||||
$count = $unitData['value'];
|
||||
|
||||
if ($short) {
|
||||
$result = $handleDeclensions($unitData['unitShort'], $count);
|
||||
$result = $handleDeclensions($unitData['unitShort'], $count, $index, $parts);
|
||||
|
||||
if ($result !== null) {
|
||||
return $result;
|
||||
}
|
||||
} elseif ($aUnit) {
|
||||
$result = $handleDeclensions('a_'.$unitData['unit'], $count);
|
||||
$result = $handleDeclensions('a_'.$unitData['unit'], $count, $index, $parts);
|
||||
|
||||
if ($result !== null) {
|
||||
return $result;
|
||||
@@ -1714,7 +1876,7 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
|
||||
}
|
||||
|
||||
if (!$absolute) {
|
||||
return $handleDeclensions($unitData['unit'], $count);
|
||||
return $handleDeclensions($unitData['unit'], $count, $index, $parts);
|
||||
}
|
||||
|
||||
return $this->translate($unitData['unit'], $interpolations, $count, $translator, $altNumbers);
|
||||
@@ -1726,7 +1888,7 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
|
||||
if ($diffIntervalData['value'] > 0) {
|
||||
$unit = $short ? $diffIntervalData['unitShort'] : $diffIntervalData['unit'];
|
||||
$count = $diffIntervalData['value'];
|
||||
$interval[] = $transChoice($short, $diffIntervalData);
|
||||
$interval[] = [$short, $diffIntervalData];
|
||||
} elseif ($options & CarbonInterface::SEQUENTIAL_PARTS_ONLY && \count($interval) > 0) {
|
||||
break;
|
||||
}
|
||||
@@ -1737,13 +1899,19 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
|
||||
}
|
||||
|
||||
// break the loop after we have reached the minimum unit
|
||||
if (\in_array($minimumUnit, [$diffIntervalData['unit'], $diffIntervalData['unitShort']])) {
|
||||
if (\in_array($minimumUnit, [$diffIntervalData['unit'], $diffIntervalData['unitShort']], true)) {
|
||||
$fallbackUnit = [$diffIntervalData['unit'], $diffIntervalData['unitShort']];
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$actualParts = \count($interval);
|
||||
|
||||
foreach ($interval as $index => &$item) {
|
||||
$item = $transChoice($item[0], $item[1], $index, $actualParts);
|
||||
}
|
||||
|
||||
if (\count($interval) === 0) {
|
||||
if ($relativeToNow && $options & CarbonInterface::JUST_NOW) {
|
||||
$key = 'diff_now';
|
||||
@@ -1812,17 +1980,17 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
$format = $this->localToStringFormat;
|
||||
$format = $this->localToStringFormat ?? static::$toStringFormat;
|
||||
|
||||
if ($format) {
|
||||
if ($format instanceof Closure) {
|
||||
return $format($this);
|
||||
}
|
||||
|
||||
return $this->format($format);
|
||||
if (!$format) {
|
||||
return $this->forHumans();
|
||||
}
|
||||
|
||||
return $this->forHumans();
|
||||
if ($format instanceof Closure) {
|
||||
return $format($this);
|
||||
}
|
||||
|
||||
return $this->format($format);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1863,7 +2031,7 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
|
||||
/**
|
||||
* Invert the interval.
|
||||
*
|
||||
* @param bool|int $inverted if a parameter is passed, the passed value casted as 1 or 0 is used
|
||||
* @param bool|int $inverted if a parameter is passed, the passed value cast as 1 or 0 is used
|
||||
* as the new value of the ->invert property.
|
||||
*
|
||||
* @return $this
|
||||
@@ -2141,7 +2309,7 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getDateIntervalSpec(DateInterval $interval)
|
||||
public static function getDateIntervalSpec(DateInterval $interval, bool $microseconds = false, array $skip = [])
|
||||
{
|
||||
$date = array_filter([
|
||||
static::PERIOD_YEARS => abs($interval->y),
|
||||
@@ -2149,10 +2317,25 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
|
||||
static::PERIOD_DAYS => abs($interval->d),
|
||||
]);
|
||||
|
||||
if (
|
||||
$interval->days >= CarbonInterface::DAYS_PER_WEEK * CarbonInterface::WEEKS_PER_MONTH &&
|
||||
(!isset($date[static::PERIOD_YEARS]) || \count(array_intersect(['y', 'year', 'years'], $skip))) &&
|
||||
(!isset($date[static::PERIOD_MONTHS]) || \count(array_intersect(['m', 'month', 'months'], $skip)))
|
||||
) {
|
||||
$date = [
|
||||
static::PERIOD_DAYS => abs($interval->days),
|
||||
];
|
||||
}
|
||||
|
||||
$seconds = abs($interval->s);
|
||||
if ($microseconds && $interval->f > 0) {
|
||||
$seconds = sprintf('%d.%06d', $seconds, abs($interval->f) * 1000000);
|
||||
}
|
||||
|
||||
$time = array_filter([
|
||||
static::PERIOD_HOURS => abs($interval->h),
|
||||
static::PERIOD_MINUTES => abs($interval->i),
|
||||
static::PERIOD_SECONDS => abs($interval->s),
|
||||
static::PERIOD_SECONDS => $seconds,
|
||||
]);
|
||||
|
||||
$specString = static::PERIOD_PREFIX;
|
||||
@@ -2176,9 +2359,9 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function spec()
|
||||
public function spec(bool $microseconds = false)
|
||||
{
|
||||
return static::getDateIntervalSpec($this);
|
||||
return static::getDateIntervalSpec($this, $microseconds);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2229,9 +2412,11 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
|
||||
$originalData = $this->toArray();
|
||||
$originalData['milliseconds'] = (int) ($originalData['microseconds'] / static::getMicrosecondsPerMillisecond());
|
||||
$originalData['microseconds'] = $originalData['microseconds'] % static::getMicrosecondsPerMillisecond();
|
||||
$originalData['daysExcludeWeeks'] = $originalData['days'];
|
||||
$originalData['weeks'] = (int) ($this->d / static::getDaysPerWeek());
|
||||
$originalData['daysExcludeWeeks'] = fmod($this->d, static::getDaysPerWeek());
|
||||
unset($originalData['days']);
|
||||
$newData = $originalData;
|
||||
$previous = [];
|
||||
|
||||
foreach (self::getFlipCascadeFactors() as $source => [$target, $factor]) {
|
||||
foreach (['source', 'target'] as $key) {
|
||||
@@ -2241,9 +2426,29 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
|
||||
}
|
||||
|
||||
$value = $newData[$source];
|
||||
$modulo = ($factor + ($value % $factor)) % $factor;
|
||||
$modulo = fmod($factor + fmod($value, $factor), $factor);
|
||||
$newData[$source] = $modulo;
|
||||
$newData[$target] += ($value - $modulo) / $factor;
|
||||
|
||||
$decimalPart = fmod($newData[$source], 1);
|
||||
|
||||
if ($decimalPart !== 0.0) {
|
||||
$unit = $source;
|
||||
|
||||
foreach ($previous as [$subUnit, $subFactor]) {
|
||||
$newData[$unit] -= $decimalPart;
|
||||
$newData[$subUnit] += $decimalPart * $subFactor;
|
||||
$decimalPart = fmod($newData[$subUnit], 1);
|
||||
|
||||
if ($decimalPart === 0.0) {
|
||||
break;
|
||||
}
|
||||
|
||||
$unit = $subUnit;
|
||||
}
|
||||
}
|
||||
|
||||
array_unshift($previous, [$source, $factor]);
|
||||
}
|
||||
|
||||
$positive = null;
|
||||
@@ -2324,13 +2529,13 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
|
||||
$cumulativeFactor = 0;
|
||||
$unitFound = false;
|
||||
$factors = self::getFlipCascadeFactors();
|
||||
$daysPerWeek = static::getDaysPerWeek();
|
||||
$daysPerWeek = (int) static::getDaysPerWeek();
|
||||
|
||||
$values = [
|
||||
'years' => $this->years,
|
||||
'months' => $this->months,
|
||||
'weeks' => (int) ($this->d / $daysPerWeek),
|
||||
'dayz' => $this->d % $daysPerWeek,
|
||||
'dayz' => fmod($this->d, $daysPerWeek),
|
||||
'hours' => $this->hours,
|
||||
'minutes' => $this->minutes,
|
||||
'seconds' => $this->seconds,
|
||||
@@ -2391,10 +2596,11 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
|
||||
}
|
||||
|
||||
if ($unit === 'weeks') {
|
||||
return $result / $daysPerWeek;
|
||||
$result /= $daysPerWeek;
|
||||
}
|
||||
|
||||
return $result;
|
||||
// Cast as int numbers with no decimal part
|
||||
return fmod($result, 1) === 0.0 ? (int) $result : $result;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2662,6 +2868,15 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
|
||||
*/
|
||||
public function roundUnit($unit, $precision = 1, $function = 'round')
|
||||
{
|
||||
if (static::getCascadeFactors() !== static::getDefaultCascadeFactors()) {
|
||||
$value = $function($this->total($unit) / $precision) * $precision;
|
||||
$inverted = $value < 0;
|
||||
|
||||
return $this->copyProperties(self::fromString(
|
||||
number_format(abs($value), 12, '.', '').' '.$unit
|
||||
)->invert($inverted)->cascade());
|
||||
}
|
||||
|
||||
$base = CarbonImmutable::parse('2000-01-01 00:00:00', 'UTC')
|
||||
->roundUnit($unit, $precision, $function);
|
||||
$next = $base->add($this);
|
||||
@@ -2752,4 +2967,88 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
|
||||
{
|
||||
return $this->round($precision, 'ceil');
|
||||
}
|
||||
|
||||
private function needsDeclension(string $mode, int $index, int $parts): bool
|
||||
{
|
||||
switch ($mode) {
|
||||
case 'last':
|
||||
return $index === $parts - 1;
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
private function checkIntegerValue(string $name, $value)
|
||||
{
|
||||
if (\is_int($value)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->assertSafeForInteger($name, $value);
|
||||
|
||||
if (\is_float($value) && (((float) (int) $value) === $value)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!self::$floatSettersEnabled) {
|
||||
$type = \gettype($value);
|
||||
@trigger_error(
|
||||
"Since 2.70.0, it's deprecated to pass $type value for $name.\n".
|
||||
"It's truncated when stored as an integer interval unit.\n".
|
||||
"From 3.0.0, decimal part will no longer be truncated and will be cascaded to smaller units.\n".
|
||||
"- To maintain the current behavior, use explicit cast: $name((int) \$value)\n".
|
||||
"- To adopt the new behavior globally, call CarbonInterval::enableFloatSetters()\n",
|
||||
\E_USER_DEPRECATED
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Throw an exception if precision loss when storing the given value as an integer would be >= 1.0.
|
||||
*/
|
||||
private function assertSafeForInteger(string $name, $value)
|
||||
{
|
||||
if ($value && !\is_int($value) && ($value >= 0x7fffffffffffffff || $value <= -0x7fffffffffffffff)) {
|
||||
throw new OutOfRangeException($name, -0x7fffffffffffffff, 0x7fffffffffffffff, $value);
|
||||
}
|
||||
}
|
||||
|
||||
private function handleDecimalPart(string $unit, $value, $integerValue)
|
||||
{
|
||||
if (self::$floatSettersEnabled) {
|
||||
$floatValue = (float) $value;
|
||||
$base = (float) $integerValue;
|
||||
|
||||
if ($floatValue === $base) {
|
||||
return;
|
||||
}
|
||||
|
||||
$units = [
|
||||
'y' => 'year',
|
||||
'm' => 'month',
|
||||
'd' => 'day',
|
||||
'h' => 'hour',
|
||||
'i' => 'minute',
|
||||
's' => 'second',
|
||||
];
|
||||
$upper = true;
|
||||
|
||||
foreach ($units as $property => $name) {
|
||||
if ($name === $unit) {
|
||||
$upper = false;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!$upper && $this->$property !== 0) {
|
||||
throw new RuntimeException(
|
||||
"You cannot set $unit to a float value as $name would be overridden, ".
|
||||
'set it first to 0 explicitly if you really want to erase its value'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$this->add($unit, $floatValue - $base);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user