Completed
Push — master ( 5c1921...6ec8b5 )
by Maksim (Ellrion)
8s
created

Loop::getItems()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Advmaker\BladeLoop;
4
5
class Loop
6
{
7
8
    /**
9
     * The array that is being iterated
10
     *
11
     * @var array
12
     */
13
    protected $items = [];
14
15
    /**
16
     * The data for the current $loop item
17
     *
18
     * @var array
19
     */
20
    protected $data;
21
22
    /**
23
     * The parent loop, if any
24
     *
25
     * @var Loop
26
     */
27
    protected $parentLoop;
28
29
    protected $loopFactory;
30
31
    /**
32
     * Sets the parent loop
33
     *
34
     * @param Loop $parentLoop
35
     * {@inheritdocs}
36
     */
37
    public function setParentLoop(Loop $parentLoop)
38
    {
39
        $this->parentLoop = $parentLoop;
40
        $this->data['parent'] = $parentLoop;
41
    }
42
43
    /**
44
     * Returns the full loop stack of the LoopFactory
45
     *
46
     * @return array
47
     */
48
    public function getLoopStack()
49
    {
50
        return $this->loopFactory->getStack();
51
    }
52
53
    /**
54
     * Resets the loop stack of the LoopFactory
55
     */
56
    public function resetLoopStack()
57
    {
58
        $this->loopFactory->reset();
59
    }
60
61
    /**
62
     * Instantiates the class
63
     *
64
     * @param array $items The array that's being iterated
65
     */
66
    public function __construct(LoopFactory $loopFactory, $items)
67
    {
68
        $this->loopFactory = $loopFactory;
69
        $this->setItems($items);
70
    }
71
72
    /**
73
     * Sets the array to monitor
74
     *
75
     * @param array $items The array that's being iterated
76
     */
77
    public function setItems($items)
78
    {
79
        if (isset($data)) {
0 ignored issues
show
Bug introduced by
The variable $data seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
80
            return;
81
        }
82
        $this->items = $items;
83
        $total = count($items);
84
        $this->data = [
85
            'index1'    => 1,
86
            'index'     => 0,
87
            'revindex1' => $total,
88
            'revindex'  => $total - 1,
89
            'first'     => true,
90
            'last'      => false,
91
            'odd'       => false,
92
            'even'      => true,
93
            'length'    => $total
94
        ];
95
    }
96
97
    public function getItems()
98
    {
99
        return $this->items;
100
    }
101
102
    /**
103
     * Magic method to access the loop data properties
104
     *
105
     * @param $key
106
     *
107
     * @return mixed
108
     */
109
    public function __get($key)
110
    {
111
        return $this->data[$key];
112
    }
113
114
    /**
115
     * To be called first in a loop before anything else
116
     */
117
    public function before()
118
    {
119
        if ($this->data['index'] % 2 == 0) {
120
            $this->data['odd'] = false;
121
            $this->data['even'] = true;
122
        } else {
123
            $this->data['odd'] = true;
124
            $this->data['even'] = false;
125
        }
126
        if ($this->data['index'] == 0) {
127
            $this->data['first'] = true;
128
        } else {
129
            $this->data['first'] = false;
130
        }
131
        if ($this->data['revindex'] == 0) {
132
            $this->data['last'] = true;
133
        } else {
134
            $this->data['last'] = false;
135
        }
136
    }
137
138
    /**
139
     * To be called last in a loop after everything else
140
     */
141
    public function after()
142
    {
143
        $this->data['index']++;
144
        $this->data['index1']++;
145
        $this->data['revindex']--;
146
        $this->data['revindex1']--;
147
    }
148
}
149