toArray(); } if (is_array($options)) { if (array_key_exists('phoneOptions', $options)) { $this->_initPhoneValidator($options['phoneOptions']); } if (array_key_exists('emailOptions', $options)) { $this->_initEmailValidator($options['emailOptions']); } } } /** * @param array $options OPTIONAL * @return Zend_Validate_EmailAddress */ protected function _initEmailValidator($options = array()) { if (null === $this->_emailValidator) { $this->_emailValidator = new Zend_Validate_EmailAddress($options); } elseif(!empty($options)) { $this->_emailValidator->setOptions($options); } return $this->_emailValidator; } /** * @param array $options OPTIONAL * @return Zend_Validate_Regex */ protected function _initPhoneValidator($options = array()) { if (null === $this->_phoneValidator) { $pattern = (array_key_exists('pattern', $options)) ? $options['pattern'] : $this->_phonePattern; $this->_phoneValidator = new Zend_Validate_Regex($pattern); $this->_phoneValidator->setMessage($this->_phoneMessage); } elseif(!empty($options)) { array_key_exists('pattern', $options) && $this->_phoneValidator->setPattern($options['pattern']); } array_key_exists('message', $options) && $this->_phoneValidator->setMessage($options['message']); array_key_exists('messages', $options) && $this->_phoneValidator->setMessages($options['messages']); return $this->_phoneValidator; } protected function _isValidEmail($value) { $validator = $this->_initEmailValidator(); if (false === ($result = $validator->isValid($value))) { $this->_messages = array_merge($this->_messages, $validator->getMessages()); } return $result; } protected function _isValidPhone($value) { $validator = $this->_initPhoneValidator(); if (false === ($result = $validator->isValid($value))) { $this->_messages = array_merge($this->_messages, $validator->getMessages()); } return $result; } public function setPhoneOptions($options) { $this->_initPhoneValidator($options); return $this; } public function setEmailOptions($options) { $this->_initEmailValidator($options); return $this; } public function setPhoneMessage($messageString, $messageKey = null) { $validator = $this->_initPhoneValidator(); $validator->setMessage($messageString, $messageKey); $this->_phoneMessage = $messageString; return $this; } public function setPhonePattern($pattern) { $validator = $this->_initPhoneValidator(); $validator->setPattern($pattern); $this->_phonePattern = $pattern; return $this; } public function setEmailMessage($messageString, $messageKey = null) { $validator = $this->_initEmailValidator(); $validator->setMessage($messageString, $messageKey); return $this; } /** * Use 'setPhoneMessage' or 'setEmailMessage' instead * * @throws Zend_Validate_Exception * @deprecated * @param $messageString * @param null $messageKey */ public function setMessage($messageString, $messageKey = null) { throw new Zend_Validate_Exception('Can not set message. Use "setPhoneMessage" or "setEmailMessage" instead'); } /** * Returns true if $value is a valid email address or a valid phone number * * @param string $value * @return bool */ public function isValid($value) { if (false !== strpos($value, '@')) { return $this->_isValidEmail($value); } else { return $this->_isValidPhone($value); } } }