55use tauthz \model \Rule ;
66use tauthz \cache \CacheHandlerContract ;
77use Casbin \Model \Model ;
8- use Casbin \Persist \Adapter ;
9- use Casbin \Persist \AdapterHelper ;
10- use Casbin \Persist \UpdatableAdapter ;
11- use Casbin \Persist \BatchAdapter ;
12- use Casbin \Persist \FilteredAdapter ;
8+ use Casbin \Persist \{Adapter , AdapterHelper , UpdatableAdapter , BatchAdapter , FilteredAdapter };
139use Casbin \Persist \Adapters \Filter ;
1410use Casbin \Exceptions \InvalidFilterTypeException ;
1511use tauthz \traits \Configurable ;
@@ -27,21 +23,21 @@ class DatabaseAdapter implements Adapter, UpdatableAdapter, BatchAdapter, Filter
2723 /**
2824 * @var bool
2925 */
30- private $ filtered = false ;
26+ private bool $ filtered = false ;
3127
3228 /**
3329 * Rules model.
3430 *
3531 * @var Rule
3632 */
37- protected $ model ;
33+ protected Rule $ model ;
3834
3935 /**
4036 * Cache Handler.
4137 *
4238 * @var CacheHandlerContract
4339 */
44- protected $ cacheHandler ;
40+ protected CacheHandlerContract $ cacheHandler ;
4541
4642 /**
4743 * the DatabaseAdapter constructor.
@@ -84,12 +80,13 @@ public function filterRule(array $rule): array
8480 *
8581 * @return void
8682 */
87- public function savePolicyLine ($ ptype , array $ rule )
83+ public function savePolicyLine (string $ ptype , array $ rule ): void
8884 {
8985 $ col ['ptype ' ] = $ ptype ;
9086 foreach ($ rule as $ key => $ value ) {
91- $ col ['v ' . strval ($ key ). '' ] = $ value ;
87+ $ col ['v ' . strval ($ key ) . '' ] = $ value ;
9288 }
89+
9390 $ this ->cacheHandler ->cachePolicies ($ this ->model )->insert ($ col );
9491 }
9592
@@ -100,7 +97,8 @@ public function savePolicyLine($ptype, array $rule)
10097 */
10198 public function loadPolicy (Model $ model ): void
10299 {
103- $ rows = $ this ->cacheHandler ->cachePolicies ($ this ->model )->field (['ptype ' , 'v0 ' , 'v1 ' , 'v2 ' , 'v3 ' , 'v4 ' , 'v5 ' ])->select ()->toArray ();
100+ $ rows = $ this ->cacheHandler ->cachePolicies ($ this ->model )->field (['ptype ' , 'v0 ' , 'v1 ' , 'v2 ' , 'v3 ' , 'v4 ' , 'v5 ' ])
101+ ->select ()->toArray ();
104102 foreach ($ rows as $ row ) {
105103 $ this ->loadPolicyArray ($ this ->filterRule ($ row ), $ model );
106104 }
@@ -160,6 +158,7 @@ public function addPolicies(string $sec, string $ptype, array $rules): void
160158 $ cols [$ i ++] = $ temp ;
161159 $ temp = [];
162160 }
161+
163162 $ this ->cacheHandler ->cachePolicies ($ this ->model )->insertAll ($ cols );
164163 }
165164
@@ -177,7 +176,7 @@ public function removePolicy(string $sec, string $ptype, array $rule): void
177176 $ instance = $ this ->model ->where ('ptype ' , $ ptype );
178177
179178 foreach ($ rule as $ key => $ value ) {
180- $ instance ->where ('v ' . strval ($ key ), $ value );
179+ $ instance ->where ('v ' . strval ($ key ), $ value );
181180 }
182181
183182 foreach ($ instance ->select () as $ model ) {
@@ -234,7 +233,7 @@ public function _removeFilteredPolicy(string $sec, string $ptype, int $fieldInde
234233 ++$ count ;
235234 }
236235 }
237-
236+
238237 return $ removedRules ;
239238 }
240239
@@ -271,7 +270,7 @@ public function updatePolicy(string $sec, string $ptype, array $oldRule, array $
271270
272271 foreach ($ newPolicy as $ key => $ value ) {
273272 $ column = 'v ' . strval ($ key );
274- $ instance ->$ column = $ value ;
273+ $ instance ->{ $ column} = $ value ;
275274 }
276275
277276 $ instance ->save ();
@@ -360,14 +359,12 @@ public function loadFilteredPolicy(Model $model, $filter): void
360359 }
361360 $ rows = $ instance ->select ()->hidden (['id ' ])->toArray ();
362361 foreach ($ rows as $ row ) {
363- $ row = array_filter ($ row , function ($ value ) {
364- return !is_null ($ value ) && $ value !== '' ;
365- });
366- $ line = implode (', ' , array_filter ($ row , function ($ val ) {
367- return '' != $ val && !is_null ($ val );
368- }));
362+ $ row = array_filter ($ row , fn ($ value ) => !is_null ($ value ) && $ value !== '' );
363+ $ line = implode (', ' , array_filter ($ row , fn ($ val ) => '' != $ val && !is_null ($ val )));
364+
369365 $ this ->loadPolicyLine (trim ($ line ), $ model );
370366 }
367+
371368 $ this ->setFiltered (true );
372369 }
373370}
0 commit comments