Policy::getFilteredPolicy()   A
last analyzed

Complexity

Conditions 6
Paths 7

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 20
ccs 11
cts 11
cp 1
rs 9.2222
cc 6
nc 7
nop 4
crap 6
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
declare(strict_types=1);
4
5
namespace Casbin\Model;
6
7
use ArrayAccess;
8
use Casbin\Constant\Constants;
9
use Casbin\Exceptions\CasbinException;
10
use Casbin\Log\Log;
11
use Casbin\Rbac\RoleManager;
12
use Casbin\Util\Util;
13
14
/**
15
 * Class Policy.
16
 *
17
 * @package Casbin\Model
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 4 spaces but found 1
Loading history...
18
 * @implements ArrayAccess<string, array<string, Assertion>>
19
 * @author [email protected]
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Coding Style introduced by
Tag value for @author tag indented incorrectly; expected 5 spaces but found 1
Loading history...
20
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
21
abstract class Policy implements ArrayAccess
22
{
23
    public const POLICY_ADD = 0;
24
25
    public const POLICY_REMOVE = 1;
26
27
    const DEFAULT_SEP = ",";
28
29
    /**
30
     * All of the Model items.
31
     *
32
     * @var array<string, array<string, Assertion>>
33
     */
34
    protected $items = [];
35
36
    /**
37
     * BuildIncrementalRoleLinks provides incremental build the role inheritance relations.
38
     *
39
     * @param RoleManager[] $rmMap
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
40
     * @param integer $op
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
Coding Style introduced by
Expected 7 spaces after parameter type; 1 found
Loading history...
41
     * @param string $sec
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
42
     * @param string $ptype
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
Coding Style introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
43
     * @param string[][] $rules
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
44
     * @return void
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
45
     */
46 39
    public function buildIncrementalRoleLinks(array $rmMap, int $op, string $sec, string $ptype, array $rules): void
47
    {
48 39
        if ($sec == "g") {
49 39
            $this->items[$sec][$ptype]->buildIncrementalRoleLinks($rmMap[$ptype], $op, $rules);
50
        }
51 26
    }
52
53
    /**
54
     * Initializes the roles in RBAC.
55
     *
56
     * @param RoleManager[] $rmMap
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
57
     * @throws CasbinException
0 ignored issues
show
Coding Style introduced by
Tag @throws cannot be grouped with parameter tags in a doc comment
Loading history...
58
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
59 330
    public function buildRoleLinks(array $rmMap): void
60
    {
61 330
        if (!isset($this->items['g'])) {
62 105
            return;
63
        }
64
65 228
        foreach ($this->items['g'] as $ptype => $ast) {
66 228
            $rm = $rmMap[$ptype];
67 228
            $ast->buildRoleLinks($rm);
68
        }
69 152
    }
70
71
    /**
72
     * Prints the policy to log.
73
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
74 330
    public function printPolicy(): void
75
    {
76 330
        Log::logPrint('Policy:');
77
78 330
        foreach (['p', 'g'] as $sec) {
79 330
            if (!isset($this->items[$sec])) {
80 105
                return;
81
            }
82 330
            foreach ($this->items[$sec] as $key => $ast) {
83 330
                Log::logPrint($key, ': ', $ast->value, ': ', $ast->policy);
84
            }
85
        }
86 152
    }
87
88
    /**
89
     * Clears all current policy.
90
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
91 354
    public function clearPolicy(): void
92
    {
93 354
        foreach (['p', 'g'] as $sec) {
94 354
            if (!isset($this->items[$sec])) {
95 120
                return;
96
            }
97
98 354
            foreach ($this->items[$sec] as $key => $ast) {
99 354
                $this->items[$sec][$key]->policy = [];
100 354
                $this->items[$sec][$key]->policyMap = [];
101
            }
102
        }
103 158
    }
104
105
    /**
106
     * Gets all rules in a policy.
107
     *
108
     * @param string $sec
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
109
     * @param string $ptype
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
110
     *
111
     * @return string[][]
112
     */
113 21
    public function getPolicy(string $sec, string $ptype): array
114
    {
115 21
        return $this->items[$sec][$ptype]->policy;
116
    }
