fix: refactor jalali validator
This commit is contained in:
1 parent
a6ea0e4eec
commit
f0b648b7a4
2 files changed
+92
-230
No files matched your search
+91
-225
@@ -7,28 +7,8 @@ use Hekmatinasser\Verta\Verta;
|
|||||||
|
|
||||||
class JalaliValidator
|
class JalaliValidator
|
||||||
{
|
{
|
||||||
/**
|
private const DEFAULT_DATE_FORMAT = 'Y/m/d';
|
||||||
* Determines if an input is a valid Jalali date with the specified format
|
private const DEFAULT_DATETIME_FORMAT = 'Y/m/d H:i:s';
|
||||||
* @param string $attribute
|
|
||||||
* @param string $value
|
|
||||||
* @param array $parameters
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function validateDate($attribute, $value, $parameters)
|
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determines if an input is a valid Jalali date with the specified format
|
* Determines if an input is a valid Jalali date with the specified format
|
||||||
@@ -37,15 +17,9 @@ class JalaliValidator
|
|||||||
* @param array $parameters
|
* @param array $parameters
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function validateDateMultiFormat(string $attribute, string $value, array $parameters): bool
|
public function validateDate(string $attribute, mixed $value, array $parameters): bool
|
||||||
{
|
{
|
||||||
foreach ($parameters as $format) {
|
return $this->validateDateTimeValue($value, $parameters, self::DEFAULT_DATE_FORMAT);
|
||||||
if ($this->validateDate($attribute, $value, [$format])) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -56,20 +30,9 @@ class JalaliValidator
|
|||||||
* @param array $parameters
|
* @param array $parameters
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function validateDateEqual($attribute, $value, $parameters)
|
public function validateDateEqual(string $attribute, mixed $value, array $parameters): bool
|
||||||
{
|
{
|
||||||
if (! is_string($value)) {
|
return $this->validateDateTimeComparison($value, $parameters, self::DEFAULT_DATE_FORMAT, 'eq');
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -80,20 +43,9 @@ class JalaliValidator
|
|||||||
* @param array $parameters
|
* @param array $parameters
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function validateDateNotEqual($attribute, $value, $parameters)
|
public function validateDateNotEqual(string $attribute, mixed $value, array $parameters): bool
|
||||||
{
|
{
|
||||||
if (! is_string($value)) {
|
return $this->validateDateTimeComparison($value, $parameters, self::DEFAULT_DATE_FORMAT, 'ne');
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -103,21 +55,9 @@ class JalaliValidator
|
|||||||
* @param array $parameters
|
* @param array $parameters
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function validateDateTime($attribute, $value, $parameters)
|
public function validateDateTime(string $attribute, mixed $value, array $parameters): bool
|
||||||
{
|
{
|
||||||
if (! is_string($value)) {
|
return $this->validateDateTimeValue($value, $parameters, self::DEFAULT_DATETIME_FORMAT);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -128,20 +68,9 @@ class JalaliValidator
|
|||||||
* @param array $parameters
|
* @param array $parameters
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function validateDateTimeEqual($attribute, $value, $parameters)
|
public function validateDateTimeEqual(string $attribute, mixed $value, array $parameters): bool
|
||||||
{
|
{
|
||||||
if (! is_string($value)) {
|
return $this->validateDateTimeComparison($value, $parameters, self::DEFAULT_DATETIME_FORMAT, 'eq');
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -152,20 +81,9 @@ class JalaliValidator
|
|||||||
* @param array $parameters
|
* @param array $parameters
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function validateDateTimeNotEqual($attribute, $value, $parameters)
|
public function validateDateTimeNotEqual(string $attribute, mixed $value, array $parameters): bool
|
||||||
{
|
{
|
||||||
if (! is_string($value)) {
|
return $this->validateDateTimeComparison($value, $parameters, self::DEFAULT_DATETIME_FORMAT, 'ne');
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -176,20 +94,9 @@ class JalaliValidator
|
|||||||
* @param array $parameters
|
* @param array $parameters
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function validateDateAfter($attribute, $value, $parameters)
|
public function validateDateAfter(string $attribute, mixed $value, array $parameters): bool
|
||||||
{
|
{
|
||||||
if (! is_string($value)) {
|
return $this->validateDateTimeComparison($value, $parameters, self::DEFAULT_DATE_FORMAT, 'gt');
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -200,20 +107,9 @@ class JalaliValidator
|
|||||||
* @param array $parameters
|
* @param array $parameters
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function validateDateAfterEqual($attribute, $value, $parameters)
|
public function validateDateAfterEqual(string $attribute, mixed $value, array $parameters): bool
|
||||||
{
|
{
|
||||||
if (! is_string($value)) {
|
return $this->validateDateTimeComparison($value, $parameters, self::DEFAULT_DATE_FORMAT, 'gte');
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -224,20 +120,9 @@ class JalaliValidator
|
|||||||
* @param array $parameters
|
* @param array $parameters
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function validateDateTimeAfter($attribute, $value, $parameters)
|
public function validateDateTimeAfter(string $attribute, mixed $value, array $parameters): bool
|
||||||
{
|
{
|
||||||
if (! is_string($value)) {
|
return $this->validateDateTimeComparison($value, $parameters, self::DEFAULT_DATETIME_FORMAT, 'gt');
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -248,20 +133,9 @@ class JalaliValidator
|
|||||||
* @param array $parameters
|
* @param array $parameters
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function validateDateTimeAfterEqual($attribute, $value, $parameters)
|
public function validateDateTimeAfterEqual(string $attribute, mixed $value, array $parameters): bool
|
||||||
{
|
{
|
||||||
if (! is_string($value)) {
|
return $this->validateDateTimeComparison($value, $parameters, self::DEFAULT_DATETIME_FORMAT, 'gte');
|
||||||
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)->gte($base);
|
|
||||||
} catch (Exception $e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -272,20 +146,9 @@ class JalaliValidator
|
|||||||
* @param array $parameters
|
* @param array $parameters
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function validateDateBefore($attribute, $value, $parameters)
|
public function validateDateBefore(string $attribute, mixed $value, array $parameters): bool
|
||||||
{
|
{
|
||||||
if (! is_string($value)) {
|
return $this->validateDateTimeComparison($value, $parameters, self::DEFAULT_DATE_FORMAT, 'lt');
|
||||||
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)->lt($base);
|
|
||||||
} catch (Exception $e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -296,20 +159,9 @@ class JalaliValidator
|
|||||||
* @param array $parameters
|
* @param array $parameters
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function validateDateBeforeEqual($attribute, $value, $parameters)
|
public function validateDateBeforeEqual(string $attribute, mixed $value, array $parameters): bool
|
||||||
{
|
{
|
||||||
if (! is_string($value)) {
|
return $this->validateDateTimeComparison($value, $parameters, self::DEFAULT_DATE_FORMAT, 'lte');
|
||||||
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)->lte($base);
|
|
||||||
} catch (Exception $e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -320,20 +172,9 @@ class JalaliValidator
|
|||||||
* @param array $parameters
|
* @param array $parameters
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function validateDateTimeBefore($attribute, $value, $parameters)
|
public function validateDateTimeBefore(string $attribute, mixed $value, array $parameters): bool
|
||||||
{
|
{
|
||||||
if (! is_string($value)) {
|
return $this->validateDateTimeComparison($value, $parameters, self::DEFAULT_DATETIME_FORMAT, 'lt');
|
||||||
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)->lt($base);
|
|
||||||
} catch (Exception $e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -344,67 +185,92 @@ class JalaliValidator
|
|||||||
* @param array $parameters
|
* @param array $parameters
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function validateDateTimeBeforeEqual($attribute, $value, $parameters)
|
public function validateDateTimeBeforeEqual(string $attribute, mixed $value, array $parameters): bool
|
||||||
{
|
{
|
||||||
if (! is_string($value)) {
|
return $this->validateDateTimeComparison($value, $parameters, self::DEFAULT_DATETIME_FORMAT, 'lte');
|
||||||
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)->lte($base);
|
|
||||||
} catch (Exception $e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* replace date or datetime
|
* replace date or datetime
|
||||||
* @param $message
|
* @param string $message
|
||||||
* @param $attribute
|
* @param string $attribute
|
||||||
* @param $rule
|
* @param string $rule
|
||||||
* @param $parameters
|
* @param array $parameters
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function replaceDateOrDatetime($message, $attribute, $rule, $parameters)
|
public function replaceDateOrDatetime(string $message, string $attribute, string $rule, array $parameters): string
|
||||||
{
|
{
|
||||||
return $message;
|
return $message;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* replace date after or before or equal
|
* replace date after or before or equal
|
||||||
* @param $message
|
* @param string $message
|
||||||
* @param $attribute
|
* @param string $attribute
|
||||||
* @param $rule
|
* @param string $rule
|
||||||
* @param $parameters
|
* @param array $parameters
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function replaceDateAfterOrBeforeOrEqual($message, $attribute, $rule, $parameters)
|
public function replaceDateAfterOrBeforeOrEqual(string $message, string $attribute, string $rule, array $parameters): string
|
||||||
{
|
{
|
||||||
$format = count($parameters) > 1 ? $parameters[1] : 'Y/m/d';
|
return $this->replaceComparisonDateInMessage($message, $parameters, self::DEFAULT_DATE_FORMAT);
|
||||||
$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
|
* replace date time after or before or equal
|
||||||
* @param $message
|
* @param string $message
|
||||||
* @param $attribute
|
* @param string $attribute
|
||||||
* @param $rule
|
* @param string $rule
|
||||||
* @param $parameters
|
* @param array $parameters
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function replaceDateTimeAfterOrBeforeOrEqual($message, $attribute, $rule, $parameters)
|
public function replaceDateTimeAfterOrBeforeOrEqual(string $message, string $attribute, string $rule, array $parameters): string
|
||||||
{
|
{
|
||||||
$format = count($parameters) > 1 ? $parameters[1] : 'Y/m/d H:i:s';
|
return $this->replaceComparisonDateInMessage($message, $parameters, self::DEFAULT_DATETIME_FORMAT);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function validateDateTimeValue(mixed $value, array $parameters, string $defaultFormat): bool
|
||||||
|
{
|
||||||
|
if (! is_string($value)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$format = count($parameters) ? $parameters[0] : $defaultFormat;
|
||||||
|
|
||||||
|
try {
|
||||||
|
Verta::parseFormat($format, $value);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function validateDateTimeComparison(
|
||||||
|
mixed $value,
|
||||||
|
array $parameters,
|
||||||
|
string $defaultFormat,
|
||||||
|
string $comparison
|
||||||
|
): bool {
|
||||||
|
if (! is_string($value)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$format = count($parameters) > 1 ? $parameters[1] : $defaultFormat;
|
||||||
|
|
||||||
|
try {
|
||||||
|
// A missing comparison date preserves the comparison methods' "now" default.
|
||||||
|
$base = count($parameters) > 0 ? Verta::parseFormat($format, $parameters[0]) : null;
|
||||||
|
|
||||||
|
return Verta::parseFormat($format, $value)->{$comparison}($base);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function replaceComparisonDateInMessage(string $message, array $parameters, string $defaultFormat): string
|
||||||
|
{
|
||||||
|
$format = count($parameters) > 1 ? $parameters[1] : $defaultFormat;
|
||||||
$date = count($parameters) ? $parameters[0] : Verta::instance()->format($format);
|
$date = count($parameters) ? $parameters[0] : Verta::instance()->format($format);
|
||||||
if (Verta::getLocale() != 'en') {
|
if (Verta::getLocale() != 'en') {
|
||||||
$en = Verta::getMessages('en');
|
$en = Verta::getMessages('en');
|
||||||
|
|||||||
@@ -9,15 +9,11 @@ use Illuminate\Support\ServiceProvider;
|
|||||||
|
|
||||||
class VertaServiceProvider extends ServiceProvider
|
class VertaServiceProvider extends ServiceProvider
|
||||||
{
|
{
|
||||||
private $rules = [
|
private array $rules = [
|
||||||
'jdate' => [
|
'jdate' => [
|
||||||
'extend' => 'validateDate',
|
'extend' => 'validateDate',
|
||||||
'replacer' => 'replaceDateOrDatetime',
|
'replacer' => 'replaceDateOrDatetime',
|
||||||
],
|
],
|
||||||
'jdate_multi_format' => [
|
|
||||||
'extend' => 'validateDateMultiFormat',
|
|
||||||
'replacer' => 'replaceDateOrDatetime',
|
|
||||||
],
|
|
||||||
'jdate_equal' => [
|
'jdate_equal' => [
|
||||||
'extend' => 'validateDateEqual',
|
'extend' => 'validateDateEqual',
|
||||||
'replacer' => 'replaceDateAfterOrBeforeOrEqual',
|
'replacer' => 'replaceDateAfterOrBeforeOrEqual',
|
||||||
|
|||||||
Reference in new issue
Block a user