Completed
Push — master ( 860482...640ced )
by Maksim (Ellrion)
7s
created

Loop::resetLoopStack()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Advmaker\BladeLoop;
4
5
class Loop
6
{
7
    /**
8
     * The array that is being iterated
9
     *
10
     * @var array
11
     */
12
    protected $items = [];
13
14
    /**
15
     * The data for the current $loop item
16
     *
17
     * @var array
18
     */
19
    protected $data;
20
21
    /**
22
     * The parent loop, if any
23
     *
24
     * @var Loop
25
     */
26
    protected $parentLoop;
27
28
    /**
29
     * Sets the parent loop
30
     *
31
     * @param Loop $parentLoop
32
     * {@inheritdocs}
33
     */
34
    public function setParentLoop(Loop $parentLoop)
35
    {
36
        $this->parentLoop = $parentLoop;
37
        $this->data['parent'] = $parentLoop;
38
    }
39
40
    /**
41
     * Instantiates the class
42
     *
43
     * @param array $items The array that's being iterated
44
     */
45 12
    public function __construct($items)
46
    {
47 12
        $this->setItems($items);
48 12
    }
49
50
    /**
51
     * Sets the array to monitor
52
     *
53
     * @param array $items The array that's being iterated
54
     */
55 12
    public function setItems($items)
56
    {
57 12
        $this->items = $items;
58 12
        $total = count($items);
59 12
        $this->data = [
60 12
            'index1'    => 1,
61 12
            'index'     => 0,
62 12
            'revindex1' => $total,
63 12
            'revindex'  => $total - 1,
64 12
            'first'     => true,
65 12
            'last'      => false,
66 12
            'odd'       => false,
67 12
            'even'      => true,
68
            'length'    => $total
69 12
        ];
70 12
    }
71
72 12
    public function getItems()
73
    {
74 12
        return $this->items;
75
    }
76
77
    /**
78
     * Magic method to access the loop data properties
79
     *
80
     * @param $key
81
     *
82
     * @return mixed
83
     */
84 12
    public function __get($key)
85
    {
86 12
        return $this->data[$key];
87
    }
88
89
    /**
90
     * To be called first in a loop before anything else
91
     */
92 12
    public function before()
93
    {
94 12
        $this->data['even'] = $this->data['index'] % 2 === 0;
95 12
        $this->data['odd'] = ! $this->data['even'];
96
97 12
        $this->data['first'] = $this->data['index'] === 0;
98 12
        $this->data['last'] = $this->data['revindex'] === 0;
99 12
    }
100
101
    /**
102
     * To be called last in a loop after everything else
103
     */
104 12
    public function after()
105
    {
106 12
        $this->data['index']++;
107 12
        $this->data['index1']++;
108 12
        $this->data['revindex']--;
109 12
        $this->data['revindex1']--;
110 12
    }
111
}
112