error_message = $message ?? trans('Vous devez ĂȘtre majeur.'); } /** * Determine if the validation rule passes. * * @param mixed $attribute * @param mixed $value * @return boolean * @throws \Exception If the attribute cannot be parsed. * @throws \RuntimeException Is already catched. */ public function passes($attribute, $value) { try { //@phpcs:enable $MJ = Carbon::now()->subYears(18); if ($this->isIsoFormat($value)) { $d = Carbon::parse($value); } if ($this->isSqlDate($value)) { $d = Carbon::createFromFormat('Y-m-d', $value); } if ($this->isFormDate($value)) { $d = Carbon::createFromFormat('d/m/Y', $value); } // * Could not parse date. if (!$d) { throw new \Exception("Could not parse date {$value} for attribute '{$attribute}'."); } if (!$d->lessThanOrEqualTo($MJ)) { throw new \RuntimeException('Users birthdate does not indicate he is an adult'); } } catch (\RuntimeException $e) { $this->error_message = $this->error_message; return false; }//end try return true; } /** * Get the validation error message. * * @return string */ public function message() { return $this->error_message; } /** * Check if provided string is formatted for mysql * * @param string $date * @return boolean */ private function isSqlDate(string $date) { 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; } /** * Check if provided string is formatted DD/MM/AAAA * * @param string $date * @return boolean */ private function isFormDate(string $date) { 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; } /** * Check if provided string is iso format * * * https://www.w3.org/TR/NOTE-datetime * * @param string $date * @return boolean */ private function isIsoFormat(string $date) { // phpcs:ignore Generic.Files.LineLength.TooLong if (preg_match("/^(\d{4})(?:-?W(\d+)(?:-?(\d+)D?)?|(?:-(\d+))?-(\d+))(?:[T ](\d+):(\d+)(?::(\d+)(?:\.(\d+))?)?)?(?:Z(-?\d*))?$/", $date)) { return true; } return false; } }