|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Rinvex\Support\Traits; |
|
6
|
|
|
|
|
7
|
|
|
use Error; |
|
8
|
|
|
use Exception; |
|
9
|
|
|
use BadMethodCallException; |
|
10
|
|
|
use Illuminate\Support\Traits\Macroable as BaseMacroable; |
|
11
|
|
|
|
|
12
|
|
|
trait Macroable |
|
13
|
|
|
{ |
|
14
|
|
|
use BaseMacroable { |
|
15
|
|
|
BaseMacroable::__call as macroableCall; |
|
16
|
|
|
BaseMacroable::__callStatic as macroableCallStatic; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Handle dynamic method calls into the model. |
|
21
|
|
|
* |
|
22
|
|
|
* @param string $method |
|
23
|
|
|
* @param array $parameters |
|
24
|
|
|
* |
|
25
|
|
|
* @return mixed |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __call($method, $parameters) |
|
28
|
|
|
{ |
|
29
|
|
|
if (in_array($method, ['increment', 'decrement'])) { |
|
30
|
|
|
return $this->{$method}(...$parameters); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
try { |
|
34
|
|
|
// Catch non-static calls for static methods |
|
35
|
|
|
if (method_exists(static::class, $method)) { |
|
36
|
|
|
return static::{$method}(...$parameters); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
if ($resolver = (static::$relationResolvers[get_class($this)][$method] ?? null)) { |
|
40
|
|
|
return $resolver($this); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return $this->forwardCallTo($this->newQuery(), $method, $parameters); |
|
|
|
|
|
|
44
|
|
|
} catch (Error | BadMethodCallException $e) { |
|
45
|
|
|
if ($method !== 'macroableCall') { |
|
46
|
|
|
return $this->macroableCall($method, $parameters); |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Handle dynamic static method calls into the method. |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $method |
|
55
|
|
|
* @param array $parameters |
|
56
|
|
|
* |
|
57
|
|
|
* @return mixed |
|
58
|
|
|
*/ |
|
59
|
|
|
public static function __callStatic($method, $parameters) |
|
60
|
|
|
{ |
|
61
|
|
|
try { |
|
62
|
|
|
// Catch static calls for non-static methods |
|
63
|
|
|
return (new static())->{$method}(...$parameters); |
|
64
|
|
|
} catch (Exception $e) { |
|
65
|
|
|
if ($method !== 'macroableCallStatic') { |
|
66
|
|
|
return (new static())::macroableCallStatic($method, $parameters); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: