1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sofa\Eloquence\Mappable; |
4
|
|
|
|
5
|
|
|
use BadMethodCallException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* This class provides instance scope for the closures |
9
|
|
|
* so they can be rebound later onto the acutal model. |
10
|
|
|
*/ |
11
|
|
|
class Hooks |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Register hook on customWhere method. |
15
|
|
|
* |
16
|
|
|
* @codeCoverageIgnore |
17
|
|
|
* |
18
|
|
|
* @return \Closure |
19
|
|
|
*/ |
20
|
|
|
public function queryHook() |
21
|
|
|
{ |
22
|
|
|
return function ($next, $query, $bag) { |
23
|
|
|
$method = $bag->get('method'); |
24
|
|
|
$args = $bag->get('args'); |
25
|
|
|
$column = $args->get('column'); |
26
|
|
|
|
27
|
|
|
if ($this->hasMapping($column)) { |
|
|
|
|
28
|
|
|
return call_user_func_array([$this, 'mappedQuery'], [$query, $method, $args]); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
View Code Duplication |
if (in_array($method, ['select', 'addSelect'])) { |
|
|
|
|
32
|
|
|
call_user_func_array([$this, 'mappedSelect'], [$query, $args]); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
return $next($query, $bag); |
36
|
|
|
}; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function isDirty() |
40
|
|
|
{ |
41
|
|
|
return function ($next, $attributes = null, $bag) { |
42
|
|
|
if (is_array($attributes)) { |
43
|
|
|
$attributes = array_map(function ($attribute) { |
44
|
|
|
return $this->getMappingForAttribute($attribute) ?: $attribute; |
|
|
|
|
45
|
|
|
}, $attributes); |
46
|
|
|
} |
47
|
|
|
return $next($attributes, $bag); |
48
|
|
|
}; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Register hook on save method. |
53
|
|
|
* |
54
|
|
|
* @codeCoverageIgnore |
55
|
|
|
* |
56
|
|
|
* @return \Closure |
57
|
|
|
*/ |
58
|
|
|
public function save() |
59
|
|
|
{ |
60
|
|
|
return function ($next, $value, $args) { |
61
|
|
|
$this->saveMapped(); |
|
|
|
|
62
|
|
|
|
63
|
|
|
return $next($value, $args); |
64
|
|
|
}; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Register hook on isset call. |
69
|
|
|
* |
70
|
|
|
* @codeCoverageIgnore |
71
|
|
|
* |
72
|
|
|
* @return \Closure |
73
|
|
|
*/ |
74
|
|
View Code Duplication |
public function __issetHook() |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
return function ($next, $isset, $args) { |
77
|
|
|
$key = $args->get('key'); |
78
|
|
|
|
79
|
|
|
if (!$isset && $this->hasMapping($key)) { |
|
|
|
|
80
|
|
|
return (bool) $this->mapAttribute($key); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $next($isset, $args); |
84
|
|
|
}; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Register hook on unset call. |
89
|
|
|
* |
90
|
|
|
* @codeCoverageIgnore |
91
|
|
|
* |
92
|
|
|
* @return \Closure |
93
|
|
|
*/ |
94
|
|
|
public function __unsetHook() |
95
|
|
|
{ |
96
|
|
|
return function ($next, $value, $args) { |
97
|
|
|
$key = $args->get('key'); |
98
|
|
|
|
99
|
|
|
if ($this->hasMapping($key)) { |
|
|
|
|
100
|
|
|
return $this->forget($key); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $next($value, $args); |
104
|
|
|
}; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Register hook on getAttribute method. |
109
|
|
|
* |
110
|
|
|
* @codeCoverageIgnore |
111
|
|
|
* |
112
|
|
|
* @return \Closure |
113
|
|
|
*/ |
114
|
|
View Code Duplication |
public function getAttribute() |
|
|
|
|
115
|
|
|
{ |
116
|
|
|
return function ($next, $value, $args) { |
117
|
|
|
$key = $args->get('key'); |
118
|
|
|
|
119
|
|
|
if ($this->hasMapping($key)) { |
|
|
|
|
120
|
|
|
$value = $this->mapAttribute($key); |
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $next($value, $args); |
124
|
|
|
}; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Register hook on setAttribute method. |
129
|
|
|
* |
130
|
|
|
* @codeCoverageIgnore |
131
|
|
|
* |
132
|
|
|
* @return \Closure |
133
|
|
|
*/ |
134
|
|
|
public function setAttribute() |
135
|
|
|
{ |
136
|
|
|
return function ($next, $value, $args) { |
137
|
|
|
$key = $args->get('key'); |
138
|
|
|
|
139
|
|
|
if ($this->hasMapping($key)) { |
|
|
|
|
140
|
|
|
return $this->setMappedAttribute($key, $value); |
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $next($value, $args); |
144
|
|
|
}; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
View Code Duplication |
public function __call($method, $params) |
|
|
|
|
148
|
|
|
{ |
149
|
|
|
if (strpos($method, '__') === 0 && method_exists($this, $method.'Hook')) { |
150
|
|
|
return call_user_func_array([$this, $method.'Hook'], $params); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
throw new BadMethodCallException("Method [{$method}] doesn't exist on this object."); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
If you implement
__call
and 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
__call
is implemented by a parent class and only the child class knows which methods exist: