JsonFormatter::decode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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