Passed
Push — main ( 8e44f7...71326a )
by Daniel
03:07
created

ContextNodeCollection::pop()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 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
    public function push(...$values)
18
    {
19
        foreach ($values as $value) {
20
            $this->items[] = $value;
21
        }
22
23
        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
    public function toJson($options = 0)
43
    {
44
        $res = $this->mapWithKeys(static function ($val) {
45
            return [
46
                $val->channelJsVarKey => $val->channelContext
47
            ];
48
        });
49
50
        $contextClass = new \stdClass();
51
        $contextClass->active = false;
52
        $contextClass->channelStack = $res->jsonSerialize();
53
54
        return json_encode($contextClass, $options);
55
    }
56
}
57