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