1 | <?php |
||
21 | class OrderField extends \yii\db\ActiveRecord |
||
22 | { |
||
23 | /** |
||
24 | * @inheritdoc |
||
25 | */ |
||
26 | 3 | public static function tableName() |
|
30 | |||
31 | /** |
||
32 | * @param bool $forDropDown if false - return array or validators, true - key=>value for dropDown |
||
33 | * @return array |
||
34 | */ |
||
35 | 2 | public function getTypes($forDropDown = true) |
|
36 | { |
||
37 | $values = [ |
||
38 | 2 | 'string' => ['value', 'string'], |
|
39 | 'integer' => ['value', 'integer'], |
||
40 | 'boolean' => ['value', 'boolean', 'trueValue' => "1", 'falseValue' => "0", 'strict' => true], |
||
41 | 'float' => ['value', 'number'], |
||
42 | 'email' => ['value', 'email'], |
||
43 | 'ip' => ['value', 'ip'], |
||
44 | 'url' => ['value', 'url'], |
||
45 | 'object' => [ |
||
46 | 2 | 'value', |
|
47 | 2 | function ($attribute, $params) { |
|
|
|||
48 | $object = null; |
||
49 | try { |
||
50 | Json::decode($this->$attribute); |
||
51 | } catch (InvalidParamException $e) { |
||
52 | $this->addError($attribute, "$attribute must be a valid JSON object"); |
||
53 | } |
||
54 | 2 | } |
|
55 | ], |
||
56 | ]; |
||
57 | |||
58 | 2 | if (!$forDropDown) { |
|
59 | 1 | return $values; |
|
60 | } |
||
61 | |||
62 | 1 | $return = []; |
|
63 | 1 | foreach ($values as $key => $value) { |
|
64 | 1 | $return[$key] = $key; |
|
65 | } |
||
66 | |||
67 | 1 | return $return; |
|
68 | } |
||
69 | |||
70 | /** |
||
71 | * @inheritdoc |
||
72 | */ |
||
73 | 1 | public function rules() |
|
74 | { |
||
75 | return [ |
||
76 | 1 | ['order_id', 'integer'], |
|
77 | [['value'], 'string'], |
||
78 | ['key', 'string', 'max' => 255], |
||
79 | [ |
||
80 | ['key'], |
||
81 | 'unique', |
||
82 | 'targetAttribute' => ['order_id', 'key'], |
||
83 | 'message' => '{attribute} "{value}" already exists for this section.' |
||
84 | ], |
||
85 | 1 | ['type', 'in', 'range' => array_keys($this->getTypes(false))], |
|
86 | ['type', 'safe'], |
||
87 | ]; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * @inheritdoc |
||
92 | */ |
||
93 | 1 | public function attributeLabels() |
|
103 | |||
104 | 1 | public function beforeSave($insert) |
|
105 | { |
||
106 | 1 | $validators = $this->getTypes(false); |
|
107 | 1 | if (!array_key_exists($this->type, $validators)) { |
|
108 | $this->addError('type', 'Please select correct type'); |
||
109 | return false; |
||
110 | } |
||
111 | |||
112 | 1 | $model = DynamicModel::validateData([ |
|
113 | 1 | 'value' => $this->value |
|
114 | ], [ |
||
115 | 1 | $validators[$this->type], |
|
116 | ]); |
||
117 | |||
118 | 1 | if ($model->hasErrors()) { |
|
119 | $this->addError('value', $model->getFirstError('value')); |
||
120 | return false; |
||
121 | } |
||
122 | |||
123 | 1 | if ($this->hasErrors()) { |
|
124 | return false; |
||
125 | } |
||
126 | |||
127 | 1 | return parent::beforeSave($insert); |
|
128 | } |
||
129 | |||
130 | /** |
||
131 | * @inheritdoc |
||
132 | */ |
||
133 | public function getSettings() |
||
141 | |||
142 | /** |
||
143 | * @inheritdoc |
||
144 | */ |
||
145 | 1 | public function setSetting($order_id, $key, $value, $type = null) |
|
146 | { |
||
147 | 1 | $model = static::findOne(['order_id' => $order_id, 'key' => $key]); |
|
148 | |||
149 | 1 | if ($model === null) { |
|
150 | 1 | $model = new static(); |
|
151 | } |
||
152 | 1 | $model->order_id = $order_id; |
|
153 | 1 | $model->key = $key; |
|
154 | 1 | $model->value = strval($value); |
|
155 | |||
156 | 1 | if ($type !== null) { |
|
157 | 1 | $model->type = $type; |
|
158 | } else { |
||
159 | $t = gettype($value); |
||
160 | if ($t == 'string') { |
||
161 | $error = false; |
||
162 | try { |
||
163 | Json::decode($value); |
||
164 | } catch (InvalidParamException $e) { |
||
165 | $error = true; |
||
166 | } |
||
167 | if (!$error) { |
||
168 | $t = 'object'; |
||
169 | } |
||
170 | } |
||
171 | $model->type = $t; |
||
172 | } |
||
173 | |||
174 | 1 | return $model->save(); |
|
175 | } |
||
176 | |||
177 | /** |
||
178 | * @inheritdoc |
||
179 | */ |
||
180 | 1 | public function deleteSetting($order_id, $key) |
|
189 | |||
190 | /** |
||
191 | * @param $key |
||
192 | * @param $order_id |
||
193 | * @return array|null|ActiveRecord |
||
194 | */ |
||
195 | public function findSetting($key, $order_id) |
||
199 | } |
||
200 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.