2
2
3
3
namespace Larapack \AttributePurging ;
4
4
5
- use Exception ;
6
-
7
5
trait Purgeable
8
6
{
9
7
/**
10
8
* @var array List of attribute names which should not be saved to the database.
11
- *
9
+ *
12
10
* protected $purge = [];
13
11
*/
14
-
12
+
15
13
/**
16
14
* @var array List of original attribute values before they were purged.
17
15
*/
18
16
protected $ originalPurgeableValues = [];
19
-
17
+
20
18
/**
21
19
* Boot the purgeable trait for a model.
20
+ *
22
21
* @return void
23
22
*/
24
23
public static function bootPurgeable ()
25
24
{
26
25
/*
27
26
* Remove any purge attributes from the data set
28
27
*/
29
- static ::creating (function ($ model ){
28
+ static ::creating (function ($ model ) {
30
29
$ model ->purgeAttributes ();
31
30
});
32
-
33
- static ::updating (function ($ model ){
31
+
32
+ static ::updating (function ($ model ) {
34
33
$ model ->purgeAttributes ();
35
34
});
36
-
37
- static ::created (function ($ model ){
35
+
36
+ static ::created (function ($ model ) {
38
37
$ model ->restorePurgedValues ();
39
38
});
40
-
41
- static ::updated (function ($ model ){
39
+
40
+ static ::updated (function ($ model ) {
42
41
$ model ->restorePurgedValues ();
43
42
});
44
43
}
45
-
44
+
46
45
/**
47
46
* Removes purged attributes from the dataset, used before saving.
47
+ *
48
48
* @param $attributes mixed Attribute(s) to purge, if unspecified, $purgable property is used
49
+ *
49
50
* @return array Current attribute set
50
51
*/
51
52
public function purgeAttributes ($ attributesToPurge = null )
52
53
{
53
- if ($ attributesToPurge !== null )
54
+ if ($ attributesToPurge !== null ) {
54
55
$ purgeable = is_array ($ attributesToPurge ) ? $ attributesToPurge : [$ attributesToPurge ];
55
- else
56
+ } else {
56
57
$ purgeable = $ this ->getPurgeableAttributes ();
57
-
58
+ }
59
+
58
60
$ attributes = $ this ->getAttributes ();
59
-
61
+
60
62
$ cleanAttributes = array_diff_key ($ attributes , array_flip ($ purgeable ));
61
-
63
+
62
64
$ originalAttributes = array_diff_key ($ attributes , $ cleanAttributes );
63
-
64
- if (is_array ($ this ->originalPurgeableValues ))
65
+
66
+ if (is_array ($ this ->originalPurgeableValues )) {
65
67
$ this ->originalPurgeableValues = array_merge ($ this ->originalPurgeableValues , $ originalAttributes );
66
- else
68
+ } else {
67
69
$ this ->originalPurgeableValues = $ originalAttributes ;
68
-
70
+ }
71
+
69
72
return $ this ->attributes = $ cleanAttributes ;
70
73
}
71
-
74
+
72
75
/**
73
76
* Returns a collection of fields that will be hashed.
74
77
*/
75
78
public function getPurgeableAttributes ()
76
79
{
77
- if (property_exists (get_called_class (), 'purge ' ))
78
- {
79
- return $ this ->purge ;
80
- }
81
-
82
- return [];
83
- }
84
-
80
+ if (property_exists (get_called_class (), 'purge ' )) {
81
+ return $ this ->purge ;
82
+ }
83
+
84
+ return [];
85
+ }
86
+
85
87
/**
86
88
* Returns the original values of any purged attributes.
87
89
*/
88
90
public function getOriginalPurgeValues ()
89
91
{
90
92
return $ this ->originalPurgeableValues ;
91
93
}
92
-
94
+
93
95
/**
94
96
* Returns the original values of any purged attributes.
95
97
*/
@@ -99,13 +101,14 @@ public function getOriginalPurgeValue($attribute)
99
101
? $ this ->originalPurgeableValues [$ attribute ]
100
102
: null ;
101
103
}
102
-
104
+
103
105
/**
104
106
* Restores the original values of any purged attributes.
105
107
*/
106
108
public function restorePurgedValues ()
107
109
{
108
110
$ this ->attributes = array_merge ($ this ->getAttributes (), $ this ->originalPurgeableValues );
111
+
109
112
return $ this ;
110
113
}
111
114
}
0 commit comments