Skip to content

Commit b08aac6

Browse files
committed
add nested or not to collection
1 parent ce6d8dc commit b08aac6

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

src/Collection.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -273,27 +273,40 @@ public function count()
273273

274274

275275
/**
276+
* @param bool $nested
276277
* @return array
277278
* @throws \Exception
278279
*/
279-
public function toArray()
280+
public function toArray($nested = true)
280281
{
281282
$ar = [];
282283

283-
foreach ($this->items as $item){
284+
if($nested){
285+
foreach ($this->items as $item){
286+
287+
if(! method_exists($item, 'toArray')){
288+
throw new \Exception('item class has not method toArray');
289+
}
290+
else{
291+
$ar[] = $item->toArray();
292+
}
284293

285-
if(! method_exists($item, 'toArray')){
286-
throw new \Exception('item class has not method toArray');
287294
}
288-
else{
289-
$ar[] = $item->toArray();
295+
}else{
296+
foreach ($this->items as $item){
297+
$ar[] = $item;
290298
}
291-
292299
}
293300

301+
294302
return $ar;
295303
}
304+
296305

306+
/**
307+
* @return array
308+
* @throws \Exception
309+
*/
297310
public function preview()
298311
{
299312
$ar = [];

0 commit comments

Comments
 (0)