117
118
    /**
119
     * Gets rules based on field filters from a policy.
120
     *
121
     * @param string $sec
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
122
     * @param string $ptype
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
123
     * @param int $fieldIndex
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
124
     * @param string ...$fieldValues
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
125
     *
126
     * @return string[][]
127
     */
128 21
    public function getFilteredPolicy(string $sec, string $ptype, int $fieldIndex, string ...$fieldValues): array
129
    {
130 21
        $res = [];
131
132 21
        foreach ($this->items[$sec][$ptype]->policy as $rule) {
133 21
            $matched = true;
134 21
            foreach ($fieldValues as $i => $fieldValue) {
135 21
                if ('' != $fieldValue && $rule[$fieldIndex + intval($i)] != $fieldValue) {
136 21
                    $matched = false;
137
138 21
                    break;
139
                }
140
            }
141
142 21
            if ($matched) {
143 21
                $res[] = $rule;
144
            }
145
        }
146
147 21
        return $res;
148
    }
149
150
    /**
151
     * Determines whether a model has the specified policy rule.
152
     *
153
     * @param string $sec
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
154
     * @param string $ptype
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
155
     * @param string[] $rule
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
156
     *
157
     * @return bool
158
     */
159 111
    public function hasPolicy(string $sec, string $ptype, array $rule): bool
160
    {
161 111
        if (!isset($this->items[$sec][$ptype])) {
162
            return false;
163
        }
164
165 111
        return isset($this->items[$sec][$ptype]->policyMap[implode(self::DEFAULT_SEP, $rule)]);
166
    }
167
168
    /**
169
     * Determines whether a model has any of the specified policies. If one is found we return true.
170
     *
171
     * @param string $sec
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
172
     * @param string $ptype
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
173
     * @param string[][] $rules
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
174
     *
175
     * @return bool
176
     */
177 45
    public function hasPolicies(string $sec, string $ptype, array $rules): bool
178
    {
179 45
        foreach ($rules as $rule) {
180 45
            if ($this->hasPolicy($sec, $ptype, $rule)) {
181 37
                return true;
182
            }
183
        }
184
185 36
        return false;
186
    }
187
188
    /**
189
     * Adds a policy rule to the model.
190
     *
191
     * @param string $sec
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
192
     * @param string $ptype
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
193
     * @param string[] $rule
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
194
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
195 99
    public function addPolicy(string $sec, string $ptype, array $rule): void
196
    {
197 99
        $assertion = &$this->items[$sec][$ptype];
198 99
        $assertion->policy[] = $rule;
199 99
        $assertion->policyMap[implode(self::DEFAULT_SEP, $rule)] = count($this->items[$sec][$ptype]->policy) - 1;
200
201 99
        $hasPriority = isset($assertion->fieldIndexMap[Constants::PRIORITY_INDEX]);
202 99
        if ($sec == 'p' && $hasPriority) {
203 6
            $idxInsert = $rule[$assertion->fieldIndexMap[Constants::PRIORITY_INDEX]];
204 6
            for ($i = count($assertion->policy) - 1; $i > 0; $i--) {
205 6
                $idx = $assertion->policy[$i - 1][$assertion->fieldIndexMap[Constants::PRIORITY_INDEX]];
206 6
                if ($idx > $idxInsert) {
207 6
                    $assertion->policy[$i] = $assertion->policy[$i - 1];
208 6
                    $assertion->policyMap[implode(self::DEFAULT_SEP, $assertion->policy[$i - 1])]++;
209
                } else {
210 6
                    break;
211
                }
212
            }
213 6
            $assertion->policy[$i] = $rule;
214 6
            $assertion->policyMap[implode(self::DEFAULT_SEP, $rule)] = $i;
215
        }
216 66
    }
217
218
    /**
219
     * Adds a policy rules to the model.
220
     *
221
     * @param string $sec
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
222
     * @param string $ptype
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
223
     * @param string[][] $rules
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
224
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
225 36
    public function addPolicies(string $sec, string $ptype, array $rules): void
226
    {
227 36
        $this->addPoliciesWithAffected($sec, $ptype, $rules);
228 24
    }
229
230
    /**
231
     * Adds policy rules to the model, and returns affected rules.
232
     * 
233
     * @param string $sec
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
234
     * @param string $ptype
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
235
     * @param string[][] $rules
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
236
     * 
237
     * @return string[][]
238
     */
