Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\View\Concerns;
4
 
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\LazyCollection;
7
 
8
trait ManagesLoops
9
{
10
    /**
11
     * The stack of in-progress loops.
12
     *
13
     * @var array
14
     */
15
    protected $loopsStack = [];
16
 
17
    /**
18
     * Add new loop to the stack.
19
     *
20
     * @param  \Countable|array  $data
21
     * @return void
22
     */
23
    public function addLoop($data)
24
    {
25
        $length = is_countable($data) && ! $data instanceof LazyCollection
26
                            ? count($data)
27
                            : null;
28
 
29
        $parent = Arr::last($this->loopsStack);
30
 
31
        $this->loopsStack[] = [
32
            'iteration' => 0,
33
            'index' => 0,
34
            'remaining' => $length ?? null,
35
            'count' => $length,
36
            'first' => true,
37
            'last' => isset($length) ? $length == 1 : null,
38
            'odd' => false,
39
            'even' => true,
40
            'depth' => count($this->loopsStack) + 1,
41
            'parent' => $parent ? (object) $parent : null,
42
        ];
43
    }
44
 
45
    /**
46
     * Increment the top loop's indices.
47
     *
48
     * @return void
49
     */
50
    public function incrementLoopIndices()
51
    {
52
        $loop = $this->loopsStack[$index = count($this->loopsStack) - 1];
53
 
54
        $this->loopsStack[$index] = array_merge($this->loopsStack[$index], [
55
            'iteration' => $loop['iteration'] + 1,
56
            'index' => $loop['iteration'],
57
            'first' => $loop['iteration'] == 0,
58
            'odd' => ! $loop['odd'],
59
            'even' => ! $loop['even'],
60
            'remaining' => isset($loop['count']) ? $loop['remaining'] - 1 : null,
61
            'last' => isset($loop['count']) ? $loop['iteration'] == $loop['count'] - 1 : null,
62
        ]);
63
    }
64
 
65
    /**
66
     * Pop a loop from the top of the loop stack.
67
     *
68
     * @return void
69
     */
70
    public function popLoop()
71
    {
72
        array_pop($this->loopsStack);
73
    }
74
 
75
    /**
76
     * Get an instance of the last loop in the stack.
77
     *
78
     * @return \stdClass|null
79
     */
80
    public function getLastLoop()
81
    {
82
        if ($last = Arr::last($this->loopsStack)) {
83
            return (object) $last;
84
        }
85
    }
86
 
87
    /**
88
     * Get the entire loop stack.
89
     *
90
     * @return array
91
     */
92
    public function getLoopStack()
93
    {
94
        return $this->loopsStack;
95
    }
96
}