Carbon.php
TLDR
The Carbon.php
file in the Illuminate\Support
namespace is a class that extends the BaseCarbon
class. It also includes two traits, Conditionable
and Dumpable
. It provides methods for setting the test time and creating a Carbon
instance from a UUID or ULID.
Methods
setTestNow
This method sets the "test now" time for the Carbon
class. It accepts a parameter testNow
which is the date and time to set as the test time. If no parameter is provided, the test time is set to null
.
createFromId
This method creates a Carbon
instance from a given ordered UUID or ULID. It accepts a parameter id
which can be an instance of the Ramsey\Uuid\Uuid
class, the Symfony\Component\Uid\Ulid
class, or a string representing a UUID or ULID. The method checks if the id
is a valid ULID and creates a Carbon
instance accordingly.
Classes
Carbon
This class extends the BaseCarbon
class and includes the Conditionable
and Dumpable
traits. It provides methods for setting the test time and creating a Carbon
instance from a UUID or ULID.
<?php
namespace Illuminate\Support;
use Carbon\Carbon as BaseCarbon;
use Carbon\CarbonImmutable as BaseCarbonImmutable;
use Illuminate\Support\Traits\Conditionable;
use Illuminate\Support\Traits\Dumpable;
use Ramsey\Uuid\Uuid;
use Symfony\Component\Uid\Ulid;
class Carbon extends BaseCarbon
{
use Conditionable, Dumpable;
/**
* {@inheritdoc}
*/
public static function setTestNow($testNow = null)
{
BaseCarbon::setTestNow($testNow);
BaseCarbonImmutable::setTestNow($testNow);
}
/**
* Create a Carbon instance from a given ordered UUID or ULID.
*
* @param \Ramsey\Uuid\Uuid|\Symfony\Component\Uid\Ulid|string $id
* @return \Illuminate\Support\Carbon
*/
public static function createFromId($id)
{
return Ulid::isValid($id)
? static::createFromInterface(Ulid::fromString($id)->getDateTime())
: static::createFromInterface(Uuid::fromString($id)->getDateTime());
}
}