Passed
Push — main ( 073c01...b49c3d )
by Dimitri
03:21
created

BaseMiddleware::fill()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 10
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
use Psr\Http\Server\MiddlewareInterface;
16
17
abstract class BaseMiddleware implements MiddlewareInterface
18
{
19
    /**
20
     * Liste des arguments envoyes au middleware
21
     */
22
    protected array $arguments = [];
23
24
    /** 
25
     * Liste des arguments que peut avoir le middleware
26
     */
27
    protected array $fillable = [];
28
29
    /**
30
     * Chemin url de la requette actuelle
31
     */
32
    protected string $path;
33
34
    public function init(array $arguments = []): self
35
    {
36
        $this->path = $arguments['path'] ?: '/';
37
        unset($arguments['path']);
38
39
        $this->arguments = $arguments;
40
41
        foreach ($this->arguments as $argument => $value) {
42
            $method = Text::camel('set_' . $argument);
43
            if (method_exists($this, $method)) {
44
                call_user_func([$this, $method], $value);
45
            }
46
        }
47
48
        return $this;
49
    }
50
51
52
    /**
53
     * @internal
54
     */
55
    public function fill(array $params): self
56
    {
57
        foreach ($this->fillable as $key) {
58
            if (empty($params)) {
59
                break;
60
            }
61
            $this->arguments[$key] = array_shift($params);
62
        }
63
64
        return $this;
65
    }
66
}
67