MultipleEmails.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ iwkUqZz279agWWIoSKKVF96nvTiezA+FHbEEG/2IbDI=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2017 Online Ventures Software & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.10 [rev.7.10.01]
  11. */
  12. /**
  13. * multiple email addresses validator class
  14. */
  15. namespace Ppb\Validate;
  16. use Cube\Validate\Email;
  17. class MultipleEmails extends Email
  18. {
  19. const NOT_VALID = 1;
  20. const DUPLICATE = 2;
  21. protected $_messages = array(
  22. self::NOT_VALID => "'%s' must contain one or multiple valid email addresses.",
  23. self::DUPLICATE => "'%s': you cannot enter an email address more than once.",
  24. );
  25. /**
  26. *
  27. * emails separator
  28. *
  29. * @var string
  30. */
  31. protected $_separator = ',';
  32. /**
  33. *
  34. * set emails separator
  35. *
  36. * @param string $separator
  37. *
  38. * @return $this
  39. */
  40. public function setSeparator($separator)
  41. {
  42. $this->_separator = $separator;
  43. return $this;
  44. }
  45. /**
  46. *
  47. * get emails separator
  48. *
  49. * @return string
  50. */
  51. public function getSeparator()
  52. {
  53. return $this->_separator;
  54. }
  55. /**
  56. *
  57. * checks if the variable contains a valid email address
  58. *
  59. * @return bool return true if the validation is successful
  60. */
  61. public function isValid()
  62. {
  63. $separator = $this->getSeparator();
  64. $value = $this->getValue();
  65. $emails = array_map('trim', (array)explode($separator, $value));
  66. $duplicates = array_count_values($emails);
  67. foreach ($emails as $email) {
  68. parent::setValue(
  69. trim($email));
  70. if (!parent::isValid()) {
  71. $this->setMessage($this->_messages[self::NOT_VALID]);
  72. return false;
  73. }
  74. if ($duplicates[$email] > 1) {
  75. $this->setMessage($this->_messages[self::DUPLICATE]);
  76. return false;
  77. }
  78. }
  79. return true;
  80. }
  81. }