1 | <?php |
||
11 | abstract class AbstractTariffForm extends \yii\base\Model |
||
12 | { |
||
13 | /** |
||
14 | * @var int Tariff ID |
||
15 | */ |
||
16 | public $id; |
||
17 | |||
18 | /** |
||
19 | * @var string Tariff name |
||
20 | */ |
||
21 | public $name; |
||
22 | |||
23 | /** |
||
24 | * @var int Parent tariff ID |
||
25 | */ |
||
26 | public $parent_id; |
||
27 | |||
28 | /** |
||
29 | * @var Tariff[] array of available parent tariffs |
||
30 | */ |
||
31 | public $parentTariffs; |
||
32 | |||
33 | /** |
||
34 | * @var Tariff the selected parent tariff |
||
35 | */ |
||
36 | public $parentTariff; |
||
37 | |||
38 | /** |
||
39 | * @var Tariff |
||
40 | */ |
||
41 | protected $tariff; |
||
42 | |||
43 | /** |
||
44 | * @var \hipanel\modules\finance\models\Resource[] |
||
45 | */ |
||
46 | protected $_resources; |
||
47 | |||
48 | /** |
||
49 | * @inheritdoc |
||
50 | */ |
||
51 | public function init() |
||
52 | { |
||
53 | if (!isset($this->parentTariffs)) { |
||
54 | throw new InvalidConfigException('Property "parentTariffs" must be filled'); |
||
55 | } |
||
56 | |||
57 | $this->initTariff(); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Initializes tariff |
||
62 | * @void |
||
63 | */ |
||
64 | protected function initTariff() |
||
65 | { |
||
66 | $this->selectParentTariff(); |
||
67 | $this->ensureTariff(); |
||
68 | $this->ensureScenario(); |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Ensures that [[tariff]] is set. |
||
73 | * Otherwise calls [[setDefaultTariff()]] |
||
74 | * @return bool |
||
75 | */ |
||
76 | protected function ensureTariff() |
||
77 | { |
||
78 | if ($this->getTariff() instanceof Tariff) { |
||
79 | return true; |
||
80 | } |
||
81 | |||
82 | return $this->setDefaultTariff(); |
||
83 | } |
||
84 | |||
85 | protected function ensureScenario() |
||
86 | { |
||
87 | foreach ($this->tariff->resources as $resource) { |
||
88 | $resource->scenario = $this->scenario; |
||
89 | } |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Sets default tariff |
||
94 | * |
||
95 | * @return bool |
||
96 | */ |
||
97 | protected function setDefaultTariff() |
||
98 | { |
||
99 | $this->setTariff($this->parentTariff); |
||
100 | |||
101 | // Default tariff's id and name are useless on create |
||
102 | $this->id = null; |
||
103 | $this->name = null; |
||
104 | |||
105 | return true; |
||
106 | } |
||
107 | |||
108 | /** @inheritdoc */ |
||
109 | public function rules() |
||
110 | { |
||
111 | return [ |
||
112 | [['name'], 'required', 'on' => ['create', 'update']], |
||
113 | [['parent_id', 'id'], 'integer', 'on' => ['create', 'update']], |
||
114 | [['parent_id'], 'required', 'on' => ['create']], |
||
115 | [['id'], 'required', 'on' => ['update']], |
||
116 | ]; |
||
117 | } |
||
118 | |||
119 | /** @inheritdoc */ |
||
120 | public function fields() |
||
126 | |||
127 | /** @inheritdoc */ |
||
128 | public function attributes() |
||
129 | { |
||
130 | return [ |
||
131 | 'id', |
||
132 | 'parent_id', |
||
133 | 'name', |
||
134 | ]; |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * @return \hipanel\modules\finance\models\Resource[] |
||
139 | */ |
||
140 | public function getResources() |
||
144 | |||
145 | /** |
||
146 | * @param \hipanel\modules\finance\models\Resource[] $resources |
||
147 | * @throws InvalidConfigException when not implemented |
||
148 | */ |
||
149 | public function setResources($resources) |
||
153 | |||
154 | /** |
||
155 | * @return array |
||
156 | */ |
||
157 | public function getResourceTypes() |
||
158 | { |
||
159 | return reset($this->parentTariff->resources)->getTypes(); |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * @return array |
||
164 | */ |
||
165 | public function attributeLabels() |
||
166 | { |
||
167 | return [ |
||
168 | 'parent_id' => Yii::t('hipanel/finance/tariff', 'Parent tariff'), |
||
169 | 'name' => Yii::t('hipanel/finance/tariff', 'Name'), |
||
170 | 'label' => Yii::t('hipanel/finance/tariff', 'Label'), |
||
171 | 'note' => Yii::t('hipanel', 'Note'), |
||
172 | ]; |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * @param array $data to be loaded |
||
177 | * @param null $formName |
||
178 | * @return bool |
||
179 | * @throws InvalidConfigException when not implemented |
||
180 | */ |
||
181 | public function load($data, $formName = null) |
||
182 | { |
||
183 | throw new InvalidConfigException("Method load must be implemented"); |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * Selects one of [[parentTariffs]] and puts it to [[parentTariff]] |
||
188 | * |
||
189 | * @return bool |
||
190 | */ |
||
191 | public function selectParentTariff() |
||
192 | { |
||
193 | if (!isset($this->parent_id)) { |
||
194 | if (isset($this->tariff)) { |
||
195 | $this->parent_id = $this->tariff->parent_id; |
||
|
|||
196 | } else { |
||
197 | $this->parent_id = ArrayHelper::getValue(reset($this->parentTariffs), 'id'); |
||
198 | } |
||
199 | } |
||
200 | |||
201 | $filtered = array_filter($this->parentTariffs, function ($model) { |
||
202 | return $model->id == $this->parent_id; |
||
203 | }); |
||
204 | |||
205 | if (count($filtered) !== 1) { |
||
206 | Yii::error('Found ' . count($filtered) . ' parent tariffs. Must be exactly one'); |
||
207 | return false; |
||
208 | } |
||
209 | |||
210 | $this->parentTariff = reset($filtered); |
||
211 | $this->parent_id = $this->parentTariff->id; |
||
212 | |||
213 | return true; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Builds key-value array of [[parentTariffs]] |
||
218 | * - key: tariff id |
||
219 | * - value: tariff name |
||
220 | * |
||
221 | * @return array |
||
222 | */ |
||
223 | public function getParentTariffsList() |
||
224 | { |
||
225 | return array_combine( |
||
226 | ArrayHelper::getColumn($this->parentTariffs, 'id'), |
||
227 | ArrayHelper::getColumn($this->parentTariffs, 'name') |
||
228 | ); |
||
229 | } |
||
230 | |||
231 | public function insert($runValidation = true) |
||
232 | { |
||
233 | throw new InvalidConfigException("Method insert must be implemented"); |
||
234 | } |
||
235 | |||
236 | public function update($runValidation = true) |
||
240 | |||
241 | /** |
||
242 | * @return Tariff |
||
243 | */ |
||
244 | public function getTariff() |
||
245 | { |
||
246 | return $this->tariff; |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * Sets [[tariff]] |
||
251 | * |
||
252 | * @param Tariff $tariff |
||
253 | * @return bool |
||
254 | */ |
||
255 | public function setTariff($tariff) |
||
268 | |||
269 | public function getPrimaryKey() |
||
273 | |||
274 | /** |
||
275 | * @var TariffCalculator |
||
276 | */ |
||
277 | protected $_calculator; |
||
278 | |||
279 | /** |
||
280 | * Creates [[TariffCalculator]] object for the [[tariff]] |
||
281 | * |
||
282 | * @return TariffCalculator |
||
283 | */ |
||
284 | protected function calculator() |
||
294 | |||
295 | /** |
||
296 | * @return \hipanel\modules\finance\models\Value |
||
297 | */ |
||
298 | public function calculation() |
||
302 | |||
303 | /** |
||
304 | * @var TariffCalculator |
||
305 | */ |
||
306 | protected $_parentCalculator; |
||
307 | |||
308 | /** |
||
309 | * Creates [[TariffCalculator]] object for the [[parentTariff]] |
||
310 | * |
||
311 | * @return TariffCalculator |
||
312 | */ |
||
313 | protected function parentCalculator() |
||
323 | |||
324 | /** |
||
325 | * @return \hipanel\modules\finance\models\Value |
||
326 | */ |
||
327 | public function parentCalculation() |
||
331 | } |
||
332 |
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.