1 | 1 |
deleted file mode 100644 |
... | ... |
@@ -1,94 +0,0 @@ |
1 |
-<?php |
|
2 |
- |
|
3 |
-namespace Illuminate\View\Concerns; |
|
4 |
- |
|
5 |
-use Countable; |
|
6 |
-use Illuminate\Support\Arr; |
|
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_array($data) || $data instanceof Countable ? count($data) : null; |
|
26 |
- |
|
27 |
- $parent = Arr::last($this->loopsStack); |
|
28 |
- |
|
29 |
- $this->loopsStack[] = [ |
|
30 |
- 'iteration' => 0, |
|
31 |
- 'index' => 0, |
|
32 |
- 'remaining' => $length ?? null, |
|
33 |
- 'count' => $length, |
|
34 |
- 'first' => true, |
|
35 |
- 'last' => isset($length) ? $length == 1 : null, |
|
36 |
- 'odd' => false, |
|
37 |
- 'even' => true, |
|
38 |
- 'depth' => count($this->loopsStack) + 1, |
|
39 |
- 'parent' => $parent ? (object) $parent : null, |
|
40 |
- ]; |
|
41 |
- } |
|
42 |
- |
|
43 |
- /** |
|
44 |
- * Increment the top loop's indices. |
|
45 |
- * |
|
46 |
- * @return void |
|
47 |
- */ |
|
48 |
- public function incrementLoopIndices() |
|
49 |
- { |
|
50 |
- $loop = $this->loopsStack[$index = count($this->loopsStack) - 1]; |
|
51 |
- |
|
52 |
- $this->loopsStack[$index] = array_merge($this->loopsStack[$index], [ |
|
53 |
- 'iteration' => $loop['iteration'] + 1, |
|
54 |
- 'index' => $loop['iteration'], |
|
55 |
- 'first' => $loop['iteration'] == 0, |
|
56 |
- 'odd' => ! $loop['odd'], |
|
57 |
- 'even' => ! $loop['even'], |
|
58 |
- 'remaining' => isset($loop['count']) ? $loop['remaining'] - 1 : null, |
|
59 |
- 'last' => isset($loop['count']) ? $loop['iteration'] == $loop['count'] - 1 : null, |
|
60 |
- ]); |
|
61 |
- } |
|
62 |
- |
|
63 |
- /** |
|
64 |
- * Pop a loop from the top of the loop stack. |
|
65 |
- * |
|
66 |
- * @return void |
|
67 |
- */ |
|
68 |
- public function popLoop() |
|
69 |
- { |
|
70 |
- array_pop($this->loopsStack); |
|
71 |
- } |
|
72 |
- |
|
73 |
- /** |
|
74 |
- * Get an instance of the last loop in the stack. |
|
75 |
- * |
|
76 |
- * @return \stdClass|null |
|
77 |
- */ |
|
78 |
- public function getLastLoop() |
|
79 |
- { |
|
80 |
- if ($last = Arr::last($this->loopsStack)) { |
|
81 |
- return (object) $last; |
|
82 |
- } |
|
83 |
- } |
|
84 |
- |
|
85 |
- /** |
|
86 |
- * Get the entire loop stack. |
|
87 |
- * |
|
88 |
- * @return array |
|
89 |
- */ |
|
90 |
- public function getLoopStack() |
|
91 |
- { |
|
92 |
- return $this->loopsStack; |
|
93 |
- } |
|
94 |
-} |
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,94 @@ |
1 |
+<?php |
|
2 |
+ |
|
3 |
+namespace Illuminate\View\Concerns; |
|
4 |
+ |
|
5 |
+use Countable; |
|
6 |
+use Illuminate\Support\Arr; |
|
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_array($data) || $data instanceof Countable ? count($data) : null; |
|
26 |
+ |
|
27 |
+ $parent = Arr::last($this->loopsStack); |
|
28 |
+ |
|
29 |
+ $this->loopsStack[] = [ |
|
30 |
+ 'iteration' => 0, |
|
31 |
+ 'index' => 0, |
|
32 |
+ 'remaining' => $length ?? null, |
|
33 |
+ 'count' => $length, |
|
34 |
+ 'first' => true, |
|
35 |
+ 'last' => isset($length) ? $length == 1 : null, |
|
36 |
+ 'odd' => false, |
|
37 |
+ 'even' => true, |
|
38 |
+ 'depth' => count($this->loopsStack) + 1, |
|
39 |
+ 'parent' => $parent ? (object) $parent : null, |
|
40 |
+ ]; |
|
41 |
+ } |
|
42 |
+ |
|
43 |
+ /** |
|
44 |
+ * Increment the top loop's indices. |
|
45 |
+ * |
|
46 |
+ * @return void |
|
47 |
+ */ |
|
48 |
+ public function incrementLoopIndices() |
|
49 |
+ { |
|
50 |
+ $loop = $this->loopsStack[$index = count($this->loopsStack) - 1]; |
|
51 |
+ |
|
52 |
+ $this->loopsStack[$index] = array_merge($this->loopsStack[$index], [ |
|
53 |
+ 'iteration' => $loop['iteration'] + 1, |
|
54 |
+ 'index' => $loop['iteration'], |
|
55 |
+ 'first' => $loop['iteration'] == 0, |
|
56 |
+ 'odd' => ! $loop['odd'], |
|
57 |
+ 'even' => ! $loop['even'], |
|
58 |
+ 'remaining' => isset($loop['count']) ? $loop['remaining'] - 1 : null, |
|
59 |
+ 'last' => isset($loop['count']) ? $loop['iteration'] == $loop['count'] - 1 : null, |
|
60 |
+ ]); |
|
61 |
+ } |
|
62 |
+ |
|
63 |
+ /** |
|
64 |
+ * Pop a loop from the top of the loop stack. |
|
65 |
+ * |
|
66 |
+ * @return void |
|
67 |
+ */ |
|
68 |
+ public function popLoop() |
|
69 |
+ { |
|
70 |
+ array_pop($this->loopsStack); |
|
71 |
+ } |
|
72 |
+ |
|
73 |
+ /** |
|
74 |
+ * Get an instance of the last loop in the stack. |
|
75 |
+ * |
|
76 |
+ * @return \stdClass|null |
|
77 |
+ */ |
|
78 |
+ public function getLastLoop() |
|
79 |
+ { |
|
80 |
+ if ($last = Arr::last($this->loopsStack)) { |
|
81 |
+ return (object) $last; |
|
82 |
+ } |
|
83 |
+ } |
|
84 |
+ |
|
85 |
+ /** |
|
86 |
+ * Get the entire loop stack. |
|
87 |
+ * |
|
88 |
+ * @return array |
|
89 |
+ */ |
|
90 |
+ public function getLoopStack() |
|
91 |
+ { |
|
92 |
+ return $this->loopsStack; |
|
93 |
+ } |
|
94 |
+} |