Completed
Push — master ( d0aee5...e823c6 )
by Lee
04:10
created

Model::offsetGet()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
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\Config\Config;
9
use Casbin\Config\ConfigContract;
10
use Casbin\Log\Log;
11
use Casbin\Util\Util;
12
13
/**
14
 * Class Model.
15
 *
16
 * @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...
17
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package 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...
18
class Model implements ArrayAccess
19
{
20
    use Policy;
21
22
    /**
23
     * All of the Model items.
24
     *
25
     * @var array
26
     */
27
    protected $items = [];
28
29
    private $model = [];
0 ignored issues
show
introduced by
The private property $model is not used, and could be removed.
Loading history...
Coding Style introduced by
Private member variable "model" must be prefixed with an underscore
Loading history...
30
31
    protected $sectionNameMap = [
32
        'r' => 'request_definition',
33
        'p' => 'policy_definition',
34
        'g' => 'role_definition',
35
        'e' => 'policy_effect',
36
        'm' => 'matchers',
37
    ];
38
39 183
    public function __construct()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
40
    {
41 183
    }
42
43
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
44
     * @param ConfigContract $cfg
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
45
     * @param string         $sec
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
46
     * @param string         $key
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
47
     *
48
     * @return bool
49
     */
50 174
    private function loadAssertion(ConfigContract $cfg, string $sec, string $key): bool
0 ignored issues
show
Coding Style introduced by
Private method name "Model::loadAssertion" must be prefixed with an underscore
Loading history...
51
    {
52 174
        $value = $cfg->getString($this->sectionNameMap[$sec].'::'.$key);
53
54 174
        return $this->addDef($sec, $key, $value);
55
    }
56
57
    /**
58
     * adds an assertion to the model.
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
59
     *
60
     * @param string $sec
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
61
     * @param string $key
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
62
     * @param string $value
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
63
     *
64
     * @return bool
65
     */
66 183
    public function addDef(string $sec, string $key, string $value): bool
67
    {
68 183
        if ('' == $value) {
69 174
            return false;
70
        }
71
72 183
        $ast = new Assertion();
73 183
        $ast->key = $key;
74 183
        $ast->value = $value;
75
76 183
        if ('r' == $sec || 'p' == $sec) {
77 183
            $ast->tokens = explode(', ', $ast->value);
78 183
            foreach ($ast->tokens as $i => $token) {
79 183
                $ast->tokens[$i] = $key.'_'.$token;
80
            }
81
        } else {
82 183
            $ast->value = Util::removeComments(Util::escapeAssertion($ast->value));
83
        }
84
85 183
        $this->items[$sec][$key] = $ast;
86
87 183
        return true;
88
    }
89
90
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
91
     * @param int $i
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
92
     *
93
     * @return string
94
     */
95 174
    private function getKeySuffix(int $i): string
0 ignored issues
show
Coding Style introduced by
Private method name "Model::getKeySuffix" must be prefixed with an underscore
Loading history...
96
    {
97 174
        if (1 == $i) {
98 174
            return '';
99
        }
100
101 174
        return (string) $i;
102
    }
103
104
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
105
     * @param ConfigContract $cfg
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
106
     * @param string         $sec
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
107
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
108 174
    private function loadSection(ConfigContract $cfg, string $sec): void
0 ignored issues
show
Coding Style introduced by
Private method name "Model::loadSection" must be prefixed with an underscore
Loading history...
109
    {
110 174
        $i = 1;
111 174
        for (;;) {
112 174
            if (!$this->loadAssertion($cfg, $sec, $sec.$this->getKeySuffix($i))) {
113 174
                break;
114
            } else {
115 174
                ++$i;
116
            }
117
        }
118 174
    }
119
120
    /**
121
     * creates an empty model.
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
122
     *
123
     * @return Model
124
     */
125 159
    public static function newModel(): self
126
    {
127 159
        return new self();
128
    }
