|
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
|
|
|
/** |
|
40
|
|
|
* Register hook on save method. |
|
41
|
|
|
* |
|
42
|
|
|
* @codeCoverageIgnore |
|
43
|
|
|
* |
|
44
|
|
|
* @return \Closure |
|
45
|
|
|
*/ |
|
46
|
|
|
public function save() |
|
47
|
|
|
{ |
|
48
|
|
|
return function ($next, $value, $args) { |
|
49
|
|
|
$this->saveMapped(); |
|
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
return $next($value, $args); |
|
52
|
|
|
}; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Register hook on isset call. |
|
57
|
|
|
* |
|
58
|
|
|
* @codeCoverageIgnore |
|
59
|
|
|
* |
|
60
|
|
|
* @return \Closure |
|
61
|
|
|
*/ |
|
62
|
|
View Code Duplication |
public function __issetHook() |
|
|
|
|
|
|
63
|
|
|
{ |
|
64
|
|
|
return function ($next, $isset, $args) { |
|
65
|
|
|
$key = $args->get('key'); |
|
66
|
|
|
|
|
67
|
|
|
if (!$isset && $this->hasMapping($key)) { |
|
|
|
|
|
|
68
|
|
|
return (bool) $this->mapAttribute($key); |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $next($isset, $args); |
|
72
|
|
|
}; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Register hook on unset call. |
|
77
|
|
|
* |
|
78
|
|
|
* @codeCoverageIgnore |
|
79
|
|
|
* |
|
80
|
|
|
* @return \Closure |
|
81
|
|
|
*/ |
|
82
|
|
|
public function __unsetHook() |
|
83
|
|
|
{ |
|
84
|
|
|
return function ($next, $value, $args) { |
|
85
|
|
|
$key = $args->get('key'); |
|
86
|
|
|
|
|
87
|
|
|
if ($this->hasMapping($key)) { |
|
|
|
|
|
|
88
|
|
|
return $this->forget($key); |
|
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $next($value, $args); |
|
92
|
|
|
}; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Register hook on getAttribute method. |
|
97
|
|
|
* |
|
98
|
|
|
* @codeCoverageIgnore |
|
99
|
|
|
* |
|
100
|
|
|
* @return \Closure |
|
101
|
|
|
*/ |
|
102
|
|
View Code Duplication |
public function getAttribute() |
|
|
|
|
|
|
103
|
|
|
{ |
|
104
|
|
|
return function ($next, $value, $args) { |
|
105
|
|
|
$key = $args->get('key'); |
|
106
|
|
|
|
|
107
|
|
|
if ($this->hasMapping($key)) { |
|
|
|
|
|
|
108
|
|
|
$value = $this->mapAttribute($key); |
|
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $next($value, $args); |
|
112
|
|
|
}; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Register hook on setAttribute method. |
|
117
|
|
|
* |
|
118
|
|
|
* @codeCoverageIgnore |
|
119
|
|
|
* |
|
120
|
|
|
* @return \Closure |
|
121
|
|
|
*/ |
|
122
|
|
|
public function setAttribute() |
|
123
|
|
|
{ |
|
124
|
|
|
return function ($next, $value, $args) { |
|
125
|
|
|
$key = $args->get('key'); |
|
126
|
|
|
|
|
127
|
|
|
if ($this->hasMapping($key)) { |
|
|
|
|
|
|
128
|
|
|
return $this->setMappedAttribute($key, $value); |
|
|
|
|
|
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return $next($value, $args); |
|
132
|
|
|
}; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
View Code Duplication |
public function __call($method, $params) |
|
|
|
|
|
|
136
|
|
|
{ |
|
137
|
|
|
if (strpos($method, '__') === 0 && method_exists($this, $method.'Hook')) { |
|
138
|
|
|
return call_user_func_array([$this, $method.'Hook'], $params); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
throw new BadMethodCallException("Method [{$method}] doesn't exist on this object."); |
|
142
|
|
|
} |
|
143
|
|
|
/** |
|
144
|
|
|
* Register hook on toArray method. |
|
145
|
|
|
* |
|
146
|
|
|
* @codeCoverageIgnore |
|
147
|
|
|
* |
|
148
|
|
|
* @return \Closure |
|
149
|
|
|
*/ |
|
150
|
|
|
public function toArray() |
|
151
|
|
|
{ |
|
152
|
|
|
return function ($next, $attributes) { |
|
153
|
|
|
$mappedAttributes = []; |
|
154
|
|
|
foreach ($attributes as $column => $value) { |
|
155
|
|
|
if ($key = $this->isMapping($column)) { |
|
|
|
|
|
|
156
|
|
|
$column = $key; |
|
157
|
|
|
} |
|
158
|
|
|
$mappedAttributes[$column] = $value; |
|
159
|
|
|
}; |
|
160
|
|
|
return $next($mappedAttributes); |
|
161
|
|
|
}; |
|
162
|
|
|
} |
|
163
|
|
|
} |
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: