Skip to content
This repository has been archived by the owner on Apr 1, 2024. It is now read-only.

Commit

Permalink
Added DateTimeBefore, DateTimeAfter, DateTimeBetween, Equals & FileEx…
Browse files Browse the repository at this point in the history
…ists
  • Loading branch information
Alphametric committed Apr 8, 2019
1 parent 6f56a31 commit 7a5cdcf
Show file tree
Hide file tree
Showing 23 changed files with 563 additions and 18 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ The following validation rules are currently available:
| Decimal | validation.decimal | Requires that the given value is a decimal with an appropriate format - see class for details |
| EncodedImage | validation.encoded_image | Requires that the given value is a base64-encoded image of a given mime type - see class for details |
| LocationCoordinates | validation.location_coordinates | Requires that the given value is a comma-separated set of latitude and longitude coordinates |
| DateTimeBefore | validation.datetime_before | Requires that the given value is before a given date - see class for details |
| DateTimeAfter | validation.datetime_after | Requires that the given value is after a given date - see class for details |
| DateTimeBetween | validation.datetime_between | Requires that the given value is between two dates - see class for details |
| FileExists | validation.file_exists | Requires that the given value is a path to an existing file - see class for details |
| Equals | validation.equals | Requires that the given value is equal to another given value |

The package will receive new rules over time, however since these updates will not be breaking changes, they will not receive major version numbers unless Laravel changes in such a way that the package requires a re-write.

Expand Down
92 changes: 92 additions & 0 deletions src/DateTimeAfter.php
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')
);
}

}
92 changes: 92 additions & 0 deletions src/DateTimeBefore.php
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')
);
}

}
108 changes: 108 additions & 0 deletions src/DateTimeBetween.php
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')
);
}

}
6 changes: 3 additions & 3 deletions src/Decimal.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public function example()
/**
* Determine if the validation rule passes.
*
* The decimal requires two parameters:
* 2. The maximum number of digits before the decimal point.
* 3. The maximum number of digits after the decimal point.
* The rule has two parameters:
* 1. The maximum number of digits before the decimal point.
* 2. The maximum number of digits after the decimal point.
*
* @param string $attribute.
* @param mixed $value.
Expand Down
44 changes: 44 additions & 0 deletions src/Equals.php
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] . '"'
);
}

}
Loading

0 comments on commit 7a5cdcf

Please sign in to comment.