| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* This file is part of the Carbon package. |
| 5 |
* |
| 6 |
* (c) Brian Nesbitt <[email protected]> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
namespace Metricool\Vendor\Carbon\Traits; |
| 12 |
|
| 13 |
/** |
| 14 |
* Trait Macros. |
| 15 |
* |
| 16 |
* Allows users to register macros within the Carbon class. |
| 17 |
*/ |
| 18 |
trait Macro |
| 19 |
{ |
| 20 |
use Mixin; |
| 21 |
/** |
| 22 |
* The registered macros. |
| 23 |
* |
| 24 |
* @var array |
| 25 |
*/ |
| 26 |
protected static $globalMacros = []; |
| 27 |
/** |
| 28 |
* The registered generic macros. |
| 29 |
* |
| 30 |
* @var array |
| 31 |
*/ |
| 32 |
protected static $globalGenericMacros = []; |
| 33 |
/** |
| 34 |
* Register a custom macro. |
| 35 |
* |
| 36 |
* @example |
| 37 |
* ``` |
| 38 |
* $userSettings = [ |
| 39 |
* 'locale' => 'pt', |
| 40 |
* 'timezone' => 'America/Sao_Paulo', |
| 41 |
* ]; |
| 42 |
* Carbon::macro('userFormat', function () use ($userSettings) { |
| 43 |
* return $this->copy()->locale($userSettings['locale'])->tz($userSettings['timezone'])->calendar(); |
| 44 |
* }); |
| 45 |
* echo Carbon::yesterday()->hours(11)->userFormat(); |
| 46 |
* ``` |
| 47 |
* |
| 48 |
* @param string $name |
| 49 |
* @param object|callable $macro |
| 50 |
* |
| 51 |
* @return void |
| 52 |
*/ |
| 53 |
public static function macro($name, $macro) |
| 54 |
{ |
| 55 |
static::$globalMacros[$name] = $macro; |
| 56 |
} |
| 57 |
/** |
| 58 |
* Remove all macros and generic macros. |
| 59 |
*/ |
| 60 |
public static function resetMacros() |
| 61 |
{ |
| 62 |
static::$globalMacros = []; |
| 63 |
static::$globalGenericMacros = []; |
| 64 |
} |
| 65 |
/** |
| 66 |
* Register a custom macro. |
| 67 |
* |
| 68 |
* @param object|callable $macro |
| 69 |
* @param int $priority marco with higher priority is tried first |
| 70 |
* |
| 71 |
* @return void |
| 72 |
*/ |
| 73 |
public static function genericMacro($macro, $priority = 0) |
| 74 |
{ |
| 75 |
if (!isset(static::$globalGenericMacros[$priority])) { |
| 76 |
static::$globalGenericMacros[$priority] = []; |
| 77 |
krsort(static::$globalGenericMacros, \SORT_NUMERIC); |
| 78 |
} |
| 79 |
static::$globalGenericMacros[$priority][] = $macro; |
| 80 |
} |
| 81 |
/** |
| 82 |
* Checks if macro is registered globally. |
| 83 |
* |
| 84 |
* @param string $name |
| 85 |
* |
| 86 |
* @return bool |
| 87 |
*/ |
| 88 |
public static function hasMacro($name) |
| 89 |
{ |
| 90 |
return isset(static::$globalMacros[$name]); |
| 91 |
} |
| 92 |
/** |
| 93 |
* Get the raw callable macro registered globally for a given name. |
| 94 |
* |
| 95 |
* @param string $name |
| 96 |
* |
| 97 |
* @return callable|null |
| 98 |
*/ |
| 99 |
public static function getMacro($name) |
| 100 |
{ |
| 101 |
return static::$globalMacros[$name] ?? null; |
| 102 |
} |
| 103 |
/** |
| 104 |
* Checks if macro is registered globally or locally. |
| 105 |
* |
| 106 |
* @param string $name |
| 107 |
* |
| 108 |
* @return bool |
| 109 |
*/ |
| 110 |
public function hasLocalMacro($name) |
| 111 |
{ |
| 112 |
return $this->localMacros && isset($this->localMacros[$name]) || static::hasMacro($name); |
| 113 |
} |
| 114 |
/** |
| 115 |
* Get the raw callable macro registered globally or locally for a given name. |
| 116 |
* |
| 117 |
* @param string $name |
| 118 |
* |
| 119 |
* @return callable|null |
| 120 |
*/ |
| 121 |
public function getLocalMacro($name) |
| 122 |
{ |
| 123 |
return ($this->localMacros ?? [])[$name] ?? static::getMacro($name); |
| 124 |
} |
| 125 |
} |
| 126 |
|