ContextNodeCollection::toJson()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2.004

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 9
c 2
b 0
f 0
dl 0
loc 16
ccs 9
cts 10
cp 0.9
rs 9.9666
cc 2
nc 1
nop 1
crap 2.004
1
<?php
2
3
namespace MallardDuck\DynamicEcho\Collections;
4
5
use Illuminate\Support\Collection;
6
use MallardDuck\DynamicEcho\ScriptGenerator\Nodes\ContextNode;
7
8
class ContextNodeCollection extends Collection
9
{
10
    /**
11
     * Push one or more items onto the end of the collection.
12
     *
13
     * @param  ContextNode|ContextNode[]  $values [optional]
14
     * @return $this
15
     */
16 1
    public function push(...$values)
17
    {
18 1
        foreach ($values as $value) {
19 1
            $this->items[] = $value;
20
        }
21
22 1
        return $this;
23
    }
24
25
    /**
26
     * Get the collection of items as JSON.
27
     *
28
     * @param  int  $options
29
     * @return string
30
     */
31 1
    public function toJson($options = 0)
32
    {
33 1
        $res = $this->mapWithKeys(static function ($val, $key) {
34 1
            if (isset($val->channelJsVarKey)) {
35
                return [
36 1
                    $val->channelJsVarKey => $val->channelContext
37
                ];
38
            }
39
            return [$key => $val];
40 1
        });
41
42 1
        $contextClass = new \stdClass();
43 1
        $contextClass->active = false;
44 1
        $contextClass->channelStack = $res->jsonSerialize();
45
46 1
        return json_encode($contextClass, $options);
47
    }
48
}
49