1 <?php
2 3 4 5 6 7 8 9 10
11
12 MainFactory::load_class('CustomerInputValidatorInterface');
13
14 require_once DIR_FS_INC . 'xtc_validate_email.inc.php';
15
16 17 18 19 20 21 22 23 24 25
26 class CustomerInputValidator implements CustomerInputValidatorInterface
27 {
28 29 30 31
32 protected $customerCountryRepository;
33
34 35 36 37
38 protected $customerCountryZoneRepository;
39
40 41 42 43
44 protected $customerService;
45
46 47 48 49
50 protected $countryService;
51
52 53 54 55
56 protected $settings;
57
58 59 60 61
62 protected $vatNumberValidator;
63
64 65 66 67
68 protected $errorMessageCollection;
69
70 71 72 73
74 protected $errorStatus = false;
75
76
77 78 79 80 81 82 83 84 85 86
87 public function __construct(CustomerServiceInterface $customerService,
88 CountryServiceInterface $countryService,
89 CustomerInputValidatorSettingsInterface $customerInputValidatorSettings,
90 CustomerCountryRepositoryInterface $customerCountryRepository,
91 CustomerCountryZoneRepositoryInterface $customerCountryZoneRepository,
92 VatNumberValidatorInterface $vatNumberValidator)
93 {
94 $this->customerService = $customerService;
95 $this->countryService = $countryService;
96 $this->settings = $customerInputValidatorSettings;
97 $this->customerCountryRepository = $customerCountryRepository;
98 $this->customerCountryZoneRepository = $customerCountryZoneRepository;
99 $this->vatNumberValidator = $vatNumberValidator;
100
101 $this->errorMessageCollection = MainFactory::create('EditableKeyValueCollection', array());
102 }
103
104
105 106 107 108 109 110 111
112 public function validateGender($p_gender)
113 {
114 if(!$this->settings->getDisplayGender())
115 {
116 return true;
117 }
118 if((string)$p_gender != 'm' && (string)$p_gender != 'f')
119 {
120 $this->errorStatus = true;
121 $this->errorMessageCollection->setValue('error_gender', $this->settings->getGenderErrorMessage());
122
123 return false;
124 }
125
126 return true;
127 }
128
129
130 131 132 133 134 135 136
137 public function validateFirstname($p_firstname)
138 {
139 if(strlen_wrapper(trim((string)$p_firstname)) > $this->settings->getFirstnameMaxLength())
140 {
141 $this->errorStatus = true;
142 $this->errorMessageCollection->setValue('error_first_name', $this->settings->getInvalidInputErrorMessage());
143
144 return false;
145 }
146
147 if(strlen_wrapper((string)$p_firstname) < $this->settings->getFirstnameMinLength())
148 {
149 $this->errorStatus = true;
150 $this->errorMessageCollection->setValue('error_first_name', $this->settings->getFirstnameErrorMessage());
151
152 return false;
153 }
154
155 return true;
156 }
157
158
159 160 161 162 163 164 165
166 public function validateLastname($p_lastname)
167 {
168 if(strlen_wrapper(trim((string)$p_lastname)) > $this->settings->getLastnameMaxLength())
169 {
170 $this->errorStatus = true;
171 $this->errorMessageCollection->setValue('error_last_name', $this->settings->getInvalidInputErrorMessage());
172
173 return false;
174 }
175
176 if(strlen_wrapper((string)$p_lastname) < $this->settings->getLastnameMinLength())
177 {
178 $this->errorStatus = true;
179 $this->errorMessageCollection->setValue('error_last_name', $this->settings->getLastnameErrorMessage());
180
181 return false;
182 }
183
184 return true;
185 }
186
187
188 189 190 191 192 193 194 195
196 public function validateDateOfBirth($p_dateOfBirth)
197 {
198
199 if(!$this->settings->getDisplayDateOfBirth())
200 {
201 return true;
202 }
203 $dateOfBirth = (string)$p_dateOfBirth;
204 $minLength = $this->settings->getDateOfBirthMinLength();
205
206 if($minLength > 0 || ($dateOfBirth != '' && $minLength === 0))
207 {
208 if(!preg_match('/^[0-9]{2}[\.\/]{1}[0-9]{2}[\.\/]{1}[0-9]{4}$/', $dateOfBirth)
209 || checkdate(substr(xtc_date_raw($dateOfBirth), 4, 2), substr(xtc_date_raw($dateOfBirth), 6, 2),
210 substr(xtc_date_raw($dateOfBirth), 0, 4)) == false
211 )
212 {
213 $this->errorStatus = true;
214 $this->errorMessageCollection->setValue('error_birth_day',
215 $this->settings->getDateOfBirthErrorMessage());
216
217 return false;
218 }
219 }
220
221 return true;
222 }
223
224
225 226 227 228 229 230 231
232 public function validateCompany($p_company)
233 {
234 if(!$this->settings->getDisplayCompany())
235 {
236 return true;
237 }
238 $company = (string)$p_company;
239
240 if(strlen_wrapper(trim($company)) > $this->settings->getCompanyMaxLength())
241 {
242 $this->errorStatus = true;
243 $this->errorMessageCollection->setValue('error_company', $this->settings->getInvalidInputErrorMessage());
244
245 return false;
246 }
247
248 if(strlen_wrapper($company) > 0 && strlen_wrapper($company) < $this->settings->getCompanyMinLength())
249 {
250 $this->errorStatus = true;
251 $this->errorMessageCollection->setValue('error_company', $this->settings->getCompanyErrorMessage());
252
253 return false;
254 }
255
256 return true;
257 }
258
259
260 261 262 263 264 265 266 267 268
269 public function validateVatNumber($p_vatNumber, $p_countryId, $p_isGuest)
270 {
271 if(strlen_wrapper(trim((string)$p_vatNumber)) > $this->settings->getVatNumberMaxLength())
272 {
273 $this->errorStatus = true;
274 $this->errorMessageCollection->setValue('error_vat', $this->settings->getInvalidInputErrorMessage());
275
276 return false;
277 }
278
279 if($this->vatNumberValidator->getErrorStatus($p_vatNumber, $p_countryId, $p_isGuest))
280 {
281 $this->errorStatus = true;
282 $this->errorMessageCollection->setValue('error_vat', $this->settings->getVatNumberErrorMessage());
283
284 return false;
285 }
286
287 return true;
288 }
289
290
291 292 293 294 295 296 297 298 299
300 public function validateEmailAndConfirmation($p_email, $p_emailConfirmation)
301 {
302 if($p_email != $p_emailConfirmation)
303 {
304 $this->errorStatus = true;
305 $this->errorMessageCollection->setValue('error_mail', $this->settings->getEmailConfirmationErrorMessage());
306
307 return false;
308 }
309
310 if(!$this->validateEmail($p_email))
311 {
312 return false;
313 }
314
315 if(!$this->validateEmailExists($p_email))
316 {
317 return false;
318 }
319
320 return true;
321 }
322
323
324 325 326 327 328 329 330
331 public function validateEmail($p_email)
332 {
333 if(strlen_wrapper($p_email) < $this->settings->getEmailMinLength())
334 {
335 $this->errorStatus = true;
336 $this->errorMessageCollection->setValue('error_mail', $this->settings->getEmailErrorMessage());
337
338 return false;
339 }
340 elseif(xtc_validate_email($p_email) == false)
341 {
342 $this->errorStatus = true;
343 $this->errorMessageCollection->setValue('error_mail', $this->settings->getEmailAddressCheckErrorMessage());
344
345 return false;
346 }
347 elseif(!filter_var($p_email, FILTER_VALIDATE_EMAIL))
348 {
349
350
351 $this->errorStatus = true;
352 $this->errorMessageCollection->setValue('error_mail', $this->settings->getEmailAddressCheckErrorMessage());
353
354 return false;
355
356 }
357
358 return true;
359 }
360
361
362 363 364 365 366 367 368 369
370 public function validateEmailExists($p_email, CustomerInterface $customer = null)
371 {
372 if($this->customerService->registreeEmailExists(MainFactory::create('CustomerEmail', $p_email))
373 && ($customer === null
374 || $customer->getEmail() != $p_email)
375 )
376 {
377 $this->errorStatus = true;
378 $this->errorMessageCollection->setValue('error_mail', $this->settings->getEmailExistsErrorMessage());
379
380 return false;
381 }
382
383 return true;
384 }
385
386
387 388 389 390 391 392 393
394 public function validateStreet($p_street)
395 {
396 if(strlen_wrapper(trim((string)$p_street)) > $this->settings->getStreetMaxLength())
397 {
398 $this->errorStatus = true;
399 $this->errorMessageCollection->setValue('error_street', $this->settings->getInvalidInputErrorMessage());
400
401 return false;
402 }
403
404 if(strlen_wrapper((string)$p_street) < $this->settings->getStreetMinLength())
405 {
406 $this->errorStatus = true;
407 $this->errorMessageCollection->setValue('error_street', $this->settings->getStreetErrorMessage());
408
409 return false;
410 }
411
412 return true;
413 }
414
415
416 417 418 419 420 421 422
423 public function validatePostcode($p_postcode)
424 {
425 if(strlen_wrapper(trim((string)$p_postcode)) > $this->settings->getPostcodeMaxLength())
426 {
427 $this->errorStatus = true;
428 $this->errorMessageCollection->setValue('error_post_code', $this->settings->getInvalidInputErrorMessage());
429
430 return false;
431 }
432
433 if(strlen_wrapper((string)$p_postcode) < $this->settings->getPostcodeMinLength())
434 {
435 $this->errorStatus = true;
436 $this->errorMessageCollection->setValue('error_post_code', $this->settings->getPostcodeErrorMessage());
437
438 return false;
439 }
440
441 return true;
442 }
443
444
445 446 447 448 449 450 451
452 public function validateCity($p_city)
453 {
454 if(strlen_wrapper(trim((string)$p_city)) > $this->settings->getCityMaxLength())
455 {
456 $this->errorStatus = true;
457 $this->errorMessageCollection->setValue('error_city', $this->settings->getInvalidInputErrorMessage());
458
459 return false;
460 }
461
462 if(strlen_wrapper((string)$p_city) < $this->settings->getCityMinLength())
463 {
464 $this->errorStatus = true;
465 $this->errorMessageCollection->setValue('error_city', $this->settings->getCityErrorMessage());
466
467 return false;
468 }
469
470 return true;
471 }
472
473
474 475 476 477 478 479 480
481 public function validateCountry($p_countryId)
482 {
483 $country = $this->customerCountryRepository->findById(new IdType($p_countryId));
484
485 if($country === null || $country->getStatus() === false)
486 {
487 $this->errorStatus = true;
488 $this->errorMessageCollection->setValue('error_country', $this->settings->getCountryErrorMessage());
489
490 return false;
491 }
492
493 return true;
494 }
495
496
497 498 499 500 501 502 503 504 505 506 507 508
509 public function validateCountryZone($p_countryZone, $p_countryId)
510 {
511 if(!$this->settings->getDisplayCountryZone())
512 {
513 return true;
514 }
515
516 $country = $this->customerCountryRepository->findById(new IdType($p_countryId));
517
518 if($country !== null && $this->countryService->countryHasCountryZones($country))
519 {
520 if(is_numeric($p_countryZone))
521 {
522 $countryZone = $this->customerCountryZoneRepository->findById(new IdType($p_countryZone));
523 }
524 else
525 {
526 if(strlen_wrapper(trim((string)$p_countryZone)) > $this->settings->getCountryZoneMaxLength())
527 {
528 $this->errorStatus = true;
529 $this->errorMessageCollection->setValue('error_state',
530 $this->settings->getInvalidInputErrorMessage());
531
532 return false;
533 }
534
535 $countryZone = $this->customerCountryZoneRepository->findByNameAndCountry(MainFactory::create('CustomerCountryZoneName',
536 $p_countryZone),
537 $country);
538 }
539
540 if($countryZone === null
541 || $country->getStatus() === false
542 || !$this->countryService->countryZoneExistsInCountry($countryZone, $country)
543 )
544 {
545 $this->errorStatus = true;
546 $this->errorMessageCollection->setValue('error_state',
547 $this->settings->getCountryZoneSelectionErrorMessage());
548
549 return false;
550 }
551 }
552 elseif(is_numeric($p_countryZone)
553 || strlen_wrapper((string)$p_countryZone) < $this->settings->getCountryZoneMinLength()
554 )
555 {
556 $this->errorStatus = true;
557 $this->errorMessageCollection->setValue('error_state', $this->settings->getCountryZoneErrorMessage());
558
559 return false;
560 }
561
562 return true;
563 }
564
565
566 567 568 569 570 571 572
573 public function validateSuburb($p_suburb)
574 {
575 if(!$this->settings->getDisplaySuburb())
576 {
577 return true;
578 }
579
580 if(strlen_wrapper(trim((string)$p_suburb)) > $this->settings->getSuburbMaxLength())
581 {
582 $this->errorStatus = true;
583 $this->errorMessageCollection->setValue('error_suburb', $this->settings->getInvalidInputErrorMessage());
584
585 return false;
586 }
587
588 return true;
589 }
590
591
592 593 594 595 596 597 598
599 public function validateTelephoneNumber($p_telephoneNumber)
600 {
601 if(!$this->settings->getDisplayTelephone())
602 {
603 return true;
604 }
605
606 if(strlen_wrapper(trim((string)$p_telephoneNumber)) > $this->settings->getCallNumberMaxLength())
607 {
608 $this->errorStatus = true;
609 $this->errorMessageCollection->setValue('error_tel', $this->settings->getInvalidInputErrorMessage());
610
611 return false;
612 }
613
614 if(strlen_wrapper((string)$p_telephoneNumber) < $this->settings->getTelephoneNumberMinLength())
615 {
616 $this->errorStatus = true;
617 $this->errorMessageCollection->setValue('error_tel', $this->settings->getTelephoneNumberErrorMessage());
618
619 return false;
620 }
621
622 return true;
623 }
624
625
626 627 628 629 630 631 632
633 public function validateFaxNumber($p_faxNumber)
634 {
635 if(!$this->settings->getDisplayFax())
636 {
637 return true;
638 }
639
640 if(strlen_wrapper(trim((string)$p_faxNumber)) > $this->settings->getCallNumberMaxLength())
641 {
642 $this->errorStatus = true;
643 $this->errorMessageCollection->setValue('error_fax', $this->settings->getInvalidInputErrorMessage());
644
645 return false;
646 }
647
648 return true;
649 }
650
651
652 653 654 655 656 657 658 659
660 public function validatePassword($p_password, $p_passwordConfirmation)
661 {
662 if(strlen_wrapper($p_password) < $this->settings->getPasswordMinLength())
663 {
664 $this->errorStatus = true;
665 $this->errorMessageCollection->setValue('error_password', $this->settings->getPasswordErrorMessage());
666
667 return false;
668 }
669 elseif($p_password !== $p_passwordConfirmation)
670 {
671 $this->errorStatus = true;
672 $this->errorMessageCollection->setValue('error_password2',
673 $this->settings->getPasswordMismatchErrorMessage());
674
675 return false;
676 }
677
678 return true;
679 }
680
681
682 683 684 685 686
687 public function getErrorMessages()
688 {
689 return $this->errorMessageCollection->getArray();
690 }
691
692
693 694 695 696 697
698 public function getErrorMessageCollection()
699 {
700 return $this->errorMessageCollection;
701 }
702
703
704 705 706 707 708
709 public function getErrorStatus()
710 {
711 return $this->errorStatus;
712 }
713 }