AllowMultiple.php
4 years ago
HasDefaultValue.php
4 years ago
HasEmailTag.php
4 years ago
HasHelpText.php
4 years ago
HasLabel.php
4 years ago
HasMaxLength.php
4 years ago
HasMinLength.php
4 years ago
HasName.php
4 years ago
HasNodes.php
4 years ago
HasOptions.php
4 years ago
HasPlaceholder.php
4 years ago
HasType.php
4 years ago
HasVisibilityConditions.php
4 years ago
InsertNode.php
4 years ago
IsReadOnly.php
4 years ago
IsRequired.php
4 years ago
Macroable.php
4 years ago
MoveNode.php
4 years ago
MoveNodeProxy.php
4 years ago
NameCollision.php
4 years ago
RemoveNode.php
4 years ago
SerializeAsJson.php
4 years ago
ShowInReceipt.php
4 years ago
StoreAsMeta.php
4 years ago
ValidationRules.php
4 years ago
WalkNodes.php
4 years ago
Macroable.php
79 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Framework\FieldsAPI\Concerns; |
| 4 | |
| 5 | use BadMethodCallException; |
| 6 | use Closure; |
| 7 | |
| 8 | /** |
| 9 | * @since 2.17.0 |
| 10 | */ |
| 11 | trait Macroable |
| 12 | { |
| 13 | |
| 14 | /** @var array */ |
| 15 | protected static $macros = []; |
| 16 | |
| 17 | /** |
| 18 | * Add a macro to the class. |
| 19 | * |
| 20 | * @since 2.17.0 |
| 21 | * |
| 22 | * @param string $name |
| 23 | * @param callable $macro |
| 24 | * |
| 25 | * @return void |
| 26 | */ |
| 27 | public static function macro($name, callable $macro) |
| 28 | { |
| 29 | static::$macros[$name] = $macro; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Check if the class has the named macro. |
| 34 | * |
| 35 | * @since 2.17.0 |
| 36 | * |
| 37 | * @param string $name |
| 38 | * |
| 39 | * @return bool |
| 40 | */ |
| 41 | public static function hasMacro($name) |
| 42 | { |
| 43 | return isset(static::$macros[$name]); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Call the macro |
| 48 | * |
| 49 | * @since 2.17.0 |
| 50 | * |
| 51 | * @param string $method |
| 52 | * @param array $parameters |
| 53 | * |
| 54 | * @return mixed |
| 55 | * |
| 56 | * @throws BadMethodCallException |
| 57 | */ |
| 58 | public function __call($method, array $parameters) |
| 59 | { |
| 60 | if ( ! static::hasMacro($method)) { |
| 61 | throw new BadMethodCallException( |
| 62 | sprintf( |
| 63 | 'Method %s::%s does not exist', |
| 64 | $method, |
| 65 | static::class |
| 66 | ) |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | $macro = static::$macros[$method]; |
| 71 | |
| 72 | if ($macro instanceof Closure) { |
| 73 | $macro = $macro->bindTo($this, static::class); |
| 74 | } |
| 75 | |
| 76 | return $macro(...$parameters); |
| 77 | } |
| 78 | } |
| 79 |