Passed
Push — main ( 173990...55ad70 )
by Daniel
02:53
created

ContextNodeCollection   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 92.86%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 13
c 1
b 0
f 0
dl 0
loc 41
ccs 13
cts 14
cp 0.9286
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A push() 0 7 2
A toJson() 0 17 2
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
    /**
12
     * Push one or more items onto the end of the collection.
13
     *
14
     * @param  ContextNode|ContextNode[]  $values [optional]
15
     * @return $this
16
     */
17 1
    public function push(...$values)
18
    {
19 1
        foreach ($values as $value) {
20 1
            $this->items[] = $value;
21
        }
22
23 1
        return $this;
24
    }
25
26
    /**
27
     * Get the collection of items as JSON.
28
     *
29
     * @param  int  $options
30
     * @return string
31
     */
32 1
    public function toJson($options = 0)
33
    {
34
35 1
        $res = $this->mapWithKeys(static function ($val, $key) {
36 1
            if (isset($val->channelJsVarKey)) {
37
                return [
38 1
                    $val->channelJsVarKey => $val->channelContext
39
                ];
40
            }
41
            return [$key => $val];
42 1
        });
43
44 1
        $contextClass = new \stdClass();
45 1
        $contextClass->active = false;
46 1
        $contextClass->channelStack = $res->jsonSerialize();
47
48 1
        return json_encode($contextClass, $options);
49
    }
50
}
51