239 36
    public function addPoliciesWithAffected(string $sec, string $ptype, array $rules): array
240
    {
241 36
        $affected = [];
242
243 36
        foreach ($rules as $rule) {
244 36
            $hashKey = implode(self::DEFAULT_SEP, $rule);
245 36
            if (isset($this->items[$sec][$ptype]->policyMap[$hashKey])) {
246 12
                continue;
247
            }
248
249 36
            $affected[] = $rule;
250 36
            $this->addPolicy($sec, $ptype, $rule);
251
        }
252
253 36
        return $affected;
254
    }
255
256
    /**
257
     * Updates a policy rule from the model.
258
     *
259
     * @param string $sec
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
260
     * @param string $ptype
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
261
     * @param string[] $oldRule
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
262
     * @param string[] $newRule
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
263
     *
264
     * @return bool
265
     */
266 6
    public function updatePolicy(string $sec, string $ptype, array $oldRule, array $newRule): bool
267
    {
268 6
        $oldPolicy = implode(self::DEFAULT_SEP, $oldRule);
269 6
        if (!isset($this->items[$sec][$ptype]->policyMap[$oldPolicy])) {
270
            return false;
271
        }
272
273 6
        $index = $this->items[$sec][$ptype]->policyMap[$oldPolicy];
274 6
        $this->items[$sec][$ptype]->policy[$index] = $newRule;
275 6
        unset($this->items[$sec][$ptype]->policyMap[$oldPolicy]);
276 6
        $this->items[$sec][$ptype]->policyMap[implode(self::DEFAULT_SEP, $newRule)] = $index;
277
278 6
        return true;
279
    }
280
281
    /**
282
     * UpdatePolicies updates a policy rule from the model.
283
     *
284
     * @param string $sec
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
285
     * @param string $ptype
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
286
     * @param string[][] $oldRules
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
287
     * @param string[][] $newRules
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
288
     * @return boolean
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
289
     */
290 6
    public function updatePolicies(string $sec, string $ptype, array $oldRules, array $newRules): bool
291
    {
292 6
        $modifiedRuleIndex = [];
293
294 6
        $newIndex = 0;
295 6
        foreach ($oldRules as $oldIndex => $oldRule) {
296 6
            $oldPolicy = implode(self::DEFAULT_SEP, $oldRule);
297 6
            $index = $this->items[$sec][$ptype]->policyMap[$oldPolicy] ?? null;
298 6
            if (is_null($index)) {
299
                // rollback
300 6
                foreach ($modifiedRuleIndex as $index => $oldNewIndex) {
301 3
                    $this->items[$sec][$ptype]->policy[$index] = $oldRules[$oldNewIndex[0]];
302 3
                    $oldPolicy = implode(self::DEFAULT_SEP, $oldRules[$oldNewIndex[0]]);
303 3
                    $newPolicy = implode(self::DEFAULT_SEP, $newRules[$oldNewIndex[1]]);
304 3
                    unset($this->items[$sec][$ptype]->policyMap[$newPolicy]);
305 3
                    $this->items[$sec][$ptype]->policyMap[$oldPolicy] = $index;
306
                }
307 6
                return false;
308
            }
309
310 6
            $this->items[$sec][$ptype]->policy[$index] = $newRules[$newIndex];
311 6
            unset($this->items[$sec][$ptype]->policyMap[$oldPolicy]);
312 6
            $this->items[$sec][$ptype]->policyMap[implode(self::DEFAULT_SEP, $newRules[$newIndex])] = $index;
313 6
            $modifiedRuleIndex[$index] = [$oldIndex, $newIndex];
314 6
            $newIndex++;
315
        }
316
317 6
        return true;
318
    }
319
320
    /**
321
     * Removes a policy rule from the model.
322
     *
323
     * @param string $sec
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
324
     * @param string $ptype
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
325
     * @param array $rule
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
326
     *
327
     * @return bool
328
     */
