@@ -73,32 +73,6 @@ public function init()
73
73
$ this ->prepareAttributes ();
74
74
}
75
75
76
- public function events ()
77
- {
78
- $ events = [];
79
- if ($ this ->performValidation ) {
80
- $ events [BaseActiveRecord::EVENT_BEFORE_VALIDATE ] = 'onBeforeValidate ' ;
81
- }
82
- return $ events ;
83
- }
84
-
85
- /**
86
- * Performs validation for all the attributes
87
- * @param Event $event
88
- */
89
- public function onBeforeValidate ($ event )
90
- {
91
- foreach ($ this ->attributeValues as $ targetAttribute => $ value ) {
92
- if ($ value instanceof DateTimeAttribute) {
93
- $ validator = \Yii::createObject ([
94
- 'class ' => DateValidator::className (),
95
- 'format ' => self ::normalizeIcuFormat ($ value ->targetFormat , $ this ->formatter )[1 ],
96
- ]);
97
- $ validator ->validateAttribute ($ this ->owner , $ targetAttribute );
98
- }
99
- }
100
- }
101
-
102
76
protected function prepareAttributes ()
103
77
{
104
78
foreach ($ this ->attributes as $ key => $ value ) {
@@ -132,61 +106,30 @@ protected function processTemplate($originalAttribute)
132
106
]);
133
107
}
134
108
135
- public function canGetProperty ($ name , $ checkVars = true )
136
- {
137
- if ($ this ->hasAttributeValue ($ name ))
138
- return true ;
139
- else
140
- return parent ::canGetProperty ($ name , $ checkVars );
141
- }
142
-
143
- protected function hasAttributeValue ($ name )
144
- {
145
- return isset ($ this ->attributeValues [$ name ]);
146
- }
147
-
148
- public function canSetProperty ($ name , $ checkVars = true )
149
- {
150
- if ($ this ->hasAttributeValue ($ name ))
151
- return true ;
152
- else
153
- return parent ::canSetProperty ($ name , $ checkVars );
154
- }
155
-
156
- public function __get ($ name )
157
- {
158
- if ($ this ->hasAttributeValue ($ name ))
159
- return $ this ->getAttributeValue ($ name );
160
- return parent ::__get ($ name );
161
- }
162
-
163
- public function __set ($ name , $ value )
164
- {
165
- if ($ this ->hasAttributeValue ($ name )) {
166
- $ this ->setAttributeValue ($ name , $ value );
167
- return ;
168
- }
169
- parent ::__set ($ name , $ value );
170
- }
171
-
172
- protected function getAttributeValue ($ name )
109
+ public function events ()
173
110
{
174
- if (is_array ($ this ->attributeValues [$ name ])) {
175
- $ this ->attributeValues [$ name ] = \Yii::createObject ($ this ->attributeValues [$ name ]);
111
+ $ events = [];
112
+ if ($ this ->performValidation ) {
113
+ $ events [BaseActiveRecord::EVENT_BEFORE_VALIDATE ] = 'onBeforeValidate ' ;
176
114
}
177
- return $ this -> attributeValues [ $ name ] ;
115
+ return $ events ;
178
116
}
179
117
180
- protected function setAttributeValue ($ name , $ value )
118
+ /**
119
+ * Performs validation for all the attributes
120
+ * @param Event $event
121
+ */
122
+ public function onBeforeValidate ($ event )
181
123
{
182
- if (is_array ($ this ->attributeValues [$ name ])) {
183
- $ this ->attributeValues [$ name ] = \Yii::createObject ($ this ->attributeValues [$ name ]);
124
+ foreach ($ this ->attributeValues as $ targetAttribute => $ value ) {
125
+ if ($ value instanceof DateTimeAttribute) {
126
+ $ validator = \Yii::createObject ([
127
+ 'class ' => DateValidator::className (),
128
+ 'format ' => self ::normalizeIcuFormat ($ value ->targetFormat , $ this ->formatter )[1 ],
129
+ ]);
130
+ $ validator ->validateAttribute ($ this ->owner , $ targetAttribute );
131
+ }
184
132
}
185
-
186
- if ($ value instanceof DateTimeAttribute)
187
- $ this ->attributeValues [$ name ] = $ value ;
188
- else
189
- $ this ->attributeValues [$ name ]->value = $ value ;
190
133
}
191
134
192
135
/**
@@ -213,4 +156,54 @@ public static function normalizeIcuFormat($format, $formatter)
213
156
}
214
157
return $ format ;
215
158
}
159
+
160
+ public function canGetProperty ($ name , $ checkVars = true )
161
+ {
162
+ if ($ this ->hasAttribute ($ name )) {
163
+ return true ;
164
+ }
165
+
166
+ return parent ::canGetProperty ($ name , $ checkVars );
167
+ }
168
+
169
+ public function hasAttribute ($ name )
170
+ {
171
+ return isset ($ this ->attributeValues [$ name ]);
172
+ }
173
+
174
+ public function canSetProperty ($ name , $ checkVars = true )
175
+ {
176
+ if ($ this ->hasAttribute ($ name )) {
177
+ return true ;
178
+ }
179
+
180
+ return parent ::canSetProperty ($ name , $ checkVars );
181
+ }
182
+
183
+ public function __get ($ name )
184
+ {
185
+ if ($ this ->hasAttribute ($ name )) {
186
+ return $ this ->getAttribute ($ name )->getValue ();
187
+ }
188
+
189
+ return parent ::__get ($ name );
190
+ }
191
+
192
+ public function __set ($ name , $ value )
193
+ {
194
+ if ($ this ->hasAttribute ($ name )) {
195
+ $ this ->getAttribute ($ name )->setValue ($ value );
196
+ return ;
197
+ }
198
+
199
+ parent ::__set ($ name , $ value );
200
+ }
201
+
202
+ public function getAttribute ($ name )
203
+ {
204
+ if (is_array ($ this ->attributeValues [$ name ])) {
205
+ $ this ->attributeValues [$ name ] = \Yii::createObject ($ this ->attributeValues [$ name ]);
206
+ }
207
+ return $ this ->attributeValues [$ name ];
208
+ }
216
209
}
0 commit comments