14
14
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
15
15
*/
16
16
17
+ use Symfony \Component \Validator \Constraints as Assert ;
18
+ use Symfony \Component \Validator \Validation ;
19
+
17
20
/**
18
21
* Template model
19
22
*
@@ -79,38 +82,59 @@ protected function _construct()
79
82
*/
80
83
public function validate ()
81
84
{
82
- $ validators = [
83
- 'template_code ' => [Zend_Filter_Input::ALLOW_EMPTY => false ],
84
- 'template_type ' => 'Int ' ,
85
- 'template_sender_email ' => 'EmailAddress ' ,
86
- 'template_sender_name ' => [Zend_Filter_Input::ALLOW_EMPTY => false ],
87
- ];
88
- $ data = [];
89
- foreach (array_keys ($ validators ) as $ validateField ) {
90
- $ data [$ validateField ] = $ this ->getDataUsingMethod ($ validateField );
91
- }
92
-
93
- $ validateInput = new Zend_Filter_Input ([], $ validators , $ data );
94
- if (!$ validateInput ->isValid ()) {
95
- $ errorMessages = [];
96
- foreach ($ validateInput ->getMessages () as $ messages ) {
97
- if (is_array ($ messages )) {
98
- foreach ($ messages as $ message ) {
99
- $ errorMessages [] = $ message ;
100
- }
101
- } else {
102
- $ errorMessages [] = $ messages ;
103
- }
85
+ $ validator = Validation::createValidator ();
86
+ $ violations = [];
87
+ $ errors = new ArrayObject ();
88
+
89
+ $ violations [] = $ validator ->validate ($ this ->getDataUsingMethod ('template_code ' ), [
90
+ new Assert \NotBlank ([
91
+ 'message ' => 'You must give a non-empty value for field \'template_code \'' ,
92
+ ]),
93
+ ]);
94
+
95
+ $ message = 'You must give a non-empty value for field \'template_type \'' ;
96
+ $ violations [] = $ validator ->validate ($ this ->getDataUsingMethod ('template_type ' ), [
97
+ new Assert \NotBlank ([
98
+ 'message ' => $ message ,
99
+ ]),
100
+ new Assert \Type ([
101
+ 'type ' => 'int ' ,
102
+ 'message ' => $ message ,
103
+ ]),
104
+ ]);
105
+
106
+ $ message = '\'invalid-email \' is not a valid email address in the basic format local-part@hostname ' ;
107
+ $ violations [] = $ validator ->validate ($ this ->getDataUsingMethod ('template_sender_email ' ), [
108
+ new Assert \NotBlank ([
109
+ 'message ' => $ message ,
110
+ ]),
111
+ new Assert \Email ([
112
+ 'message ' => $ message ,
113
+ ]),
114
+ ]);
115
+
116
+ $ violations [] = $ validator ->validate ($ this ->getDataUsingMethod ('template_sender_name ' ), [
117
+ new Assert \NotBlank ([
118
+ 'message ' => 'You must give a non-empty value for field \'template_sender_name \'' ,
119
+ ]),
120
+ ]);
121
+
122
+ foreach ($ violations as $ violation ) {
123
+ foreach ($ violation as $ error ) {
124
+ $ errors ->append ($ error ->getMessage ());
104
125
}
126
+ }
105
127
106
- Mage::throwException (implode ("\n" , $ errorMessages ));
128
+ if (count ($ errors ) !== 0 ) {
129
+ Mage::throwException (implode ("\n" , iterator_to_array ($ errors )));
107
130
}
108
131
}
109
132
110
133
/**
111
134
* Processing object before save data
112
135
*
113
136
* @inheritDoc
137
+ * @throws Mage_Core_Exception
114
138
*/
115
139
protected function _beforeSave ()
116
140
{
@@ -147,7 +171,7 @@ public function isValidForSend()
147
171
/**
148
172
* Getter for template type
149
173
*
150
- * @return int|string
174
+ * @return int
151
175
*/
152
176
public function getType ()
153
177
{
@@ -280,7 +304,8 @@ public function getMail()
280
304
* @param array $variables template variables
281
305
* @param string|null $name receiver name (if subscriber model not specified)
282
306
* @param Mage_Newsletter_Model_Queue|null $queue queue model, used for problems reporting.
283
- * @return bool
307
+ * @return bool
308
+ * @throws Exception|Throwable
284
309
* @deprecated since 1.4.0.1
285
310
**/
286
311
public function send ($ subscriber , array $ variables = [], $ name = null , ?Mage_Newsletter_Model_Queue $ queue = null )
@@ -346,23 +371,23 @@ public function send($subscriber, array $variables = [], $name = null, ?Mage_New
346
371
if (!is_null ($ queue )) {
347
372
$ subscriber ->received ($ queue );
348
373
}
349
- } catch (Exception $ e ) {
374
+ } catch (Exception $ exception ) {
350
375
if ($ subscriber instanceof Mage_Newsletter_Model_Subscriber) {
351
376
// If letter sent for subscriber, we create a problem report entry
352
377
$ problem = Mage::getModel ('newsletter/problem ' );
353
378
$ problem ->addSubscriberData ($ subscriber );
354
379
if (!is_null ($ queue )) {
355
380
$ problem ->addQueueData ($ queue );
356
381
}
357
- $ problem ->addErrorData ($ e );
382
+ $ problem ->addErrorData ($ exception );
358
383
$ problem ->save ();
359
384
360
385
if (!is_null ($ queue )) {
361
386
$ subscriber ->received ($ queue );
362
387
}
363
388
} else {
364
389
// Otherwise throw error to upper level
365
- throw $ e ;
390
+ throw $ exception ;
366
391
}
367
392
return false ;
368
393
}
@@ -374,6 +399,7 @@ public function send($subscriber, array $variables = [], $name = null, ?Mage_New
374
399
* Prepare Process (with save)
375
400
*
376
401
* @return $this
402
+ * @throws Throwable
377
403
* @deprecated since 1.4.0.1
378
404
*/
379
405
public function preprocess ()
@@ -388,6 +414,7 @@ public function preprocess()
388
414
* Retrieve processed template subject
389
415
*
390
416
* @return string
417
+ * @throws Exception
391
418
*/
392
419
public function getProcessedTemplateSubject (array $ variables )
393
420
{
0 commit comments