Passed
Push — master ( 662d52...2a8232 )
by eXeCUT
03:58
created

components/Parser.php (1 issue)

1
<?php
2
/**
3
 * User: execut
4
 * Date: 19.07.16
5
 * Time: 14:01
6
 */
7
8
namespace execut\import\components;
9
10
11
use execut\import\components\parser\Attribute;
12
use execut\import\components\parser\exception\ColumnIsEmpty;
13
use execut\import\components\parser\exception\MoreThanOne;
14
use execut\import\components\parser\exception\NotFoundRecord;
15
use execut\import\components\parser\exception\Validate;
16
use execut\import\components\parser\ModelsFinder;
17
use execut\yii\db\ActiveRecord;
18
use yii\base\Component;
19
use yii\base\Exception;
20
21
class Parser extends Component
22
{
23
    public $query = null;
24
    public $row = null;
25
    protected $attributes = [];
26
    public $isValidate = true;
27
    /**
28
     * @var ModelsFinder
29
     */
30
    protected $modelsFinder = [];
31
    protected $_stack = null;
32
33 3
    public function setModelsFinder($finder) {
34 3
        $this->modelsFinder = $finder;
35 3
    }
36
37 3
    public function setStack($stack) {
38 3
        $this->_stack = $stack;
39
40 3
        return $this;
41
    }
42
43 4
    public function getStack() {
44 4
        return $this->_stack;
45
    }
46
47
    /**
48
     * @return ModelsFinder
49
     */
50 3
    public function getModelsFinder() {
51 3
        if (is_array($this->modelsFinder)) {
0 ignored issues
show
The condition is_array($this->modelsFinder) is always false.
Loading history...
52 1
            if (empty($this->modelsFinder['class'])) {
53 1
                $this->modelsFinder['class'] = ModelsFinder::class;
54
            }
55
56 1
            $this->modelsFinder = \yii::createObject($this->modelsFinder);
57
        }
58
59 3
        $this->modelsFinder->stack = $this->getStack();
60
61 3
        return $this->modelsFinder;
62
    }
63
64 5
    public function setAttributes($attributes) {
65 5
        $result = [];
66 5
        foreach ($attributes as $key => $attributeParams) {
67 5
            if (is_object($attributeParams)) {
68 2
                $attribute = $attributeParams;
69
            } else {
70 3
                if (!is_array($attributeParams)) {
71
                    $attributeParams = [
72 1
                        'value' => $attributeParams,
73
                    ];
74
                }
75
76 3
                if (empty($attributeParams['key'])) {
77 3
                    $attributeParams['key'] = $key;
78
                }
79
80 3
                $attribute = new Attribute($attributeParams);
81
            }
82
83 5
            $result[$key] = $attribute;
84
        }
85
86 5
        $this->attributes = $result;
87 5
    }
88
89 3
    public function getAttributes() {
90 3
        $attributes = $this->attributes;
91 3
        foreach ($attributes as $attribute) {
92 2
            $attribute->row = $this->row;
93
        }
94
95 3
        return $attributes;
96
    }
97
98 2
    public function parse() {
99 2
        $settedAttributes = [];
100 2
        $attributes = $this->attributes;
101 2
        foreach ($attributes as $key => $attribute) {
102 2
            $settedAttributes[$key] = $attribute->value;
103
        }
104
105 2
        $modelsFinder = $this->getModelsFinder();
106
107 2
        $modelsFinder->attributes = $attributes;
108
109 2
        $result = $modelsFinder->findModel();
110 2
        foreach ($result->getModels() as $model) {
111 1
            if ($this->isValidate) {
112 1
                $this->validateAttributes($model, array_keys($settedAttributes));
113
            }
114
        }
115
116 1
        return $result;
117
    }
118
119
    /**
120
     * @param ActiveRecord $model
121
     * @param $attributes
122
     */
123 1
    protected function validateAttributes($model, $attributesKeys) {
124 1
        $columns = [];
125 1
        $attributes = $this->attributes;
126 1
        foreach ($attributesKeys as $attribute) {
127 1
            if (isset($attributes[$attribute]) && $attributes[$attribute]->column !== null) {
128 1
                if (!$model->validate([$attribute], false)) {
129 1
                    $e = new Validate();
130 1
                    $e->errors = $model->errors;
131 1
                    $e->columnNbr = $attributes[$attribute]->column;
132 1
                    $e->attribute = $attribute;
133
134 1
                    throw $e;
135
                }
136
            }
137
        }
138
    }
139
140
    /**
141
     * @param $modelClass
142
     * @param $result
143
     * @return mixed
144
     */
145
    protected function createModel($modelClass, $result)
146
    {
147
        $model = new $modelClass;
148
        foreach ($result as $key => $value) {
149
            $result[$key] = (string) $value;
150
        }
151
        $model->attributes = $result;
152
153
        return $model;
154
    }
155
}