Issues (13)

src/PermissionResolver.php (1 issue)

1
<?php
2
3
namespace Mrluke\Privileges;
4
5
/**
6
 * Class PermissionResolver
7
 * @package Mrluke\Privileges
8
 *
9
 * @author  Hubert Smusz <[email protected]>
10
 * @version 1.0.0
11
 */
12
class PermissionResolver
13
{
14
15
    /**
16
     * @param          $subject
17
     * @param string   $scope
18
     * @param int|null $level
19
     *
20
     * @return array
21
     */
22
    public static function getScopeRestrictions($subject, string $scope, int $level = null): array
23
    {
24
        $scopes = $subject->roles->flatMap(function ($role) use ($scope, $level) {
25
            if ($level) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $level of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
26
                $permissions = $role->permissions->where('level', $level);
27
            } else {
28
                $permissions = $role->permissions;
29
            }
30
31
            return preg_grep("/^${scope}:/", array_column($permissions->toArray(), 'scope'));
32
        })->toArray();
33
34
        return array_map(function ($val) use ($scope) {
35
            return str_replace($scope . ':', '', $val);
36
        }, $scopes);
37
    }
38
}
39