strictRfcCompliant = true; } else { $this->rfcCompliant = true; } return $this; } /** * Ensure that the email is a strictly enforced RFC compliant email address. * * @return $this */ public function strict() { return $this->rfcCompliant(true); } /** * Ensure that the email address has a valid MX record. * * Requires the PHP intl extension. * * @return $this */ public function validateMxRecord() { $this->validateMxRecord = true; return $this; } /** * Ensure that the email address is not attempting to spoof another email address using invalid unicode characters. * * @return $this */ public function preventSpoofing() { $this->preventSpoofing = true; return $this; } /** * Ensure the email address is valid using PHP's native email validation functions. * * @param bool $allowUnicode * @return $this */ public function withNativeValidation(bool $allowUnicode = false) { if ($allowUnicode) { $this->nativeValidationWithUnicodeAllowed = true; } else { $this->nativeValidation = true; } return $this; } /** * Specify additional validation rules that should be merged with the default rules during validation. * * @param string|array $rules * @return $this */ public function rules($rules) { $this->customRules = array_merge($this->customRules, Arr::wrap($rules)); return $this; } /** * Determine if the validation rule passes. * * @param string $attribute * @param mixed $value * @return bool */ public function passes($attribute, $value) { $this->messages = []; if (! is_string($value) && ! (is_object($value) && method_exists($value, '__toString'))) { return false; } $validator = Validator::make( $this->data, [$attribute => $this->buildValidationRules()], $this->validator->customMessages, $this->validator->customAttributes ); if ($validator->fails()) { $this->messages = array_merge($this->messages, $validator->messages()->all()); return false; } return true; } /** * Build the array of underlying validation rules based on the current state. * * @return array */ protected function buildValidationRules() { $rules = []; if ($this->rfcCompliant) { $rules[] = 'rfc'; } if ($this->strictRfcCompliant) { $rules[] = 'strict'; } if ($this->validateMxRecord) { $rules[] = 'dns'; } if ($this->preventSpoofing) { $rules[] = 'spoof'; } if ($this->nativeValidation) { $rules[] = 'filter'; } if ($this->nativeValidationWithUnicodeAllowed) { $rules[] = 'filter_unicode'; } if ($rules) { $rules = ['email:'.implode(',', $rules)]; } else { $rules = ['email']; } return array_merge(array_filter($rules), $this->customRules); } /** * Get the validation error message. * * @return array */ public function message() { return $this->messages; } /** * Set the current validator. * * @param \Illuminate\Contracts\Validation\Validator $validator * @return $this */ public function setValidator($validator) { $this->validator = $validator; return $this; } /** * Set the current data under validation. * * @param array $data * @return $this */ public function setData($data) { $this->data = $data; return $this; } }