opnform-host-nginx/api/app/Rules/OneEmailPerLine.php

59 lines
1.2 KiB
PHP
Raw Normal View History

2022-09-20 21:59:52 +02:00
<?php
namespace App\Rules;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
2022-09-20 21:59:52 +02:00
class OneEmailPerLine implements ValidationRule
2022-09-20 21:59:52 +02:00
{
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
2024-02-23 11:54:12 +01:00
if ($value === null || empty(trim($value))) {
return true;
}
2022-09-20 21:59:52 +02:00
foreach (preg_split("/\r\n|\n|\r/", $value) as $email) {
$email = trim($email);
2024-02-23 11:54:12 +01:00
if (! filter_var($email, FILTER_VALIDATE_EMAIL)) {
2022-09-20 21:59:52 +02:00
return false;
}
}
2024-02-23 11:54:12 +01:00
2022-09-20 21:59:52 +02:00
return true;
}
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if(!$this->passes($attribute, $value)) {
$fail($this->message());
}
}
2022-09-20 21:59:52 +02:00
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'You need one valid email per line.';
}
}