FormTrait::buildRequestForm()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 2
dl 0
loc 18
ccs 0
cts 8
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Nilnice\Payment\Alipay\Traits;
4
5
trait FormTrait
6
{
7
    /**
8
     * Build request form.
9
     *
10
     * @param string $gateway
11
     * @param array  $payload
12
     *
13
     * @return string
14
     */
15
    public function buildRequestForm(string $gateway, array $payload) : string
16
    {
17
        $format
18
            = <<<HTML
19
<form id="alipaysubmit" name="alipaysubmit" action="%s" method="post">
20
    %s
21
    <input type="submit" value="ok" style="display: none">
22
    <script>document.forms['alipaysubmit'].submit()</script>
23
</form>
24
HTML;
25
        $input = '';
26
        foreach ($payload as $key => $val) {
27
            $val = str_replace("'", '&apos;', $val);
28
            $input .= "<input type='hidden' name='{$key}' value='{$val}'>";
29
        }
30
        $html = sprintf($format, $gateway, $input);
31
32
        return $html;
33
    }
34
}
35