15
15
16
16
class NestableCollection extends Collection
17
17
{
18
- protected $ total ;
18
+ protected int $ total ;
19
19
20
- protected $ parentColumn ;
20
+ protected string $ parentColumn ;
21
21
22
- protected $ removeItemsWithMissingAncestor = true ;
22
+ protected bool $ removeItemsWithMissingAncestor = true ;
23
23
24
- protected $ indentChars = ' ' ;
24
+ protected string $ indentChars = ' ' ;
25
25
26
- protected $ childrenName = 'items ' ;
26
+ protected string $ childrenName = 'items ' ;
27
27
28
- protected $ parentRelation = 'parent ' ;
28
+ protected string $ parentRelation = 'parent ' ;
29
29
30
- public function __construct ($ items = [])
30
+ public function __construct (array $ items = [])
31
31
{
32
32
parent ::__construct ($ items );
33
33
$ this ->parentColumn = 'parent_id ' ;
34
34
$ this ->total = count ($ items );
35
35
}
36
36
37
- public function childrenName ($ name )
37
+ public function childrenName (string $ name ): self
38
38
{
39
39
$ this ->childrenName = $ name ;
40
40
@@ -43,10 +43,8 @@ public function childrenName($name)
43
43
44
44
/**
45
45
* Nest items.
46
- *
47
- * @return mixed NestableCollection
48
46
*/
49
- public function nest ()
47
+ public function nest (): self
50
48
{
51
49
$ parentColumn = $ this ->parentColumn ;
52
50
if (!$ parentColumn ) {
@@ -80,6 +78,7 @@ public function nest()
80
78
foreach ($ collection ->items as $ item ) {
81
79
if ($ item ->{$ parentColumn } && isset ($ collection [$ item ->{$ parentColumn }])) {
82
80
$ collection [$ item ->{$ parentColumn }]->{$ this ->childrenName }->push ($ item );
81
+ // @phpstan-ignore-next-line
83
82
$ keysToDelete [] = $ item ->id ;
84
83
}
85
84
}
@@ -93,29 +92,21 @@ public function nest()
93
92
/**
94
93
* Recursive function that flatten a nested Collection
95
94
* with characters (default is four spaces).
96
- *
97
- * @param string $column
98
- * @param int $level
99
- * @param array &$flattened
100
- * @param null|string $indentChars
101
- * @param null|bool|string $parent_string
102
- *
103
- * @return array
104
95
*/
105
- public function listsFlattened ($ column = 'title ' , BaseCollection $ collection = null , $ level = 0 , array &$ flattened = [], $ indentChars = null , $ parent_string = null )
96
+ public function listsFlattened (string $ column = 'title ' , BaseCollection $ collection = null , int $ level = 0 , array &$ flattened = [], ? string $ indentChars = null , mixed $ parentString = null ): array
106
97
{
107
98
$ collection = $ collection ?: $ this ;
108
99
$ indentChars = $ indentChars ?: $ this ->indentChars ;
109
100
foreach ($ collection as $ item ) {
110
- if ($ parent_string ) {
111
- $ item_string = ($ parent_string === true ) ? $ item ->{$ column } : $ parent_string .$ indentChars .$ item ->{$ column };
101
+ if ($ parentString ) {
102
+ $ item_string = ($ parentString === true ) ? $ item ->{$ column } : $ parentString .$ indentChars .$ item ->{$ column };
112
103
} else {
113
104
$ item_string = str_repeat ($ indentChars , $ level ).$ item ->{$ column };
114
105
}
115
106
116
107
$ flattened [$ item ->id ] = $ item_string ;
117
108
if ($ item ->{$ this ->childrenName }) {
118
- $ this ->listsFlattened ($ column , $ item ->{$ this ->childrenName }, $ level + 1 , $ flattened , $ indentChars , ($ parent_string ) ? $ item_string : null );
109
+ $ this ->listsFlattened ($ column , $ item ->{$ this ->childrenName }, $ level + 1 , $ flattened , $ indentChars , ($ parentString ) ? $ item_string : null );
119
110
}
120
111
}
121
112
@@ -124,27 +115,16 @@ public function listsFlattened($column = 'title', BaseCollection $collection = n
124
115
125
116
/**
126
117
* Returns a fully qualified version of listsFlattened.
127
- *
128
- * @param string $column
129
- * @param int $level
130
- * @param array &$flattened
131
- * @param string $indentChars
132
- *
133
- * @return array
134
118
*/
135
- public function listsFlattenedQualified ($ column = 'title ' , BaseCollection $ collection = null , $ level = 0 , array &$ flattened = [], $ indentChars = null )
119
+ public function listsFlattenedQualified (string $ column = 'title ' , BaseCollection $ collection = null , int $ level = 0 , array &$ flattened = [], ? string $ indentChars = null ): array
136
120
{
137
121
return $ this ->listsFlattened ($ column , $ collection , $ level , $ flattened , $ indentChars , true );
138
122
}
139
123
140
124
/**
141
125
* Change the default indent characters when flattening lists.
142
- *
143
- * @param string $indentChars
144
- *
145
- * @return $this
146
126
*/
147
- public function setIndent ($ indentChars )
127
+ public function setIndent (string $ indentChars ): self
148
128
{
149
129
$ this ->indentChars = $ indentChars ;
150
130
@@ -153,10 +133,8 @@ public function setIndent($indentChars)
153
133
154
134
/**
155
135
* Force keeping items that have a missing ancestor.
156
- *
157
- * @return NestableCollection
158
136
*/
159
- public function noCleaning ()
137
+ public function noCleaning (): self
160
138
{
161
139
$ this ->removeItemsWithMissingAncestor = false ;
162
140
@@ -165,12 +143,8 @@ public function noCleaning()
165
143
166
144
/**
167
145
* Check if an ancestor is missing.
168
- *
169
- * @param mixed $item
170
- *
171
- * @return bool
172
146
*/
173
- public function anAncestorIsMissing ($ item )
147
+ public function anAncestorIsMissing (mixed $ item ): bool
174
148
{
175
149
$ parentColumn = $ this ->parentColumn ;
176
150
if (!$ item ->{$ parentColumn }) {
@@ -186,38 +160,33 @@ public function anAncestorIsMissing($item)
186
160
187
161
/**
188
162
* Get total items in nested collection.
189
- *
190
- * @return int
191
163
*/
192
- public function total ()
164
+ public function total (): int
193
165
{
194
166
return $ this ->total ;
195
167
}
196
168
197
169
/**
198
170
* Get total items for laravel 4 compatibility.
199
- *
200
- * @return int
201
171
*/
202
- public function getTotal ()
172
+ public function getTotal (): int
203
173
{
204
174
return $ this ->total ();
205
175
}
206
176
207
177
/**
208
- * Sets the $item->parent relation for each item in the NestableCollection to be the parent it has in the collection
178
+ * Sets the $item->parent relation for each item in the
179
+ * NestableCollection to be the parent it has in the collection
209
180
* so it can be used without querying the database.
210
- *
211
- * @return $this
212
181
*/
213
- public function setParents ()
182
+ public function setParents (): self
214
183
{
215
184
$ this ->setParentsRecursive ($ this );
216
185
217
186
return $ this ;
218
187
}
219
188
220
- protected function setParentsRecursive (&$ items , &$ parent = null )
189
+ protected function setParentsRecursive (self &$ items , &$ parent = null ): void
221
190
{
222
191
foreach ($ items as $ item ) {
223
192
if ($ parent ) {
0 commit comments