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

JsonPacker   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 16
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
eloc 6
c 2
b 0
f 0
dl 0
loc 16
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A pack() 0 3 1
A unpack() 0 9 2
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