-
Notifications
You must be signed in to change notification settings - Fork 773
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #292 from henriquemoody/date_time
Improve date and time handling on "Min" and "Max" rules
- Loading branch information
Showing
8 changed files
with
74 additions
and
34 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
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,36 @@ | ||
<?php | ||
namespace Respect\Validation\Rules; | ||
|
||
use Exception; | ||
use DateTime; | ||
|
||
abstract class AbstractInterval extends AbstractRule | ||
{ | ||
public $interval; | ||
public $inclusive; | ||
|
||
public function __construct($interval, $inclusive = false) | ||
{ | ||
$this->interval = $interval; | ||
$this->inclusive = $inclusive; | ||
} | ||
|
||
protected function filterInterval($value) | ||
{ | ||
if (!is_string($value) || is_numeric($value) || empty($value)) { | ||
return $value; | ||
} | ||
|
||
if (strlen($value) == 1) { | ||
return $value; | ||
} | ||
|
||
try { | ||
return new DateTime($value); | ||
} catch (Exception $e) { | ||
// Pokémon Exception Handling | ||
} | ||
|
||
return $value; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,23 +1,14 @@ | ||
<?php | ||
namespace Respect\Validation\Rules; | ||
|
||
class Max extends AbstractRule | ||
class Max extends AbstractInterval | ||
{ | ||
public $maxValue; | ||
public $inclusive; | ||
|
||
public function __construct($maxValue, $inclusive = false) | ||
{ | ||
$this->maxValue = $maxValue; | ||
$this->inclusive = $inclusive; | ||
} | ||
|
||
public function validate($input) | ||
{ | ||
if ($this->inclusive) { | ||
return $input <= $this->maxValue; | ||
} else { | ||
return $input < $this->maxValue; | ||
return $this->filterInterval($input) <= $this->filterInterval($this->interval); | ||
} | ||
|
||
return $this->filterInterval($input) < $this->filterInterval($this->interval); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,23 +1,14 @@ | ||
<?php | ||
namespace Respect\Validation\Rules; | ||
|
||
class Min extends AbstractRule | ||
class Min extends AbstractInterval | ||
{ | ||
public $inclusive; | ||
public $minValue; | ||
|
||
public function __construct($minValue, $inclusive = false) | ||
{ | ||
$this->minValue = $minValue; | ||
$this->inclusive = $inclusive; | ||
} | ||
|
||
public function validate($input) | ||
{ | ||
if ($this->inclusive) { | ||
return $input >= $this->minValue; | ||
} else { | ||
return $input > $this->minValue; | ||
return $this->filterInterval($input) >= $this->filterInterval($this->interval); | ||
} | ||
|
||
return $this->filterInterval($input) > $this->filterInterval($this->interval); | ||
} | ||
} |
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