Completed
Push — master ( f5bffe...9f8e49 )
by Joachim
04:21
created

functions.php ➔ decodeJson()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 9
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 9.6666
c 0
b 0
f 0
1
<?php
2
namespace Loevgaard\Consignor\ShipmentServer;
3
4
use Loevgaard\Consignor\ShipmentServer\Exception\EncodeJsonException;
5
use Loevgaard\Consignor\ShipmentServer\Exception\InvalidJsonException;
6
7
/**
8
 * @param string $json
9
 * @return array
10
 * @throws InvalidJsonException
11
 */
12
function decodeJson(string $json) : array
13
{
14 1
    $data = \json_decode($json, true);
15 1
    if (JSON_ERROR_NONE !== json_last_error()) {
16
        throw new InvalidJsonException('json_decode error: ' . json_last_error_msg());
17
    }
18
19 1
    return (array)$data;
20
}
21
22
/**
23
 * @param array $data
24
 * @return string
25
 * @throws EncodeJsonException
26
 */
27
function encodeJson(array $data) : string
28
{
29
    $json = \json_encode($data);
30
    if (JSON_ERROR_NONE !== json_last_error()) {
31
        throw new EncodeJsonException('json_encode error: ' . json_last_error_msg());
32
    }
33
34
    return $json;
35
}
36