CollectionTrait::build()   A
last analyzed

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.9666
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 11
    public function build(): array {
10 11
        $output = [];
11
12 11
        $data = $this->getData();
13
        /** @var \Traversable $modelCollection */
14 11
        $modelCollection = \array_shift($data);
15
16 11
        foreach ($modelCollection as $model) {
17 11
            $item = $this->mapData($model, ...$data);
18
19 11
            if (!\is_array($item)) {
20 1
                continue;
21
            }
22
23 11
            $output[] = $item;
24
        }
25
26 11
        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
}