Passed
Push — master ( d4e337...e0198f )
by Martin
02:43
created

CollectionTrait::build()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 0
dl 0
loc 18
ccs 10
cts 10
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace KingsonDe\Marshal\Data;
6
7
trait CollectionTrait {
8
9 6
    public function build(): array {
10 6
        $output = [];
11
12 6
        $data = $this->getData();
13
        /** @var \Traversable $modelCollection */
14 6
        $modelCollection = \array_shift($data);
15
16 6
        foreach ($modelCollection as $model) {
17 6
            $item = $this->mapData($model, ...$data);
18
19 6
            if (!\is_array($item)) {
20 1
                continue;
21
            }
22
23 6
            $output[] = $item;
24
        }
25
26 6
        return $output;
27
    }
28
29
    abstract protected function getData(): array;
30
31
    /**
32
     * @param mixed $model
33
     * @param array $data
34
     * @return array|null
35
     */
36
    abstract protected function mapData($model, ...$data);
37
}