Passed
Pull Request — master (#754)
by Songda
01:52
created

QueryPacker::unpack()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 15
rs 10
cc 4
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Packer;
6
7
use Yansongda\Pay\Contract\PackerInterface;
8
use Yansongda\Supports\Str;
9
10
class QueryPacker implements PackerInterface
11
{
12
    public function pack(array $payload): string
13
    {
14
        return http_build_query($payload, '', '&');
15
    }
16
17
    public function unpack(string $payload): ?array
18
    {
19
        if (empty($payload) || !Str::contains($payload, '=')) {
20
            return [];
21
        }
22
23
        $result = [];
24
25
        foreach (explode('&', $payload) as $item) {
26
            $pos = strpos($item, '=');
27
28
            $result[substr($item, 0, $pos)] = substr($item, $pos + 1);
29
        }
30
31
        return $result;
32
    }
33
}
34