Completed
Push — master ( 370309...3a15aa )
by Ben
03:14
created

BuildArray::fromCollection()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 18
ccs 9
cts 9
cp 1
rs 8.8571
cc 5
eloc 9
nc 6
nop 2
crap 5
1
<?php
2
3
namespace Thinktomorrow\Vine\Branch;
4
5
class BuildArray
6
{
7 3
    public static function fromCollection(BranchCollection $collection = null,array $mapping = [])
8
    {
9 3
        $array = [];
10
11 3
        if(empty($collection)) return $array;
12
13 3
        foreach($collection as $branch)
14
        {
15 3
            $children = $branch->hasBranches() ? self::fromCollection($branch->branches,$mapping) : null;
16 3
17
            $record = ['id' => $branch->key,'slug' => $branch->value,'name' => $branch->label];
18 3
            if($children) $record['children'] = $children;
19 2
20
            $array[] = $record;
21 3
        }
22
23
        return $array;
24
    }
25
}
26