JsonEncoding::encode()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Stratadox\Json;
5
6
use function json_encode;
7
use const JSON_ERROR_NONE;
8
use function json_last_error;
9
use function json_last_error_msg;
10
use Throwable;
11
12
trait JsonEncoding
13
{
14
    /** @throws InvalidJson */
15
    private function encode($decodedJsonData): string
16
    {
17
        try {
18
            $encoded = json_encode($decodedJsonData);
19
        } catch (Throwable $exception) {
20
            throw InputDataCannotBeEncoded::encountered($exception);
21
        }
22
        if (json_last_error() !== JSON_ERROR_NONE) {
23
            throw InputDataCannotBeEncoded::found(json_last_error_msg());
24
        }
25
        return $encoded;
26
    }
27
}
28
29