ChannelContextNode::__toString()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 12
ccs 9
cts 9
cp 1
rs 10
cc 3
nc 3
nop 0
crap 3
1
<?php
2
3
namespace MallardDuck\DynamicEcho\ScriptGenerator\Nodes;
4
5
class ChannelContextNode extends ContextNode
6
{
7
    public string $channelJsVarKey;
8
    public array $channelContext;
9
10 3
    public function __construct(string $channelJsVarKey, array $channelContext)
11
    {
12 3
        $this->channelJsVarKey = $channelJsVarKey;
13 3
        $this->channelContext = $channelContext;
14 3
    }
15
16 1
    public function __toString()
17
    {
18 1
        $res = "";
19 1
        $count = count($this->channelContext);
20 1
        foreach ($this->channelContext as $key => $value) {
21 1
            --$count;
22 1
            $res .= sprintf("%s: %s", $key, $value);
23 1
            if ($count >= 1) {
24 1
                $res .= ",\r\n";
25
            }
26
        }
27 1
        return sprintf("%s: {%s}", $this->channelJsVarKey, $res);
28
    }
29
30
    public function jsonSerialize()
31
    {
32
        return json_encode([$this->channelJsVarKey => $this->channelContext]);
33
    }
34
}
35