Issues (56)

Resolver/AbstractScopeResolver.php (1 issue)

1
<?php
2
3
4
namespace oliverde8\ComfyBundle\Resolver;
5
6
7
use oliverde8\AssociativeArraySimplified\AssociativeArray;
8
9
abstract class AbstractScopeResolver implements ScopeResolverInterface
10
{
11
    /** @var array */
12
    private ?array $scopes = null;
13
14
    /**
15
     * @inheritDoc
16
     */
17
    public function validateScope(string $scope = null): bool
18
    {
19
        $this->initScopes();
20
21
        $scopes = $this->getScopes();
22
        $scope = $this->getScope($scope);
23
        return isset($scopes[$scope]);
24
    }
25
26
    /**
27
     * @inheritDoc
28
     */
29
    public function getScope(string $scope = null): string
30
    {
31
        $this->initScopes();
32
33
        if (is_null($scope)) {
34
            $scope = $this->getCurrentScope();
35
        }
36
37
        return $scope;
38
    }
39
40
    /**
41
     * @inheritDoc
42
     */
43
    public function inherits(string $scope = null): ?string
44
    {
45
        $this->initScopes();
46
        $scope = $this->getScope($scope);
47
48
        $parts = explode('/', $scope);
49
        if (count($parts) <= 1) {
50
            return null;
51
        }
52
53
        array_pop($parts);
54
        return implode('/', $parts);
55
    }
56
57
    /**
58
     * @inheritDoc
59
     */
60
    public function getScopeTree() : array
61
    {
62
        $this->initScopes();
63
64
        $data = [];
65
        foreach ($this->scopes as $scopeKey => $scopeName) {
66
            $parentScopeKey = $this->inherits($scopeKey);
67
            if ($parentScopeKey && !AssociativeArray::checkKeyExist($data, $parentScopeKey . "/~name")) {
68
                AssociativeArray::setFromKey($data, $parentScopeKey . "/~name", $parentScopeKey);
69
            }
70
71
            AssociativeArray::setFromKey($data, $scopeKey . "/~name", $scopeName);
72
        }
73
74
        return $data;
75
    }
76
77
    /**
78
     * @return array
79
     */
80
    protected function getScopes(): array
81
    {
82
        if (is_null($this->scopes)) {
83
            $this->scopes = $this->initScopes();
84
        }
85
86
        return $this->scopes;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->scopes could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
87
    }
88
89
    abstract protected function initScopes(): array;
90
}
91