Skip to content

Commit

Permalink
Allow to remove macro by passing null value
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Feb 5, 2024
1 parent c8f9c77 commit 9316398
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 26 deletions.
9 changes: 3 additions & 6 deletions src/Carbon/CarbonInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3146,6 +3146,8 @@ public function lte(DateTimeInterface|string $date): bool;
/**
* Register a custom macro.
*
* Pass null macro to remove it.
*
* @example
* ```
* $userSettings = [
Expand All @@ -3157,13 +3159,8 @@ public function lte(DateTimeInterface|string $date): bool;
* });
* echo Carbon::yesterday()->hours(11)->userFormat();
* ```
*
* @param string $name
* @param object|callable $macro
*
* @return void
*/
public static function macro(string $name, callable|object $macro): void;
public static function macro(string $name, callable|object|null $macro): void;

/**
* Make a Carbon instance from given variable if possible.
Expand Down
9 changes: 3 additions & 6 deletions src/Carbon/CarbonInterval.php
Original file line number Diff line number Diff line change
Expand Up @@ -1469,20 +1469,17 @@ public function isEmpty(): bool
/**
* Register a custom macro.
*
* Pass null macro to remove it.
*
* @example
* ```
* CarbonInterval::macro('twice', function () {
* return $this->times(2);
* });
* echo CarbonInterval::hours(2)->twice();
* ```
*
* @param string $name
* @param object|callable $macro
*
* @return void
*/
public static function macro(string $name, $macro): void
public static function macro(string $name, object|callable|null $macro): void
{
static::$macros[$name] = $macro;
}
Expand Down
9 changes: 3 additions & 6 deletions src/Carbon/CarbonPeriod.php
Original file line number Diff line number Diff line change
Expand Up @@ -514,20 +514,17 @@ private static function makeInterval(mixed $input): ?CarbonInterval
/**
* Register a custom macro.
*
* Pass null macro to remove it.
*
* @example
* ```
* CarbonPeriod::macro('middle', function () {
* return $this->getStartDate()->average($this->getEndDate());
* });
* echo CarbonPeriod::since('2011-05-12')->until('2011-06-03')->middle();
* ```
*
* @param string $name
* @param object|callable $macro
*
* @return void
*/
public static function macro(string $name, $macro): void
public static function macro(string $name, object|callable|null $macro): void
{
static::$macros[$name] = $macro;
}
Expand Down
8 changes: 5 additions & 3 deletions src/Carbon/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,19 +305,21 @@ public function getHumanDiffOptions(): int
/**
* Register a custom macro.
*
* Pass null macro to remove it.
*
* @example
* ```
* $userSettings = [
* 'locale' => 'pt',
* 'timezone' => 'America/Sao_Paulo',
* ];
* Carbon::macro('userFormat', function () use ($userSettings) {
* $factory->macro('userFormat', function () use ($userSettings) {
* return $this->copy()->locale($userSettings['locale'])->tz($userSettings['timezone'])->calendar();
* });
* echo Carbon::yesterday()->hours(11)->userFormat();
* echo $factory->yesterday()->hours(11)->userFormat();
* ```
*/
public function macro(string $name, object|callable $macro): void
public function macro(string $name, object|callable|null $macro): void
{
$macros = $this->getSettings()['macros'] ?? [];
$macros[$name] = $macro;
Expand Down
7 changes: 2 additions & 5 deletions src/Carbon/Traits/Macro.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,9 @@ trait Macro
* echo Carbon::yesterday()->hours(11)->userFormat();
* ```
*
* @param string $name
* @param object|callable $macro
*
* @return void
* Pass null macro to remove it.
*/
public static function macro(string $name, object|callable $macro): void
public static function macro(string $name, object|callable|null $macro): void
{
FactoryImmutable::getDefaultInstance()->macro($name, $macro);
}
Expand Down

0 comments on commit 9316398

Please sign in to comment.