jeremykenedy /
laravel-blocker
| 1 | <?php |
||
| 2 | |||
| 3 | namespace jeremykenedy\LaravelBlocker\App\Rules; |
||
| 4 | |||
| 5 | use Illuminate\Contracts\Validation\Rule; |
||
| 6 | use jeremykenedy\LaravelBlocker\App\Models\BlockedType; |
||
| 7 | |||
| 8 | class UniqueBlockerItemValueEmail implements Rule |
||
| 9 | { |
||
| 10 | private $typeId; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Create a new rule instance. |
||
| 14 | * |
||
| 15 | * @return void |
||
| 16 | */ |
||
| 17 | public function __construct($typeId) |
||
| 18 | { |
||
| 19 | $this->typeId = $typeId; |
||
| 20 | } |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Determine if the validation rule passes. |
||
| 24 | * |
||
| 25 | * @param string $attribute |
||
| 26 | * @param mixed $value |
||
| 27 | * |
||
| 28 | * @return bool |
||
| 29 | */ |
||
| 30 | public function passes($attribute, $value) |
||
| 31 | { |
||
| 32 | if ($this->typeId) { |
||
| 33 | $type = BlockedType::find($this->typeId); |
||
| 34 | |||
| 35 | if ($type->slug == 'email' || $type->slug == 'user') { |
||
| 36 | $check = $this->checkEmail($value); |
||
| 37 | |||
| 38 | if ($check) { |
||
| 39 | return $value; |
||
| 40 | } |
||
| 41 | |||
| 42 | return false; |
||
| 43 | } |
||
| 44 | } |
||
| 45 | |||
| 46 | return true; |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Check if value is proper formed email. |
||
| 51 | * |
||
| 52 | * @param string $email The email |
||
| 53 | * |
||
| 54 | * @return bool |
||
| 55 | */ |
||
| 56 | public function checkEmail($email) |
||
| 57 | { |
||
| 58 | $find1 = strpos($email, '@'); |
||
| 59 | $find2 = strpos($email, '.'); |
||
| 60 | |||
| 61 | return $find1 !== false && $find2 !== false && $find2 > $find1 ? true : false; |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Get the validation error message. |
||
| 66 | * |
||
| 67 | * @return string |
||
| 68 | */ |
||
| 69 | public function message() |
||
| 70 | { |
||
| 71 | return trans('laravelblocker::laravelblocker.validation.email'); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 72 | } |
||
| 73 | } |
||
| 74 |