PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.16.9
GiveWP – Donation Plugin and Fundraising Platform v4.16.9
4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 All 255 releases
give / src / Framework / FieldsAPI / Concerns / Macroable.php

Macroable.php in GiveWP – Donation Plugin and Fundraising Platform 4.16.9, at src/Framework/FieldsAPI/Concerns/Macroable.php

79 lines 1.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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