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

Query   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 60
ccs 0
cts 27
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getFields() 0 4 1
A selectByFields() 0 10 3
A restoreHierarchy() 0 12 3
attributesMap() 0 1 ?
1
<?php
2
3
namespace hiapi\query;
4
5
abstract class Query extends \yii\db\Query
6
{
7
    /**
8
     * @var FieldFactoryInterface
9
     */
10
    protected $fieldFactory;
11
12
    /**
13
     * @var string
14
     */
15
    protected $modelClass;
16
17
    public function __construct(FieldFactoryInterface $filterFactory, array $config = [])
18
    {
19
        parent::__construct($config);
20
21
        $this->fieldFactory = $filterFactory;
22
    }
23
24
    /**
25
     * @return Field[]
26
     */
27
    public function getFields()
28
    {
29
        return $this->fieldFactory->createByModelAttributes(new $this->modelClass, $this->attributesMap());
30
    }
31
32
    /**
33
     * @param Field[] $fields
34
     * @return $this
35
     */
36
    protected function selectByFields($fields)
37
    {
38
        foreach ($fields as $field) {
39
            if ($field->canBeSelected()) {
40
                $this->addSelect($field->getSql() . ' as ' . $field->getName());
41
            }
42
        }
43
44
        return $this;
45
    }
46
47
    public function restoreHierarchy($row)
48
    {
49
        foreach ($row as $key => $value) {
50
            $parts = explode($this->fieldFactory->getHierarchySeparator(), $key, 2);
51
            if (count($parts) > 1) {
52
                $row[$parts[0]][$parts[1]] = $value;
53
                unset($row[$key]);
54
            }
55
        }
56
57
        return $row;
58
    }
59
60
    /**
61
     * @return mixed
62
     */
63
    abstract protected function attributesMap();
64
}
65