329 51
    public function removePolicy(string $sec, string $ptype, array $rule): bool
330
    {
331 51
        if (!isset($this->items[$sec][$ptype])) {
332
            return false;
333
        }
334
335 51
        $hashKey = implode(self::DEFAULT_SEP, $rule);
336 51
        if (!isset($this->items[$sec][$ptype]->policyMap[$hashKey])) {
337 9
            return false;
338
        }
339
340 51
        $index = $this->items[$sec][$ptype]->policyMap[$hashKey];
341 51
        array_splice($this->items[$sec][$ptype]->policy, $index, 1);
342
343 51
        unset($this->items[$sec][$ptype]->policyMap[$hashKey]);
344
345 51
        $count = count($this->items[$sec][$ptype]->policy);
346 51
        for ($i = $index; $i < $count; $i++) {
347 39
            $this->items[$sec][$ptype]->policyMap[implode(self::DEFAULT_SEP, $this->items[$sec][$ptype]->policy[$i])] = $i;
348
        }
349
350 51
        return true;
351
    }
352
353
    /**
354
     * Removes a policy rules from the model.
355
     *
356
     * @param string $sec
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
357
     * @param string $ptype
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
358
     * @param string[][] $rules
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
359
     *
360
     * @return bool
361
     */
362 21
    public function removePolicies(string $sec, string $ptype, array $rules): bool
363
    {
364 21
        if (!isset($this->items[$sec][$ptype])) {
365
            return false;
366
        }
367
368 21
        foreach ($rules as $rule) {
369 21
            $this->removePolicy($sec, $ptype, $rule);
370
        }
371
372 21
        return true;
373
    }
374
375
    /**
376
     * Removes policy rules based on field filters from the model.
377
     *
378
     * @param string $sec
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
379
     * @param string $ptype
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
380
     * @param int $fieldIndex
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
381
     * @param string ...$fieldValues
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
382
     *
383
     * If more than one rule is removed, return the removed rule array, otherwise return false
384
     * @return string[][]|false
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
385
     */
386 27
    public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex, string ...$fieldValues)
387
    {
388 27
        $tmp = [];
389 27
        $effects = [];
390 27
        $res = false;
391
392 27
        if (!isset($this->items[$sec][$ptype])) {
393 3
            return $res;
394
        }
395
396 27
        $this->items[$sec][$ptype]->policyMap = [];
397
398 27
        foreach ($this->items[$sec][$ptype]->policy as $index => $rule) {
399 27
            $matched = true;
400 27
            foreach ($fieldValues as $i => $fieldValue) {
401 27
                if ('' != $fieldValue && $rule[$fieldIndex + intval($i)] != $fieldValue) {
402 21
                    $matched = false;
403 25
                    break;
404
                }
405
            }
406
407 27
            if ($matched) {
408 27
                $effects[] = $rule;
409
            } else {
410 21
                $tmp[] = $rule;
411 25
                $this->items[$sec][$ptype]->policyMap[implode(self::DEFAULT_SEP, $rule)] = count($tmp) - 1;
412
            }
413
        }
414
415 27
        if (count($tmp) != count($this->items[$sec][$ptype]->policy)) {
416 27
            $this->items[$sec][$ptype]->policy = $tmp;
417 27
            $res = true;
418
        }
419
420 27
        return $res ? $effects : false;
421
    }
422
423
    /**
424
     * Gets all values for a field for all rules in a policy, duplicated values are removed.
425
     *
426
     * @param string $sec
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
427
     * @param string $ptype
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
428
     * @param int $fieldIndex
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
429
     *
430
     * @return string[]
431
     */
432 27
    public function getValuesForFieldInPolicy(string $sec, string $ptype, int $fieldIndex): array
433
    {
434 27
        $values = [];
435
436 27
        if (!isset($this->items[$sec][$ptype])) {
437 6
            return $values;
438
        }
439
440 27
        foreach ($this->items[$sec][$ptype]->policy as $rule) {
441 27
            $values[] = $rule[$fieldIndex];
442
        }
443
444 27
        Util::arrayRemoveDuplicates($values);
445
446 27
        return $values;
447
    }
