@@ -47,6 +47,26 @@ public function __construct(Rule $eloquent)
47
47
$ this ->eloquent = $ eloquent ;
48
48
}
49
49
50
+ /**
51
+ * Filter the rule.
52
+ *
53
+ * @param array $rule
54
+ * @return array
55
+ */
56
+ public function filterRule (array $ rule ): array
57
+ {
58
+ $ rule = array_values ($ rule );
59
+
60
+ $ i = count ($ rule ) - 1 ;
61
+ for (; $ i >= 0 ; $ i --) {
62
+ if ($ rule [$ i ] != '' && !is_null ($ rule [$ i ])) {
63
+ break ;
64
+ }
65
+ }
66
+
67
+ return array_slice ($ rule , 0 , $ i + 1 );
68
+ }
69
+
50
70
/**
51
71
* savePolicyLine function.
52
72
*
@@ -177,28 +197,51 @@ public function removePolicies(string $sec, string $ptype, array $rules): void
177
197
}
178
198
179
199
/**
180
- * RemoveFilteredPolicy removes policy rules that match the filter from the storage.
181
- * This is part of the Auto-Save feature.
182
- *
183
- * @param string $sec
184
- * @param string $ptype
185
- * @param int $fieldIndex
186
- * @param string ...$fieldValues
200
+ * @param string $sec
201
+ * @param string $ptype
202
+ * @param int $fieldIndex
203
+ * @param string|null ...$fieldValues
204
+ * @return array
205
+ * @throws Throwable
187
206
*/
188
- public function removeFilteredPolicy (string $ sec , string $ ptype , int $ fieldIndex , string ...$ fieldValues ): void
207
+ public function _removeFilteredPolicy (string $ sec , string $ ptype , int $ fieldIndex , ? string ...$ fieldValues ): array
189
208
{
209
+ $ removedRules = [];
190
210
$ instance = $ this ->eloquent ->where ('ptype ' , $ ptype );
191
211
192
212
foreach (range (0 , 5 ) as $ value ) {
193
213
if ($ fieldIndex <= $ value && $ value < $ fieldIndex + count ($ fieldValues )) {
194
214
if ('' != $ fieldValues [$ value - $ fieldIndex ]) {
195
- $ instance ->where ('v ' . strval ($ value ), $ fieldValues [$ value - $ fieldIndex ]);
215
+ $ instance ->where ('v ' . strval ($ value ), $ fieldValues [$ value - $ fieldIndex ]);
196
216
}
197
217
}
198
218
}
199
-
219
+
220
+ $ oldP = $ instance ->get ()->makeHidden (['created_at ' ,'updated_at ' , 'id ' , 'ptype ' ])->toArray ();
221
+ foreach ($ oldP as &$ item ) {
222
+ $ item = $ this ->filterRule ($ item );
223
+ $ removedRules [] = $ item ;
224
+ }
225
+
200
226
$ instance ->delete ();
201
227
Rule::fireModelEvent ('deleted ' );
228
+
229
+ return $ removedRules ;
230
+ }
231
+
232
+ /**
233
+ * RemoveFilteredPolicy removes policy rules that match the filter from the storage.
234
+ * This is part of the Auto-Save feature.
235
+ *
236
+ * @param string $sec
237
+ * @param string $ptype
238
+ * @param int $fieldIndex
239
+ * @param string|null ...$fieldValues
240
+ * @return void
241
+ */
242
+ public function removeFilteredPolicy (string $ sec , string $ ptype , int $ fieldIndex , ?string ...$ fieldValues ): void
243
+ {
244
+ $ this ->_removeFilteredPolicy ($ sec , $ ptype , $ fieldIndex , ...$ fieldValues );
202
245
}
203
246
204
247
/**
@@ -255,44 +298,12 @@ public function updatePolicies(string $sec, string $ptype, array $oldRules, arra
255
298
*/
256
299
public function updateFilteredPolicies (string $ sec , string $ ptype , array $ newPolicies , int $ fieldIndex , string ...$ fieldValues ): array
257
300
{
258
- $ where ['ptype ' ] = $ ptype ;
259
- foreach ($ fieldValues as $ fieldValue ) {
260
- if (!is_null ($ fieldValue ) && $ fieldValue !== '' ) {
261
- $ where ['v ' . $ fieldIndex ++] = $ fieldValue ;
262
- }
263
- }
264
-
265
- $ newP = [];
266
- $ oldP = [];
267
- foreach ($ newPolicies as $ newRule ) {
268
- $ col ['ptype ' ] = $ ptype ;
269
- $ col ['created_at ' ] = new DateTime ();
270
- $ col ['updated_at ' ] = $ col ['created_at ' ];
271
- foreach ($ newRule as $ key => $ value ) {
272
- $ col ['v ' . strval ($ key )] = $ value ;
273
- }
274
- $ newP [] = $ col ;
275
- }
276
-
277
- DB ::transaction (function () use ($ newP , $ where , &$ oldP ) {
278
- $ oldRules = $ this ->eloquent ->where ($ where );
279
- $ oldP = $ oldRules ->get ()->makeHidden (['created_at ' ,'updated_at ' , 'id ' ])->toArray ();
280
-
281
- foreach ($ oldP as &$ item ) {
282
- $ item = array_filter ($ item , function ($ value ) {
283
- return !is_null ($ value ) && $ value !== '' ;
284
- });
285
- unset($ item ['ptype ' ]);
286
- }
287
-
288
- $ oldRules ->delete ();
289
- $ this ->eloquent ->insert ($ newP );
301
+ $ oldRules = [];
302
+ DB ::transaction (function () use ($ sec , $ ptype , $ fieldIndex , $ fieldValues , $ newPolicies , &$ oldRules ) {
303
+ $ oldRules = $ this ->_removeFilteredPolicy ($ sec , $ ptype , $ fieldIndex , ...$ fieldValues );
304
+ $ this ->addPolicies ($ sec , $ ptype , $ newPolicies );
290
305
});
291
-
292
- Rule::fireModelEvent ('saved ' );
293
-
294
- // return deleted rules
295
- return $ oldP ;
306
+ return $ oldRules ;
296
307
}
297
308
298
309
/**
0 commit comments