This repository has been archived by the owner on Apr 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added DateTimeBefore, DateTimeAfter, DateTimeBetween, Equals & FileEx…
…ists
- Loading branch information
Alphametric
committed
Apr 8, 2019
1 parent
6f56a31
commit 7a5cdcf
Showing
23 changed files
with
563 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
// Namespace | ||
namespace Alphametric\Validation\Rules; | ||
|
||
// Using directives | ||
use Carbon\Carbon; | ||
|
||
// Date time after rule | ||
class DateTimeAfter extends Rule | ||
{ | ||
|
||
/** | ||
* The date format for the rule. | ||
* | ||
**/ | ||
protected $date_format; | ||
|
||
|
||
|
||
/** | ||
* The minimum acceptable date. | ||
* | ||
**/ | ||
protected $minimum; | ||
|
||
|
||
|
||
/** | ||
* The time zone for the rule. | ||
* | ||
**/ | ||
protected $time_zone; | ||
|
||
|
||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param none. | ||
* @return instance. | ||
* | ||
**/ | ||
public function __construct() | ||
{ | ||
$this->minimum = func_get_args()[0]; | ||
$this->date_format = func_get_args()[1] ?? 'Y-m-d H:i:s'; | ||
$this->time_zone = func_get_args()[2] ?? 'UTC'; | ||
} | ||
|
||
|
||
|
||
/** | ||
* Determine if the validation rule passes. | ||
* | ||
* The rule has three parameters: | ||
* 1. The minimum date / time permitted. | ||
* 2. The PHP date format, defaults to 'Y-m-d H:i:s'. | ||
* 3. The PHP time zone, defaults to 'UTC'. | ||
* | ||
* @param string $attribute. | ||
* @param mixed $value. | ||
* @return bool. | ||
* | ||
**/ | ||
public function passes($attribute, $value) | ||
{ | ||
$boundary = Carbon::createFromFormat($this->date_format, $this->minimum, $this->time_zone); | ||
|
||
return Carbon::createFromFormat($this->date_format, $value, $this->time_zone)->greaterThan($boundary); | ||
} | ||
|
||
|
||
|
||
/** | ||
* Get the validation error message. | ||
* | ||
* @param none. | ||
* @return string. | ||
* | ||
**/ | ||
public function message() | ||
{ | ||
return Helper::getLocalizedErrorMessage( | ||
'datetime_after', | ||
'The :attribute must be a date / time after ' . | ||
Carbon::createFromFormat($this->date_format, $this->minimum, $this->time_zone) | ||
->format('jS F Y @ g:i:s A') | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
// Namespace | ||
namespace Alphametric\Validation\Rules; | ||
|
||
// Using directives | ||
use Carbon\Carbon; | ||
|
||
// Date time before rule | ||
class DateTimeBefore extends Rule | ||
{ | ||
|
||
/** | ||
* The date format for the rule. | ||
* | ||
**/ | ||
protected $date_format; | ||
|
||
|
||
|
||
/** | ||
* The maximum acceptable date. | ||
* | ||
**/ | ||
protected $maximum; | ||
|
||
|
||
|
||
/** | ||
* The time zone for the rule. | ||
* | ||
**/ | ||
protected $time_zone; | ||
|
||
|
||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param none. | ||
* @return instance. | ||
* | ||
**/ | ||
public function __construct() | ||
{ | ||
$this->maximum = func_get_args()[0]; | ||
$this->date_format = func_get_args()[1] ?? 'Y-m-d H:i:s'; | ||
$this->time_zone = func_get_args()[2] ?? 'UTC'; | ||
} | ||
|
||
|
||
|
||
/** | ||
* Determine if the validation rule passes. | ||
* | ||
* The rule has three parameters: | ||
* 1. The maximum date / time permitted. | ||
* 2. The PHP date format, defaults to 'Y-m-d H:i:s'. | ||
* 3. The PHP time zone, defaults to 'UTC'. | ||
* | ||
* @param string $attribute. | ||
* @param mixed $value. | ||
* @return bool. | ||
* | ||
**/ | ||
public function passes($attribute, $value) | ||
{ | ||
$boundary = Carbon::createFromFormat($this->date_format, $this->maximum, $this->time_zone); | ||
|
||
return Carbon::createFromFormat($this->date_format, $value, $this->time_zone)->lessThan($boundary); | ||
} | ||
|
||
|
||
|
||
/** | ||
* Get the validation error message. | ||
* | ||
* @param none. | ||
* @return string. | ||
* | ||
**/ | ||
public function message() | ||
{ | ||
return Helper::getLocalizedErrorMessage( | ||
'datetime_before', | ||
'The :attribute must be a date / time before ' . | ||
Carbon::createFromFormat($this->date_format, $this->maximum, $this->time_zone) | ||
->format('jS F Y @ g:i:s A') | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?php | ||
|
||
// Namespace | ||
namespace Alphametric\Validation\Rules; | ||
|
||
// Using directives | ||
use Carbon\Carbon; | ||
|
||
// Date time between rule | ||
class DateTimeBetween extends Rule | ||
{ | ||
|
||
/** | ||
* The date format for the rule. | ||
* | ||
**/ | ||
protected $date_format; | ||
|
||
|
||
|
||
/** | ||
* The maximum acceptable date. | ||
* | ||
**/ | ||
protected $maximum; | ||
|
||
|
||
|
||
/** | ||
* The minimum acceptable date. | ||
* | ||
**/ | ||
protected $minimum; | ||
|
||
|
||
|
||
/** | ||
* The time zone for the rule. | ||
* | ||
**/ | ||
protected $time_zone; | ||
|
||
|
||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param none. | ||
* @return instance. | ||
* | ||
**/ | ||
public function __construct() | ||
{ | ||
$this->minimum = func_get_args()[0]; | ||
$this->maximum = func_get_args()[1]; | ||
$this->date_format = func_get_args()[2] ?? 'Y-m-d H:i:s'; | ||
$this->time_zone = func_get_args()[3] ?? 'UTC'; | ||
} | ||
|
||
|
||
|
||
/** | ||
* Determine if the validation rule passes. | ||
* | ||
* The rule has four parameters: | ||
* 1. The minimum date / time permitted. | ||
* 2. The maximum date / time permitted. | ||
* 3. The PHP date format, defaults to 'Y-m-d H:i:s'. | ||
* 4. The PHP time zone, defaults to 'UTC'. | ||
* | ||
* @param string $attribute. | ||
* @param mixed $value. | ||
* @return bool. | ||
* | ||
**/ | ||
public function passes($attribute, $value) | ||
{ | ||
$boundary_start = Carbon::createFromFormat($this->date_format, $this->minimum, $this->time_zone); | ||
$boundary_finish = Carbon::createFromFormat($this->date_format, $this->maximum, $this->time_zone); | ||
|
||
$date_time = Carbon::createFromFormat($this->date_format, $value, $this->time_zone); | ||
|
||
return $date_time->greaterThan($boundary_start) && $date_time->lessThan($boundary_finish); | ||
} | ||
|
||
|
||
|
||
/** | ||
* Get the validation error message. | ||
* | ||
* @param none. | ||
* @return string. | ||
* | ||
**/ | ||
public function message() | ||
{ | ||
return Helper::getLocalizedErrorMessage( | ||
'datetime_between', | ||
'The :attribute must be a date / time after ' . | ||
Carbon::createFromFormat($this->date_format, $this->minimum, $this->time_zone) | ||
->format('jS F Y @ g:i:s A') . | ||
' and before ' . | ||
Carbon::createFromFormat($this->date_format, $this->maximum, $this->time_zone) | ||
->format('jS F Y @ g:i:s A') | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
// Namespace | ||
namespace Alphametric\Validation\Rules; | ||
|
||
// Equals rule | ||
class Equals extends Rule | ||
{ | ||
|
||
/** | ||
* Determine if the validation rule passes. | ||
* | ||
* This rule is semantic sugar. You can achieve the | ||
* same result by using the native 'in' rule, but | ||
* using 'equals' may make the intention clearer. | ||
* | ||
* @param string $attribute. | ||
* @param mixed $value. | ||
* @return bool. | ||
* | ||
**/ | ||
public function passes($attribute, $value) | ||
{ | ||
return $this->parameters[0] === $value; | ||
} | ||
|
||
|
||
|
||
/** | ||
* Get the validation error message. | ||
* | ||
* @param none. | ||
* @return string. | ||
* | ||
**/ | ||
public function message() | ||
{ | ||
return Helper::getLocalizedErrorMessage( | ||
'equals', | ||
'The :attribute must be set to "' . $this->parameters[0] . '"' | ||
); | ||
} | ||
|
||
} |
Oops, something went wrong.