1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\core\models; |
4
|
|
|
|
5
|
|
|
use app\core\exceptions\InvalidArgumentException; |
6
|
|
|
use app\core\types\ReimbursementStatus; |
7
|
|
|
use app\core\types\RuleStatus; |
8
|
|
|
use app\core\types\TransactionStatus; |
9
|
|
|
use app\core\types\TransactionType; |
10
|
|
|
use Yii; |
11
|
|
|
use yii\behaviors\TimestampBehavior; |
12
|
|
|
use yiier\helpers\DateHelper; |
13
|
|
|
use yiier\validators\ArrayValidator; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* This is the model class for table "{{%rule}}". |
17
|
|
|
* |
18
|
|
|
* @property int $id |
19
|
|
|
* @property int $user_id |
20
|
|
|
* @property string $name |
21
|
|
|
* @property string|array $if_keywords Multiple choice use, |
22
|
|
|
* @property int $then_transaction_type |
23
|
|
|
* @property int|null $then_category_id |
24
|
|
|
* @property int|null $then_from_account_id |
25
|
|
|
* @property int|null $then_to_account_id |
26
|
|
|
* @property int|null $then_transaction_status |
27
|
|
|
* @property int|null $then_reimbursement_status |
28
|
|
|
* @property string|null|array $then_tags Multiple choice use, |
29
|
|
|
* @property int|string $status |
30
|
|
|
* @property int|null $sort |
31
|
|
|
* @property string|null $created_at |
32
|
|
|
* @property string|null $updated_at |
33
|
|
|
* |
34
|
|
|
* @property-read Category $thenCategory |
35
|
|
|
*/ |
36
|
|
|
class Rule extends \yii\db\ActiveRecord |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
|
|
public static function tableName() |
42
|
|
|
{ |
43
|
|
|
return '{{%rule}}'; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @inheritdoc |
48
|
|
|
* @throws \yii\base\InvalidConfigException |
49
|
|
|
*/ |
50
|
|
|
public function behaviors() |
51
|
|
|
{ |
52
|
|
|
return [ |
53
|
|
|
[ |
54
|
|
|
'class' => TimestampBehavior::class, |
55
|
|
|
'value' => Yii::$app->formatter->asDatetime('now') |
56
|
|
|
], |
57
|
|
|
]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function rules() |
64
|
|
|
{ |
65
|
|
|
return [ |
66
|
|
|
[['name', 'if_keywords', 'then_transaction_type'], 'required'], |
67
|
|
|
[['user_id', 'then_category_id', 'then_from_account_id', 'then_to_account_id', 'sort'], 'integer'], |
68
|
|
|
['status', 'in', 'range' => RuleStatus::names()], |
69
|
|
|
['then_transaction_type', 'in', 'range' => TransactionType::names()], |
70
|
|
|
['then_reimbursement_status', 'in', 'range' => ReimbursementStatus::names()], |
71
|
|
|
['then_transaction_status', 'in', 'range' => TransactionStatus::names()], |
72
|
|
|
[['if_keywords', 'then_tags'], ArrayValidator::class], |
73
|
|
|
[['name'], 'string', 'max' => 255], |
74
|
|
|
]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
|
|
public function attributeLabels() |
81
|
|
|
{ |
82
|
|
|
return [ |
83
|
|
|
'id' => Yii::t('app', 'ID'), |
84
|
|
|
'user_id' => Yii::t('app', 'User ID'), |
85
|
|
|
'name' => Yii::t('app', 'Name'), |
86
|
|
|
'if_keywords' => Yii::t('app', 'If Keywords'), |
87
|
|
|
'then_transaction_type' => Yii::t('app', 'Then Transaction Type'), |
88
|
|
|
'then_category_id' => Yii::t('app', 'Then Category ID'), |
89
|
|
|
'then_from_account_id' => Yii::t('app', 'Then From Account ID'), |
90
|
|
|
'then_to_account_id' => Yii::t('app', 'Then To Account ID'), |
91
|
|
|
'then_transaction_status' => Yii::t('app', 'Then Transaction Status'), |
92
|
|
|
'then_reimbursement_status' => Yii::t('app', 'Then Reimbursement Status'), |
93
|
|
|
'then_tags' => Yii::t('app', 'Then Tags'), |
94
|
|
|
'status' => Yii::t('app', 'Status'), |
95
|
|
|
'sort' => Yii::t('app', 'Sort'), |
96
|
|
|
'created_at' => Yii::t('app', 'Created At'), |
97
|
|
|
'updated_at' => Yii::t('app', 'Updated At'), |
98
|
|
|
]; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param bool $insert |
103
|
|
|
* @return bool |
104
|
|
|
* @throws InvalidArgumentException |
105
|
|
|
*/ |
106
|
|
|
public function beforeSave($insert) |
107
|
|
|
{ |
108
|
|
|
if (parent::beforeSave($insert)) { |
109
|
|
|
if ($insert) { |
110
|
|
|
$this->user_id = Yii::$app->user->id; |
|
|
|
|
111
|
|
|
} |
112
|
|
|
$this->then_reimbursement_status = is_null($this->then_reimbursement_status) ? |
113
|
|
|
ReimbursementStatus::NONE : ReimbursementStatus::toEnumValue($this->then_reimbursement_status); |
114
|
|
|
$this->then_transaction_status = is_null($this->then_transaction_status) ? |
115
|
|
|
TransactionStatus::DONE : TransactionStatus::toEnumValue($this->then_transaction_status); |
116
|
|
|
|
117
|
|
|
$this->status = is_null($this->status) ? RuleStatus::ACTIVE : RuleStatus::toEnumValue($this->status); |
|
|
|
|
118
|
|
|
$this->then_transaction_type = TransactionType::toEnumValue($this->then_transaction_type); |
119
|
|
|
$this->if_keywords = $this->if_keywords ? implode(',', $this->if_keywords) : null; |
|
|
|
|
120
|
|
|
$this->then_tags = $this->then_tags ? implode(',', $this->then_tags) : null; |
121
|
|
|
return true; |
122
|
|
|
} else { |
123
|
|
|
return false; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function getThenCategory() |
128
|
|
|
{ |
129
|
|
|
return $this->hasOne(Category::class, ['id' => 'then_category_id']); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return array |
134
|
|
|
*/ |
135
|
|
|
public function fields() |
136
|
|
|
{ |
137
|
|
|
$fields = parent::fields(); |
138
|
|
|
unset($fields['user_id']); |
139
|
|
|
|
140
|
|
|
$fields['then_transaction_type'] = function (self $model) { |
141
|
|
|
return TransactionType::getName($model->then_transaction_type); |
142
|
|
|
}; |
143
|
|
|
|
144
|
|
|
$fields['then_transaction_type_text'] = function (self $model) { |
145
|
|
|
return data_get(TransactionType::texts(), $model->then_transaction_type); |
146
|
|
|
}; |
147
|
|
|
|
148
|
|
|
$fields['then_tags'] = function (self $model) { |
149
|
|
|
return $model->then_tags ? explode(',', $model->then_tags) : []; |
|
|
|
|
150
|
|
|
}; |
151
|
|
|
$fields['thenCategory'] = function (self $model) { |
152
|
|
|
return $model->thenCategory; |
153
|
|
|
}; |
154
|
|
|
|
155
|
|
|
$fields['if_keywords'] = function (self $model) { |
156
|
|
|
return explode(',', $model->if_keywords); |
|
|
|
|
157
|
|
|
}; |
158
|
|
|
|
159
|
|
|
$fields['status'] = function (self $model) { |
160
|
|
|
return RuleStatus::getName($model->status); |
|
|
|
|
161
|
|
|
}; |
162
|
|
|
|
163
|
|
|
$fields['then_reimbursement_status'] = function (self $model) { |
164
|
|
|
return ReimbursementStatus::getName($model->then_reimbursement_status); |
|
|
|
|
165
|
|
|
}; |
166
|
|
|
|
167
|
|
|
$fields['then_transaction_status'] = function (self $model) { |
168
|
|
|
return TransactionStatus::getName($model->then_transaction_status); |
|
|
|
|
169
|
|
|
}; |
170
|
|
|
|
171
|
|
|
$fields['created_at'] = function (self $model) { |
172
|
|
|
return DateHelper::datetimeToIso8601($model->created_at); |
|
|
|
|
173
|
|
|
}; |
174
|
|
|
|
175
|
|
|
$fields['updated_at'] = function (self $model) { |
176
|
|
|
return DateHelper::datetimeToIso8601($model->updated_at); |
|
|
|
|
177
|
|
|
}; |
178
|
|
|
|
179
|
|
|
return $fields; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.