Completed
Push — master ( 9d10cb...2a8386 )
by Dmitry
03:18
created

FieldFactory::getHierarchySeparator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace hiapi\query;
4
5
use hiqdev\billing\hiapi\models\ModelInterface;
6
7
class FieldFactory implements FieldFactoryInterface
8
{
9
    /**
10
     * @param ModelInterface $model
11
     * @param $map
12
     * @return Field[]
13
     */
14
    public function createByModelAttributes($model, $map, $parentRelations = [])
15
    {
16
        $result = [];
17
18
        foreach ($map as $attributeName => $definition) {
19
            if (!is_array($definition)) {
20
                $name = implode(array_merge($parentRelations, [$attributeName]), $this->getHierarchySeparator());
21
                $result[] = new Field($name, $definition, $model->getAttribute($attributeName));
22
            } else {
23
                $relationClass = $model->getRelation($attributeName);
24
                $result = array_merge($result, $this->createByModelAttributes(
25
                    new $relationClass,
26
                    $definition,
27
                    array_merge($parentRelations, [$attributeName])
28
                ));
29
            }
30
        }
31
32
        return $result;
33
    }
34
35
    public function getHierarchySeparator(): string
36
    {
37
        return '-';
38
    }
39
}
40