Passed
Push — main ( 71326a...1c3482 )
by Daniel
02:25
created

ContextNodeCollection   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 81.25%

Importance

Changes 0
Metric Value
wmc 5
eloc 14
c 0
b 0
f 0
dl 0
loc 51
ccs 13
cts 16
cp 0.8125
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A pop() 0 3 1
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 and remove the last item from the collection.
28
     *
29
     * @return ContextNode
30
     */
31
    public function pop()
32
    {
33
        return array_pop($this->items);
34
    }
35
36
    /**
37
     * Get the collection of items as JSON.
38
     *
39
     * @param  int  $options
40
     * @return string
41
     */
42 1
    public function toJson($options = 0)
43
    {
44
45 1
        $res = $this->mapWithKeys(static function ($val, $key) {
46 1
            if (isset($val->channelJsVarKey)) {
47
                return [
48 1
                    $val->channelJsVarKey => $val->channelContext
49
                ];
50
            }
51
            return [$key => $val];
52 1
        });
53
54 1
        $contextClass = new \stdClass();
55 1
        $contextClass->active = false;
56 1
        $contextClass->channelStack = $res->jsonSerialize();
57
58 1
        return json_encode($contextClass, $options);
59
    }
60
}
61