2021-03-26 16:10:56 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Rules;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Contracts\Validation\Rule;
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
|
|
|
|
|
|
class Adult implements Rule
|
|
|
|
|
{
|
2022-09-03 16:56:16 +02:00
|
|
|
/**
|
|
|
|
|
* Error message
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2021-03-26 16:10:56 +01:00
|
|
|
private $error_messages = "la date de naissance est incorrecte";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a new rule instance.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determine if the validation rule passes.
|
|
|
|
|
*
|
2022-09-03 16:56:16 +02:00
|
|
|
* @param mixed $attribute
|
|
|
|
|
* @param mixed $value
|
2022-08-25 12:26:43 +02:00
|
|
|
* @return boolean
|
2022-09-03 16:56:16 +02:00
|
|
|
*
|
|
|
|
|
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceBeforeLastUsed
|
2021-03-26 16:10:56 +01:00
|
|
|
*/
|
|
|
|
|
public function passes($attribute, $value)
|
|
|
|
|
{
|
2022-09-03 16:56:16 +02:00
|
|
|
//@phpcs:enable
|
2021-03-26 16:10:56 +01:00
|
|
|
$MJ = Carbon::now()->subYears(18);
|
|
|
|
|
|
|
|
|
|
if ($this->isSqlDate($value)) {
|
|
|
|
|
$d = Carbon::createFromFormat('Y-m-d', $value);
|
|
|
|
|
if (!$d->lessThanOrEqualTo($MJ)) {
|
|
|
|
|
$this->error_messages = __("Vous devez être majeur pour vous inscrire.");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} elseif ($this->isFormDate($value)) {
|
|
|
|
|
$d = Carbon::createFromFormat('d/m/Y', $value);
|
|
|
|
|
if (!$d->lessThanOrEqualTo($MJ)) {
|
|
|
|
|
$this->error_messages = __("Vous devez être majeur pour vous inscrire.");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$this->error_messages = __("Erreur sur le champs : date de naissance.");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-03 16:56:16 +02:00
|
|
|
// Pass.
|
2021-03-26 16:10:56 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the validation error message.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function message()
|
|
|
|
|
{
|
|
|
|
|
return $this->error_messages;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-03 16:56:16 +02:00
|
|
|
/**
|
|
|
|
|
* Check if provided string is formatted for mysql
|
|
|
|
|
*
|
|
|
|
|
* @param string $date
|
|
|
|
|
* @return boolean
|
|
|
|
|
*/
|
|
|
|
|
private function isSqlDate(string $date)
|
2021-03-26 16:10:56 +01:00
|
|
|
{
|
|
|
|
|
if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/", $date)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-03 16:56:16 +02:00
|
|
|
/**
|
|
|
|
|
* Check if provided string is formatted DD/MM/AAAA
|
|
|
|
|
*
|
|
|
|
|
* @param string $date
|
|
|
|
|
* @return boolean
|
|
|
|
|
*/
|
|
|
|
|
private function isFormDate(string $date)
|
2021-03-26 16:10:56 +01:00
|
|
|
{
|
|
|
|
|
if (preg_match("/^(0[1-9]|[1-2][0-9]|3[0-1])\/(0[1-9]|1[0-2])\/[0-9]{4}$/", $date)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|