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

FieldFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 1
dl 0
loc 33
ccs 0
cts 22
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createByModelAttributes() 0 20 3
A getHierarchySeparator() 0 4 1
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