Completed
Push — master ( a1b189...a1cb6a )
by Kristof
04:46
created

JsonDocument::getId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * @file
4
 */
5
6
namespace CultuurNet\UDB3\ReadModel;
7
8
use Broadway\ReadModel\ReadModelInterface;
9
10
class JsonDocument implements ReadModelInterface
11
{
12
    protected $id;
13
    protected $body;
14
15
    public function __construct($id, $rawBody = '{}')
16
    {
17
        $this->id = $id;
18
        $this->body = $rawBody;
19
    }
20
21
    /**
22
     * @return string
23
     */
24
    public function getId()
25
    {
26
        return $this->id;
27
    }
28
29
    public function getBody()
30
    {
31
        return json_decode($this->body);
32
    }
33
34
    public function getRawBody()
35
    {
36
        return $this->body;
37
    }
38
39
    /**
40
     * @param \stdClass $body
41
     * @return static
42
     */
43
    public function withBody($body)
44
    {
45
        return new self($this->id, json_encode($body));
46
    }
47
48
    /**
49
     * @param callable $fn
50
     * @return
51
     */
52
    public function apply(callable $fn)
53
    {
54
        $body = $fn($this->getBody());
55
        return $this->withBody($body);
56
    }
57
}
58