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

JsonPacker::unpack()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Packer;
6
7
use Yansongda\Pay\Contract\PackerInterface;
8
9
class JsonPacker implements PackerInterface
10
{
11
    public function pack(array $payload): string
12
    {
13
        return json_encode($payload, JSON_UNESCAPED_UNICODE);
14
    }
15
16
    public function unpack(string $payload): ?array
17
    {
18
        $data = json_decode($payload, true);
19
20
        if (JSON_ERROR_NONE === json_last_error()) {
21
            return $data;
22
        }
23
24
        return null;
25
    }
26
}
27