Completed
Push — master ( 183dcf...c5dab6 )
by Jodie
02:15
created

JsonHelper::encode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Rexlabs\Smokescreen\Helpers;
4
5
use Rexlabs\Smokescreen\Exception\JsonEncodeException;
6
7
class JsonHelper
8
{
9
    /**
10
     * Encodes data as a JSON string representation.
11
     * @param mixed $data
12
     * @param int   $options
13
     *
14
     * @return string
15
     * @throws \Rexlabs\Smokescreen\Exception\JsonEncodeException
16
     */
17 2
    public static function encode($data, $options = 0): string
18
    {
19 2
        $json = json_encode($data, $options);
20 2
        if (JSON_ERROR_NONE !== json_last_error()) {
21 1
            throw new JsonEncodeException(json_last_error_msg());
22
        }
23
24 1
        return $json;
25
    }
26
}