129
130
    /**
131
     * creates a model from a .CONF file.
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
132
     *
133
     * @param string $path
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
134
     *
135
     * @return Model
136
     */
137 147
    public static function newModelFromFile(string $path): self
138
    {
139 147
        $m = self::newModel();
140
141 147
        $m->loadModel($path);
142
143 147
        return $m;
144
    }
145
146
    /**
147
     * creates a model from a string which contains model text.
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
148
     *
149
     * @param string $text
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
150
     *
151
     * @return Model
152
     */
153 3
    public static function newModelFromString(string $text): self
154
    {
155 3
        $m = self::newModel();
156
157 3
        $m->loadModelFromText($text);
158
159 3
        return $m;
160
    }
161
162
    /**
163
     * loads the model from model CONF file.
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
164
     *
165
     * @param string $path
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
166
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
167 168
    public function loadModel(string $path): void
168
    {
169 168
        $cfg = Config::newConfig($path);
170
171 168
        $this->loadSection($cfg, 'r');
172 168
        $this->loadSection($cfg, 'p');
173 168
        $this->loadSection($cfg, 'e');
174 168
        $this->loadSection($cfg, 'm');
175
176 168
        $this->loadSection($cfg, 'g');
177 168
    }
178
179
    /**
180
     * loads the model from the text.
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
181
     *
182
     * @param string $text
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
183
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
184 6
    public function loadModelFromText(string $text): void
185
    {
186 6
        $cfg = Config::newConfigFromText($text);
187
188 6
        $this->loadSection($cfg, 'r');
189 6
        $this->loadSection($cfg, 'p');
190 6
        $this->loadSection($cfg, 'e');
191 6
        $this->loadSection($cfg, 'm');
192
193 6
        $this->loadSection($cfg, 'g');
194 6
    }
195
196
    /**
197
     * prints the model to the log.
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
198
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
199 159
    public function printModel(): void
200
    {
201 159
        Log::logPrint('Model:');
0 ignored issues
show
Bug introduced by
'Model:' of type string is incompatible with the type Casbin\Log\mix expected by parameter $v of Casbin\Log\Log::logPrint(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

201
        Log::logPrint(/** @scrutinizer ignore-type */ 'Model:');
Loading history...
202 159
        foreach ($this->items as $k => $v) {
203 159
            foreach ($v as $i => $j) {
204 159
                Log::logPrintf('%s.%s: %s', $k, $i, $j->value);
205
            }
206
        }
207 159
    }
208
209
    /**
210
     * loads an initial function map.
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
211
     *
212
     * @return FunctionMap
213
     */
214 162
    public static function loadFunctionMap(): FunctionMap
215
    {
216 162
        return FunctionMap::loadFunctionMap();
217
    }
218
219
    /**
220
     * Determine if the given Model option exists.
221
     *
222
     * @param mixed $offset
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
223
     *
224
     * @return bool
225
     */
226 165
    public function offsetExists($offset)
227
    {
228 165
        return isset($this->items[$offset]);
229
    }
230
231
    /**
232
     * Get a Model option.
233
     *
234
     * @param mixed $offset
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
235
     *
236
     * @return mixed
237
     */
238 165
    public function offsetGet($offset)
239
    {
240 165
        return isset($this->items[$offset]) ? $this->items[$offset] : null;
241
    }
242
243
    /**
244
     * Set a Model option.
245
     *
246
     * @param mixed $offset
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
247
     * @param mixed $value
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
248
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
249 150
    public function offsetSet($offset, $value)
250
    {
251 150
        if (is_null($offset)) {
252
            $this->items[] = $value;
253
        } else {
254 150
            $this->items[$offset] = $value;
255
        }
256 150
    }
257
258
    /**
259
     * Unset a Model option.
260
     *
261
     * @param mixed $offset
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
262
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
263
    public function offsetUnset($offset)
264
    {
265
        unset($this->items[$offset]);
266
    }
267
}
268