Passed
Branch main (64d39c)
by Dimitri
03:50
created

BaseMiddleware::__get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 1
cp 0
crap 2
rs 10
1
<?php
2
3
/**
4
 * This file is part of Blitz PHP framework.
5
 *
6
 * (c) 2022 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace BlitzPHP\Middlewares;
13
14
use BlitzPHP\Utilities\String\Text;
15
16
abstract class BaseMiddleware
17
{
18
    /**
19
     * Liste des arguments envoyes au middleware
20
     */
21
    protected array $arguments = [];
22
23
    /**
24
     * Liste des arguments que peut avoir le middleware
25
     */
26
    protected array $fillable = [];
27
28
    /**
29
     * Chemin url de la requette actuelle
30
     */
31
    protected string $path;
32
33
    public function init(array $arguments = []): static
34
    {
35
        $this->path = $arguments['path'] ?: '/';
36
        unset($arguments['path']);
37
38
        $this->arguments = array_merge($this->arguments, $arguments);
39
40
        foreach ($this->arguments as $argument => $value) {
41
            if (! is_string($argument)) {
42
                continue;
43
            }
44
45
            $method = Text::camel('set_' . $argument);
46
            if (method_exists($this, $method)) {
47
                call_user_func([$this, $method], $value);
48
            } elseif (property_exists($this, $argument)) {
49
                $this->{$argument} = $value;
50
            }
51
        }
52
53
        return $this;
54
    }
55
56
    public function __get($name)
57
    {
58
        return $this->arguments[$name] ?? null;
59
    }
60
61
    /**
62
     * @internal
63
     */
64
    final public function fill(array $params): static
65
    {
66
        foreach ($this->fillable as $key) {
67
            if (empty($params)) {
68
                break;
69
            }
70
            $this->arguments[$key] = array_shift($params);
71
        }
72
73
        return $this;
74
    }
75
}
76