diff --git a/src/Laravel/JalaliValidator.php b/src/Laravel/JalaliValidator.php index 92ed578..04b41e5 100644 --- a/src/Laravel/JalaliValidator.php +++ b/src/Laravel/JalaliValidator.php @@ -7,404 +7,274 @@ class JalaliValidator { + protected const DEFAULT_DATE_FORMAT = 'Y/m/d'; + protected const DEFAULT_DATETIME_FORMAT = 'Y/m/d H:i:s'; + /** - * Determines if an input is a valid Jalali date with the specified format - * @param string $attribute - * @param string $value - * @param array $parameters - * @return bool + * Validate Jalali date with specified format */ - public function validateDate($attribute, $value, $parameters) + public function validateDate(string $attribute, $value, array $parameters): bool { - if (!is_string($value)) { - return false; - } - $format = count($parameters) ? $parameters[0] : 'Y/m/d'; - - try { - Verta::parseFormat($format, $value); - } catch (Exception $e) { - return false; - } - - return true; + return $this->validateDateTimeValue($value, $parameters, self::DEFAULT_DATE_FORMAT); } /** - * Determines if an input is a valid Jalali date with the specified format - * @param string $attribute - * @param string $value - * @param array $parameters - * @return bool + * Validate Jalali date with multiple possible formats */ public function validateDateMultiFormat(string $attribute, string $value, array $parameters): bool { foreach ($parameters as $format) { - if ($this->validateDate($attribute, $value, [$format])) { + if ($this->validateDateTimeValue($value, [$format], self::DEFAULT_DATE_FORMAT)) { return true; } } + return false; } /** - * Determines if an input is a valid Jalali date with the specified format and - * it is equal a given date - * @param string $attribute - * @param string $value - * @param array $parameters - * @return bool + * Validate Jalali date equality */ - public function validateDateEqual($attribute, $value, $parameters) + public function validateDateEqual(string $attribute, $value, array $parameters): bool { - if (!is_string($value)) { - return false; - } - $format = count($parameters) > 1 ? $parameters[1] : 'Y/m/d'; - - try { - $base = count($parameters) > 0 ? Verta::parseFormat($format, $parameters[0]) : null; - - return Verta::parseFormat($format, $value)->eq($base); - } catch (Exception $e) { - return false; - } + return $this->validateDateTimeComparison( + $value, + $parameters, + self::DEFAULT_DATE_FORMAT, + fn($value, $base) => $value->eq($base) + ); } /** - * Determines if an input is a valid Jalali date with the specified format and - * it is not equal a given date - * @param string $attribute - * @param string $value - * @param array $parameters - * @return bool + * Validate Jalali date inequality */ - public function validateDateNotEqual($attribute, $value, $parameters) + public function validateDateNotEqual(string $attribute, $value, array $parameters): bool { - if (!is_string($value)) { - return false; - } - $format = count($parameters) > 1 ? $parameters[1] : 'Y/m/d'; - - try { - $base = count($parameters) > 0 ? Verta::parseFormat($format, $parameters[0]) : null; - - return Verta::parseFormat($format, $value)->ne($base); - } catch (Exception $e) { - return false; - } + return $this->validateDateTimeComparison( + $value, + $parameters, + self::DEFAULT_DATE_FORMAT, + fn($value, $base) => $value->ne($base) + ); } /** - * Determines if an input is a valid Jalali datetime with the specified format - * @param string $attribute - * @param string $value - * @param array $parameters - * @return bool + * Validate Jalali datetime with specified format */ - public function validateDateTime($attribute, $value, $parameters) + public function validateDateTime(string $attribute, $value, array $parameters): bool { - if (!is_string($value)) { - return false; - } - - $format = count($parameters) ? $parameters[0] : 'Y/m/d H:i:s'; - - try { - Verta::parseFormat($format, $value); - } catch (Exception $e) { - return false; - } - - return true; + return $this->validateDateTimeValue($value, $parameters, self::DEFAULT_DATETIME_FORMAT); } /** - * Determines if an input is a valid Jalali datetime with the specified format and - * it is equal a given datetime - * @param string $attribute - * @param string $value - * @param array $parameters - * @return bool + * Validate Jalali datetime equality */ - public function validateDateTimeEqual($attribute, $value, $parameters) + public function validateDateTimeEqual(string $attribute, $value, array $parameters): bool { - if (!is_string($value)) { - return false; - } - $format = count($parameters) > 1 ? $parameters[1] : 'Y/m/d H:i:s'; - - try { - $base = count($parameters) > 0 ? Verta::parseFormat($format, $parameters[0]) : null; - - return Verta::parseFormat($format, $value)->eq($base); - } catch (Exception $e) { - return false; - } + return $this->validateDateTimeComparison( + $value, + $parameters, + self::DEFAULT_DATETIME_FORMAT, + fn($value, $base) => $value->eq($base) + ); } /** - * Determines if an input is a valid Jalali datetime with the specified format and - * it is not equal a given datetime - * @param string $attribute - * @param string $value - * @param array $parameters - * @return bool + * Validate Jalali datetime inequality */ - public function validateDateTimeNotEqual($attribute, $value, $parameters) + public function validateDateTimeNotEqual(string $attribute, $value, array $parameters): bool { - if (!is_string($value)) { - return false; - } - $format = count($parameters) > 1 ? $parameters[1] : 'Y/m/d H:i:s'; - - try { - $base = count($parameters) > 0 ? Verta::parseFormat($format, $parameters[0]) : null; - - return Verta::parseFormat($format, $value)->ne($base); - } catch (Exception $e) { - return false; - } + return $this->validateDateTimeComparison( + $value, + $parameters, + self::DEFAULT_DATETIME_FORMAT, + fn($value, $base) => $value->ne($base) + ); } /** - * Determines if an input is a valid Jalali date with the specified format and - * it is after a given date - * @param string $attribute - * @param string $value - * @param array $parameters - * @return bool + * Validate Jalali date is after */ - public function validateDateAfter($attribute, $value, $parameters) + public function validateDateAfter(string $attribute, $value, array $parameters): bool { - if (!is_string($value)) { - return false; - } - $format = count($parameters) > 1 ? $parameters[1] : 'Y/m/d'; - - try { - $base = count($parameters) > 0 ? Verta::parseFormat($format, $parameters[0]) : null; - - return Verta::parseFormat($format, $value)->gt($base); - } catch (Exception $e) { - return false; - } + return $this->validateDateTimeComparison( + $value, + $parameters, + self::DEFAULT_DATE_FORMAT, + fn($value, $base) => $value->gt($base) + ); } /** - * Determines if an input is a valid Jalali datetime with the specified format and - * it is after or equal a given datetime - * @param string $attribute - * @param string $value - * @param array $parameters - * @return bool + * Validate Jalali date is after or equal */ - public function validateDateAfterEqual($attribute, $value, $parameters) + public function validateDateAfterEqual(string $attribute, $value, array $parameters): bool { - if (!is_string($value)) { - return false; - } - $format = count($parameters) > 1 ? $parameters[1] : 'Y/m/d'; - - try { - $base = count($parameters) > 0 ? Verta::parseFormat($format, $parameters[0]) : null; - - return Verta::parseFormat($format, $value)->gte($base); - } catch (Exception $e) { - return false; - } + return $this->validateDateTimeComparison( + $value, + $parameters, + self::DEFAULT_DATE_FORMAT, + fn($value, $base) => $value->gte($base) + ); } /** - * Determines if an input is a valid Jalali datetime with the specified format and - * it is after a given datetime - * @param string $attribute - * @param string $value - * @param array $parameters - * @return bool + * Validate Jalali datetime is after */ - public function validateDateTimeAfter($attribute, $value, $parameters) + public function validateDateTimeAfter(string $attribute, $value, array $parameters): bool { - if (!is_string($value)) { - return false; - } - $format = count($parameters) > 1 ? $parameters[1] : 'Y/m/d H:i:s'; - - try { - $base = count($parameters) > 0 ? Verta::parseFormat($format, $parameters[0]) : null; - - return Verta::parseFormat($format, $value)->gt($base); - } catch (Exception $e) { - return false; - } + return $this->validateDateTimeComparison( + $value, + $parameters, + self::DEFAULT_DATETIME_FORMAT, + fn($value, $base) => $value->gt($base) + ); } /** - * Determines if an input is a valid Jalali datetime with the specified format and - * it is after or equal a given datetime - * @param string $attribute - * @param string $value - * @param array $parameters - * @return bool + * Validate Jalali datetime is after or equal */ - public function validateDateTimeAfterEqual($attribute, $value, $parameters) + public function validateDateTimeAfterEqual(string $attribute, $value, array $parameters): bool { - if (!is_string($value)) { - return false; - } - $format = count($parameters) > 1 ? $parameters[1] : 'Y/m/d H:i:s'; - - try { - $base = count($parameters) > 0 ? Verta::parseFormat($format, $parameters[0]) : null; + return $this->validateDateTimeComparison( + $value, + $parameters, + self::DEFAULT_DATETIME_FORMAT, + fn($value, $base) => $value->gte($base) + ); + } - return Verta::parseFormat($format, $value)->gte($base); - } catch (Exception $e) { - return false; - } + /** + * Validate Jalali date is before + */ + public function validateDateBefore(string $attribute, $value, array $parameters): bool + { + return $this->validateDateTimeComparison( + $value, + $parameters, + self::DEFAULT_DATE_FORMAT, + fn($value, $base) => $value->lt($base) + ); } /** - * Determines if an input is a valid Jalali date with the specified format and - * it is before a given date - * @param string $attribute - * @param string $value - * @param array $parameters - * @return bool + * Validate Jalali date is before or equal */ - public function validateDateBefore($attribute, $value, $parameters) + public function validateDateBeforeEqual(string $attribute, $value, array $parameters): bool { - if (!is_string($value)) { - return false; - } - $format = count($parameters) > 1 ? $parameters[1] : 'Y/m/d'; + return $this->validateDateTimeComparison( + $value, + $parameters, + self::DEFAULT_DATE_FORMAT, + fn($value, $base) => $value->lte($base) + ); + } - try { - $base = count($parameters) > 0 ? Verta::parseFormat($format, $parameters[0]) : null; + /** + * Validate Jalali datetime is before + */ + public function validateDateTimeBefore(string $attribute, $value, array $parameters): bool + { + return $this->validateDateTimeComparison( + $value, + $parameters, + self::DEFAULT_DATETIME_FORMAT, + fn($value, $base) => $value->lt($base) + ); + } - return Verta::parseFormat($format, $value)->lt($base); - } catch (Exception $e) { - return false; - } + /** + * Validate Jalali datetime is before or equal + */ + public function validateDateTimeBeforeEqual(string $attribute, $value, array $parameters): bool + { + return $this->validateDateTimeComparison( + $value, + $parameters, + self::DEFAULT_DATETIME_FORMAT, + fn($value, $base) => $value->lte($base) + ); } /** - * Determines if an input is a valid Jalali date with the specified format and - * it is before or equal a given date - * @param string $attribute - * @param string $value - * @param array $parameters - * @return bool + * Replace date or datetime in message */ - public function validateDateBeforeEqual($attribute, $value, $parameters) + public function replaceDateOrDatetime($message, $attribute, $rule, $parameters): string { - if (!is_string($value)) { - return false; - } - $format = count($parameters) > 1 ? $parameters[1] : 'Y/m/d'; + return $message; + } - try { - $base = count($parameters) > 0 ? Verta::parseFormat($format, $parameters[0]) : null; + /** + * Replace date comparison in message + */ + public function replaceDateAfterOrBeforeOrEqual($message, $attribute, $rule, $parameters): string + { + return $this->replaceComparisonDateInMessage($message, $parameters, self::DEFAULT_DATE_FORMAT); + } - return Verta::parseFormat($format, $value)->lte($base); - } catch (Exception $e) { - return false; - } + /** + * Replace datetime comparison in message + */ + public function replaceDateTimeAfterOrBeforeOrEqual($message, $attribute, $rule, $parameters): string + { + return $this->replaceComparisonDateInMessage($message, $parameters, self::DEFAULT_DATETIME_FORMAT); } /** - * Determines if an input is a valid Jalali datetime with the specified format and - * it is before a given date-time - * @param string $attribute - * @param string $value - * @param array $parameters - * @return bool + * Core validation method for date/time values */ - public function validateDateTimeBefore($attribute, $value, $parameters) + protected function validateDateTimeValue($value, array $parameters, string $defaultFormat): bool { if (!is_string($value)) { return false; } - $format = count($parameters) > 1 ? $parameters[1] : 'Y/m/d H:i:s'; - try { - $base = count($parameters) > 0 ? Verta::parseFormat($format, $parameters[0]) : null; + $format = $parameters[0] ?? $defaultFormat; - return Verta::parseFormat($format, $value)->lt($base); + try { + Verta::parseFormat($format, $value); + return true; } catch (Exception $e) { return false; } } /** - * Determines if an input is a valid Jalali datetime with the specified format and - * it is before or equal a given datetime - * @param string $attribute - * @param string $value - * @param array $parameters - * @return bool + * Core comparison validation method */ - public function validateDateTimeBeforeEqual($attribute, $value, $parameters) - { + protected function validateDateTimeComparison( + $value, + array $parameters, + string $defaultFormat, + callable $comparison + ): bool { if (!is_string($value)) { return false; } - $format = count($parameters) > 1 ? $parameters[1] : 'Y/m/d H:i:s'; - try { - $base = count($parameters) > 0 ? Verta::parseFormat($format, $parameters[0]) : null; + $format = $parameters[1] ?? $defaultFormat; - return Verta::parseFormat($format, $value)->lte($base); + try { + $base = isset($parameters[0]) ? Verta::parseFormat($format, $parameters[0]) : null; + $parsedValue = Verta::parseFormat($format, $value); + + return $comparison($parsedValue, $base); } catch (Exception $e) { return false; } } /** - * replace date or datetime - * @param $message - * @param $attribute - * @param $rule - * @param $parameters - * @return string + * Replace date in validation message */ - public function replaceDateOrDatetime($message, $attribute, $rule, $parameters) - { - return $message; - } + protected function replaceComparisonDateInMessage( + string $message, + array $parameters, + string $defaultFormat + ): string { + $format = $parameters[1] ?? $defaultFormat; + $date = $parameters[0] ?? Verta::instance()->format($format); - /** - * replace date after or before or equal - * @param $message - * @param $attribute - * @param $rule - * @param $parameters - * @return string - */ - public function replaceDateAfterOrBeforeOrEqual($message, $attribute, $rule, $parameters) - { - $format = count($parameters) > 1 ? $parameters[1] : 'Y/m/d'; - $date = count($parameters) ? $parameters[0] : Verta::instance()->format($format); - if (Verta::getLocale() != 'en') { - $en = Verta::getMessages('en'); - $to = Verta::getMessages(); - $date = str_replace(array_values($en['numbers']), array_values($to['numbers']), $date); - } - - return str_replace(':date', $date, $message); - } - - /** - * replace date time after or before or equal - * @param $message - * @param $attribute - * @param $rule - * @param $parameters - * @return string - */ - public function replaceDateTimeAfterOrBeforeOrEqual($message, $attribute, $rule, $parameters) - { - $format = count($parameters) > 1 ? $parameters[1] : 'Y/m/d H:i:s'; - $date = count($parameters) ? $parameters[0] : Verta::instance()->format($format); if (Verta::getLocale() != 'en') { $en = Verta::getMessages('en'); $to = Verta::getMessages();