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