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

Query::restoreHierarchy()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 10
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
crap 12
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