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

BaseMiddleware   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 22
c 3
b 1
f 0
dl 0
loc 58
ccs 0
cts 12
cp 0
rs 10
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 21 6
A fill() 0 10 3
A __get() 0 3 1
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