11<?php
22
3+ declare (strict_types=1 );
4+
35/*
46 * (c) Samuel De Backer <sdebacker@gmail.com>
57 *
68 * For the full copyright and license information, please view the LICENSE
79 * file that was distributed with this source code.
810 */
9-
1011namespace TypiCMS ;
1112
1213use Illuminate \Database \Eloquent \Collection ;
@@ -76,7 +77,7 @@ public function noCleaning(): self
7677 /** @return self<TKey, TModel> */
7778 public function nest (): self
7879 {
79- if (! $ this ->parentColumn ) {
80+ if ($ this -> parentColumn === '' || $ this ->parentColumn === ' 0 ' ) {
8081 return $ this ;
8182 }
8283
@@ -95,10 +96,10 @@ public function nest(): self
9596 return $ this ;
9697 }
9798
98- public function anAncestorIsMissing (Model $ item ): bool
99+ public function anAncestorIsMissing (Model $ model ): bool
99100 {
100101 /** @var int|string|null $parentId */
101- $ parentId = $ item ->{$ this ->parentColumn };
102+ $ parentId = $ model ->{$ this ->parentColumn };
102103
103104 if (! $ parentId ) {
104105 return false ;
@@ -115,23 +116,23 @@ public function anAncestorIsMissing(Model $item): bool
115116 }
116117
117118 /**
118- * @param BaseCollection<array-key, Model>|null $collection
119+ * @param BaseCollection<array-key, Model>|null $baseCollection
119120 * @param array<int|string, string> $flattened
120121 * @return array<int|string, string>
121122 */
122123 public function listsFlattened (
123124 string $ column = 'title ' ,
124- ?BaseCollection $ collection = null ,
125+ ?BaseCollection $ baseCollection = null ,
125126 int $ level = 0 ,
126127 array &$ flattened = [],
127128 ?string $ indentChars = null ,
128129 mixed $ parentString = null ,
129130 ): array {
130- $ collection ??= $ this ;
131+ $ baseCollection ??= $ this ;
131132 $ indentChars ??= $ this ->indentChars ;
132133
133134 /** @var Model $item */
134- foreach ($ collection as $ item ) {
135+ foreach ($ baseCollection as $ item ) {
135136 $ itemString = $ this ->buildFlattenedLabel ($ item , $ column , $ indentChars , $ level , $ parentString );
136137 /** @var int|string $key */
137138 $ key = $ item ->getKey ();
@@ -156,18 +157,18 @@ public function listsFlattened(
156157 }
157158
158159 /**
159- * @param BaseCollection<array-key, Model>|null $collection
160+ * @param BaseCollection<array-key, Model>|null $baseCollection
160161 * @param array<int|string, string> $flattened
161162 * @return array<int|string, string>
162163 */
163164 public function listsFlattenedQualified (
164165 string $ column = 'title ' ,
165- ?BaseCollection $ collection = null ,
166+ ?BaseCollection $ baseCollection = null ,
166167 int $ level = 0 ,
167168 array &$ flattened = [],
168169 ?string $ indentChars = null ,
169170 ): array {
170- return $ this ->listsFlattened ($ column , $ collection , $ level , $ flattened , $ indentChars , true );
171+ return $ this ->listsFlattened ($ column , $ baseCollection , $ level , $ flattened , $ indentChars , true );
171172 }
172173
173174 public function total (): int
@@ -200,7 +201,7 @@ protected function initializeChildrenCollections(): void
200201 protected function rejectOrphans (): static
201202 {
202203 /** @var static */
203- return $ this ->reject (fn ($ item ) => $ item ->{$ this ->parentColumn } && $ this ->anAncestorIsMissing ($ item ));
204+ return $ this ->reject (fn ($ item ): bool => $ item ->{$ this ->parentColumn } && $ this ->anAncestorIsMissing ($ item ));
204205 }
205206
206207 /**
@@ -213,10 +214,12 @@ protected function assignChildrenToParents(self|BaseCollection $collection): arr
213214
214215 /** @var Model $item */
215216 foreach ($ collection as $ item ) {
216- if (! $ item ->{$ this ->parentColumn } || ! isset ($ collection [$ item ->{$ this ->parentColumn }])) {
217+ if (! $ item ->{$ this ->parentColumn }) {
218+ continue ;
219+ }
220+ if (! isset ($ collection [$ item ->{$ this ->parentColumn }])) {
217221 continue ;
218222 }
219-
220223 /** @var BaseCollection<array-key, Model> $parentChildren */
221224 $ parentChildren = $ collection [$ item ->{$ this ->parentColumn }]->{$ this ->childrenName };
222225 $ parentChildren ->push ($ item );
@@ -227,14 +230,14 @@ protected function assignChildrenToParents(self|BaseCollection $collection): arr
227230 }
228231
229232 protected function buildFlattenedLabel (
230- Model $ item ,
233+ Model $ model ,
231234 string $ column ,
232235 string $ indentChars ,
233236 int $ level ,
234237 mixed $ parentString ,
235238 ): string {
236239 /** @var string $value */
237- $ value = $ item ->{$ column };
240+ $ value = $ model ->{$ column };
238241
239242 if (! $ parentString ) {
240243 return str_repeat ($ indentChars , $ level ).$ value ;
@@ -248,13 +251,13 @@ protected function buildFlattenedLabel(
248251 return $ parentString .$ indentChars .$ value ;
249252 }
250253
251- /** @param BaseCollection<array-key, Model> $items */
252- protected function setParentsRecursive (BaseCollection $ items , ?Model $ parent = null ): void
254+ /** @param BaseCollection<array-key, Model> $baseCollection */
255+ protected function setParentsRecursive (BaseCollection $ baseCollection , ?Model $ model = null ): void
253256 {
254257 /** @var Model $item */
255- foreach ($ items as $ item ) {
256- if ($ parent ) {
257- $ item ->setRelation ($ this ->parentRelation , $ parent );
258+ foreach ($ baseCollection as $ item ) {
259+ if ($ model instanceof \ Illuminate \ Database \ Eloquent \Model ) {
260+ $ item ->setRelation ($ this ->parentRelation , $ model );
258261 }
259262
260263 /** @var BaseCollection<array-key, Model> $children */
0 commit comments