Aggiornato Composer
This commit is contained in:
@@ -19,6 +19,8 @@ use Carbon\Exceptions\InvalidFormatException;
|
||||
use Carbon\Exceptions\OutOfRangeException;
|
||||
use Carbon\Translator;
|
||||
use Closure;
|
||||
use DateMalformedStringException;
|
||||
use DateTimeImmutable;
|
||||
use DateTimeInterface;
|
||||
use DateTimeZone;
|
||||
use Exception;
|
||||
@@ -79,8 +81,8 @@ trait Creator
|
||||
|
||||
// Work-around for PHP bug https://bugs.php.net/bug.php?id=67127
|
||||
if (!str_contains((string) .1, '.')) {
|
||||
$locale = setlocale(LC_NUMERIC, '0');
|
||||
setlocale(LC_NUMERIC, 'C');
|
||||
$locale = setlocale(LC_NUMERIC, '0'); // @codeCoverageIgnore
|
||||
setlocale(LC_NUMERIC, 'C'); // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -92,7 +94,7 @@ trait Creator
|
||||
$this->constructedObjectId = spl_object_hash($this);
|
||||
|
||||
if (isset($locale)) {
|
||||
setlocale(LC_NUMERIC, $locale);
|
||||
setlocale(LC_NUMERIC, $locale); // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
self::setLastErrors(parent::getLastErrors());
|
||||
@@ -112,7 +114,7 @@ trait Creator
|
||||
$safeTz = static::safeCreateDateTimeZone($tz);
|
||||
|
||||
if ($safeTz) {
|
||||
return $date->setTimezone($safeTz);
|
||||
return ($date instanceof DateTimeImmutable ? $date : clone $date)->setTimezone($safeTz);
|
||||
}
|
||||
|
||||
return $date;
|
||||
@@ -148,7 +150,7 @@ trait Creator
|
||||
|
||||
$instance = new static($date->format('Y-m-d H:i:s.u'), $date->getTimezone());
|
||||
|
||||
if ($date instanceof CarbonInterface || $date instanceof Options) {
|
||||
if ($date instanceof CarbonInterface) {
|
||||
$settings = $date->getSettings();
|
||||
|
||||
if (!$date->hasLocalTranslator()) {
|
||||
@@ -184,7 +186,13 @@ trait Creator
|
||||
try {
|
||||
return new static($time, $tz);
|
||||
} catch (Exception $exception) {
|
||||
$date = @static::now($tz)->change($time);
|
||||
// @codeCoverageIgnoreStart
|
||||
try {
|
||||
$date = @static::now($tz)->change($time);
|
||||
} catch (DateMalformedStringException $ignoredException) {
|
||||
$date = null;
|
||||
}
|
||||
// @codeCoverageIgnoreEnd
|
||||
|
||||
if (!$date) {
|
||||
throw new InvalidFormatException("Could not parse '$time': ".$exception->getMessage(), 0, $exception);
|
||||
@@ -354,13 +362,13 @@ trait Creator
|
||||
* If $hour is not null then the default values for $minute and $second
|
||||
* will be 0.
|
||||
*
|
||||
* @param int|null $year
|
||||
* @param int|null $month
|
||||
* @param int|null $day
|
||||
* @param int|null $hour
|
||||
* @param int|null $minute
|
||||
* @param int|null $second
|
||||
* @param DateTimeZone|string|null $tz
|
||||
* @param DateTimeInterface|int|null $year
|
||||
* @param int|null $month
|
||||
* @param int|null $day
|
||||
* @param int|null $hour
|
||||
* @param int|null $minute
|
||||
* @param int|null $second
|
||||
* @param DateTimeZone|string|null $tz
|
||||
*
|
||||
* @throws InvalidFormatException
|
||||
*
|
||||
@@ -368,7 +376,7 @@ trait Creator
|
||||
*/
|
||||
public static function create($year = 0, $month = 1, $day = 1, $hour = 0, $minute = 0, $second = 0, $tz = null)
|
||||
{
|
||||
if (\is_string($year) && !is_numeric($year) || $year instanceof DateTimeInterface) {
|
||||
if ((\is_string($year) && !is_numeric($year)) || $year instanceof DateTimeInterface) {
|
||||
return static::parse($year, $tz ?: (\is_string($month) || $month instanceof DateTimeZone ? $month : null));
|
||||
}
|
||||
|
||||
@@ -634,6 +642,10 @@ trait Creator
|
||||
$time = preg_replace('/^(.*)(am|pm|AM|PM)(.*)$/U', '$1$3 $2', $time);
|
||||
}
|
||||
|
||||
if ($tz === false) {
|
||||
$tz = null;
|
||||
}
|
||||
|
||||
// First attempt to create an instance, so that error messages are based on the unmodified format.
|
||||
$date = self::createFromFormatAndTimezone($format, $time, $tz);
|
||||
$lastErrors = parent::getLastErrors();
|
||||
@@ -651,12 +663,14 @@ trait Creator
|
||||
$tz = clone $mock->getTimezone();
|
||||
}
|
||||
|
||||
// Set microseconds to zero to match behavior of DateTime::createFromFormat()
|
||||
// See https://bugs.php.net/bug.php?id=74332
|
||||
$mock = $mock->copy()->microsecond(0);
|
||||
$mock = $mock->copy();
|
||||
|
||||
// Prepend mock datetime only if the format does not contain non escaped unix epoch reset flag.
|
||||
if (!preg_match("/{$nonEscaped}[!|]/", $format)) {
|
||||
if (preg_match('/[HhGgisvuB]/', $format)) {
|
||||
$mock = $mock->setTime(0, 0);
|
||||
}
|
||||
|
||||
$format = static::MOCK_DATETIME_FORMAT.' '.$format;
|
||||
$time = ($mock instanceof self ? $mock->rawFormat(static::MOCK_DATETIME_FORMAT) : $mock->format(static::MOCK_DATETIME_FORMAT)).' '.$time;
|
||||
}
|
||||
@@ -862,6 +876,19 @@ trait Creator
|
||||
*/
|
||||
public static function createFromLocaleFormat($format, $locale, $time, $tz = null)
|
||||
{
|
||||
$format = preg_replace_callback(
|
||||
'/(?:\\\\[a-zA-Z]|[bfkqCEJKQRV]){2,}/',
|
||||
static function (array $match) use ($locale): string {
|
||||
$word = str_replace('\\', '', $match[0]);
|
||||
$translatedWord = static::translateTimeString($word, $locale, 'en');
|
||||
|
||||
return $word === $translatedWord
|
||||
? $match[0]
|
||||
: preg_replace('/[a-zA-Z]/', '\\\\$0', $translatedWord);
|
||||
},
|
||||
$format
|
||||
);
|
||||
|
||||
return static::rawCreateFromFormat($format, static::translateTimeString($time, $locale, 'en'), $tz);
|
||||
}
|
||||
|
||||
@@ -907,9 +934,9 @@ trait Creator
|
||||
if (\is_string($var)) {
|
||||
$var = trim($var);
|
||||
|
||||
if (!preg_match('/^P[0-9T]/', $var) &&
|
||||
!preg_match('/^R[0-9]/', $var) &&
|
||||
preg_match('/[a-z0-9]/i', $var)
|
||||
if (!preg_match('/^P[\dT]/', $var) &&
|
||||
!preg_match('/^R\d/', $var) &&
|
||||
preg_match('/[a-z\d]/i', $var)
|
||||
) {
|
||||
$date = static::parse($var);
|
||||
}
|
||||
@@ -921,13 +948,20 @@ trait Creator
|
||||
/**
|
||||
* Set last errors.
|
||||
*
|
||||
* @param array $lastErrors
|
||||
* @param array|bool $lastErrors
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private static function setLastErrors(array $lastErrors)
|
||||
private static function setLastErrors($lastErrors)
|
||||
{
|
||||
static::$lastErrors = $lastErrors;
|
||||
if (\is_array($lastErrors) || $lastErrors === false) {
|
||||
static::$lastErrors = \is_array($lastErrors) ? $lastErrors : [
|
||||
'warning_count' => 0,
|
||||
'warnings' => [],
|
||||
'error_count' => 0,
|
||||
'errors' => [],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user