CsrfTrait::getCsrfField()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 1
cts 1
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * This file is part of the Shieldon package.
4
 *
5
 * (c) Terry L. <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * php version 7.1.0
11
 *
12
 * @category  Web-security
13
 * @package   Shieldon
14
 * @author    Terry Lin <[email protected]>
15
 * @copyright 2019 terrylinooo
16
 * @license   https://github.com/terrylinooo/shieldon/blob/2.x/LICENSE MIT
17
 * @link      https://github.com/terrylinooo/shieldon
18
 * @see       https://shieldon.io
19
 */
20
21
declare(strict_types=1);
22
23
namespace Shieldon\Firewall\Panel;
24
25
/*
26
 * Tradit for handling CSRF function.
27
 */
28
trait CsrfTrait
29
{
30
    /**
31
     *   Public methods       | Desctiotion
32
     *  ----------------------|---------------------------------------------
33
     *   csrf                 | Receive the CSRF name and token from the App.
34
     *   setCsrfField         | Set CSRF input fields.
35
     *   fieldCsrf            | Output HTML input element with CSRF token.
36
     *  ----------------------|---------------------------------------------
37
     */
38
39
    /**
40
     * See $this->csrf()
41
     *
42
     * @var array
43
     */
44
    protected $csrfField = [];
45
46
    /**
47
     * Most popular PHP framework has a built-in CSRF protection such as Laravel.
48
     * We need to pass the CSRF token for our form actions.
49
     *
50
     * @param array ...$csrfparams The arguments.
51
     *
52
     * @return void
53
     */
54 2
    public function csrf(...$csrfparams): void
55
    {
56 2
        foreach ($csrfparams as $value) {
57 2
            foreach ($value as $k => $v) {
58 2
                $this->csrfField[] = [
59 2
                    'name'  => $k,
60 2
                    'value' => $v,
61 2
                ];
62
            }
63
        }
64
    }
65
66
    /**
67
     * Set CSRF input fields.
68
     *
69
     * @param array $csrfParams The arguments.
70
     *
71
     * @return void
72
     */
73 5
    public function setCsrfField(array $csrfParams): void
74
    {
75 5
        $this->csrfField = $csrfParams;
76
    }
77
78
    /**
79
     * Output HTML input element with CSRF token.
80
     *
81
     * @return string
82
     */
83 40
    public function fieldCsrf(): string
84
    {
85 40
        $string = '';
86 40
        if (!empty($this->csrfField)) {
87 1
            foreach ($this->csrfField as $value) {
88 1
                // phpcs:ignore
89
                $string .= '<input type="hidden" name="' . $value['name'] .'" value="' . $value['value'] . '" id="csrf-field">';
90
            }
91 40
        }
92
        return $string;
93
    }
94
95
    /**
96
     * Get CSRF input fields.
97
     *
98
     * @return array
99 5
     */
100
    protected function getCsrfField(): array
101 5
    {
102
        return $this->csrfField;
103
    }
104
}
105