|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ianrizky\Illuminate\Database\Eloquent\Concerns; |
|
4
|
|
|
|
|
5
|
|
|
use Ianrizky\Illuminate\Support\Traits\Tappable as BaseTappable; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
7
|
|
|
|
|
8
|
|
|
trait Tappable |
|
9
|
|
|
{ |
|
10
|
|
|
use BaseTappable; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Call the given Closure with this instance, save the instance into storage, then return the instance. |
|
14
|
|
|
* |
|
15
|
|
|
* @param callable|null $callback |
|
16
|
|
|
* @return $this |
|
17
|
|
|
*/ |
|
18
|
|
|
public function tapAndSave(callable $callback = null) |
|
19
|
|
|
{ |
|
20
|
|
|
return $this->tap($callback)->tap(function (Model $model) { |
|
21
|
|
|
$model->save(); |
|
22
|
|
|
}); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Call the given Closure with this instance, save the instance into storage, then return the instance if the given condition is true. |
|
27
|
|
|
* |
|
28
|
|
|
* @param mixed $condition |
|
29
|
|
|
* @param callable|null $callback |
|
30
|
|
|
* @return $this |
|
31
|
|
|
*/ |
|
32
|
|
|
public function tapAndSaveIf($condition, callable $callback = null) |
|
33
|
|
|
{ |
|
34
|
|
|
return $this->tapIf($condition, $callback)->tapIf($condition, function (Model $model) { |
|
35
|
|
|
$model->save(); |
|
36
|
|
|
}); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Call the given Closure with this instance, save the instance into storage, then return the instance unless the given condition is true. |
|
41
|
|
|
* |
|
42
|
|
|
* @param mixed $condition |
|
43
|
|
|
* @param callable|null $callback |
|
44
|
|
|
* @return $this |
|
45
|
|
|
*/ |
|
46
|
|
|
public function tapAndSaveUnless($condition, callable $callback = null) |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->tapUnless($condition, $callback)->tapUnless($condition, function (Model $model) { |
|
49
|
|
|
$model->save(); |
|
50
|
|
|
}); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Save the instance into storage, call the given Closure with this instance, then return the instance. |
|
55
|
|
|
* |
|
56
|
|
|
* @param callable|null $callback |
|
57
|
|
|
* @return $this|\Illuminate\Support\HigherOrderTapProxy |
|
58
|
|
|
*/ |
|
59
|
|
|
public function saveAndTap(callable $callback = null) |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->tap(function (Model $model) { |
|
62
|
|
|
$model->save(); |
|
63
|
|
|
})->tap($callback); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Save the instance into storage, call the given Closure with this instance, then return the instance if the given condition is true. |
|
68
|
|
|
* |
|
69
|
|
|
* @param mixed $condition |
|
70
|
|
|
* @param callable|null $callback |
|
71
|
|
|
* @return $this|\Illuminate\Support\HigherOrderTapProxy |
|
72
|
|
|
*/ |
|
73
|
|
|
public function saveAndTapIf($condition, callable $callback = null) |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->tapIf($condition, function (Model $model) { |
|
76
|
|
|
$model->save(); |
|
77
|
|
|
})->tapIf($condition, $callback); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Save the instance into storage, call the given Closure with this instance, then return the instance unless the given condition is true. |
|
82
|
|
|
* |
|
83
|
|
|
* @param mixed $condition |
|
84
|
|
|
* @param callable|null $callback |
|
85
|
|
|
* @return $this|\Illuminate\Support\HigherOrderTapProxy |
|
86
|
|
|
*/ |
|
87
|
|
|
public function saveAndTapUnless($condition, callable $callback = null) |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->tapUnless($condition, function (Model $model) { |
|
90
|
|
|
$model->save(); |
|
91
|
|
|
})->tapUnless($condition, $callback); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|