JsonFormatter::encode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1.0019

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 10
ccs 7
cts 8
cp 0.875
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1.0019
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PascalDeVink\CloudEvents\Format;
6
7
use PascalDeVink\CloudEvents\V03\CloudEvent;
8
9
class JsonFormatter
10
{
11 6
    public function encode(CloudEvent $cloudEvent) : string
12
    {
13 6
        return json_encode(
14
            array_filter(
15 6
                $cloudEvent->toArray(),
16 2
                function ($value) {
17 6
                    return null !== $value;
18 6
                }
19
            ),
20 6
            JSON_THROW_ON_ERROR
21
        );
22
    }
23
24 3
    public function decode(string $json) : CloudEvent
25
    {
26 3
        $eventData = json_decode($json, true);
27
28 3
        return CloudEvent::fromArray($eventData);
29
    }
30
}
31