448
449
    /**
450
     * Gets all values for a field for all rules in a policy of all ptypes, duplicated values are removed.
451
     *
452
     * @param string $sec
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
453
     * @param int $fieldIndex
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
454
     *
455
     * @return string[]
456
     */
457 12
    public function getValuesForFieldInPolicyAllTypes(string $sec, int $fieldIndex): array
458
    {
459 12
        $values = [];
460
461 12
        foreach ($this->items[$sec] as $key => $ptype) {
462 12
            $values = array_merge($values, $this->getValuesForFieldInPolicy($sec, $key, $fieldIndex));
463
        }
464
465 12
        Util::arrayRemoveDuplicates($values);
466
467 12
        return $values;
468
    }
469
470
    /**
471
     * Gets all values for a field for all rules in a policy of all ptypes, duplicated values are removed.
472
     *
473
     * @param string $sec
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
474
     * @param string $field
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
475
     * 
476
     * @return array<string>
477
     * @throws CasbinException
478
     */
479 9
    public function getValuesForFieldInPolicyAllTypesByName(string $sec, string $field): array
480
    {
481 9
        $values = [];
482
483 9
        foreach ($this->items[$sec] as $ptype => $rules) {
484 9
            $index = $this->getFieldIndex($ptype, $field);
485 9
            $v = $this->getValuesForFieldInPolicy($sec, $ptype, $index);
486
487 9
            $values = array_merge($values, $v);
488
        }
489
490 9
        Util::arrayRemoveDuplicates($values);
491
492 9
        return $values;
493
    }
494
495
    /**
496
     * Gets the index for a given ptype and field.
497
     *
498
     * @param string $ptype
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
499
     * @param string $field
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
500
     * 
501
     * @return int $fieldIndex
502
     * @throws CasbinException
503
     */
504 330
    public function getFieldIndex(string $ptype, string $field): int
505
    {
506 330
        $assertion = &$this->items['p'][$ptype];
507 330
        if (isset($assertion->fieldIndexMap[$field])) {
508 39
            return $assertion->fieldIndexMap[$field];
509
        }
510 330
        $pattern = $ptype . '_' . $field;
511 330
        $index = -1;
512 330
        foreach ($assertion->tokens as $i => $token) {
513 330
            if ($token == $pattern) {
514 66
                $index = $i;
515 242
                break;
516
            }
517
        }
518 330
        if ($index == -1) {
519 327
            throw new CasbinException($field . ' index is not set, please use enforcer.SetFieldIndex() to set index');
520
        }
521 66
        $assertion->fieldIndexMap[$field] = $index;
522 66
        return $index;
523
    }
524
525
    /**
526
     * Sets the index for a given ptype and field.
527
     *
528
     * @param string $ptype
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
529
     * @param string $field
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
530
     * @param int $index
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
531
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
532 3
    public function setFieldIndex(string $ptype, string $field, int $index): void
533
    {
534 3
        $assertion = &$this->items['p'][$ptype];
535 3
        $assertion->fieldIndexMap[$field] = $index;
536 2
    }
537
538
    /**
539
     * Determine if the given Model option exists.
540
     *
541
     * @param string $offset
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
542
     *
543
     * @return bool
544
     */
545 363
    public function offsetExists($offset): bool
546
    {
547 363
        return isset($this->items[$offset]);
548
    }
549
550
    /**
551
     * Get a Model option.
552
     *
553
     * @param string $offset
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
554
     *
555
     * @return array<string, Assertion>|null
556
     */
557 363
    public function offsetGet($offset): ?array
558
    {
559 363
        return $this->items[$offset] ?? null;
560
    }
561
562
    /**
563
     * Set a Model option.
564
     *
565
     * @param string $offset
0 ignored issues
show
Coding Style introduced by
Expected 19 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
566
     * @param array<string, Assertion> $value
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
567
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
568 327
    public function offsetSet($offset, $value): void
569
    {
570 327
        $this->items[$offset] = $value;
571 218
    }
572
573
    /**
574
     * Unset a Model option.
575
     *
576
     * @param string $offset
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
577
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
578
    public function offsetUnset($offset): void
579
    {
580
        unset($this->items[$offset]);
581
    }
582
}
583