| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class FormValidators |
| 4 |
* |
| 5 |
* @package Packetery\Module |
| 6 |
*/ |
| 7 |
|
| 8 |
declare(strict_types=1); |
| 9 |
|
| 10 |
namespace Packetery\Module; |
| 11 |
|
| 12 |
use DateTimeImmutable; |
| 13 |
use Packetery\Core\CoreHelper; |
| 14 |
use Packetery\Nette\Forms\Controls\BaseControl; |
| 15 |
|
| 16 |
/** |
| 17 |
* Class FormValidators |
| 18 |
* |
| 19 |
* @package Packetery\Module |
| 20 |
*/ |
| 21 |
class FormValidators { |
| 22 |
/** |
| 23 |
* Tests if input value is greater than argument. |
| 24 |
* |
| 25 |
* @param BaseControl $input Form input. |
| 26 |
* @param float $arg Validation argument. |
| 27 |
* |
| 28 |
* @return bool |
| 29 |
*/ |
| 30 |
public static function greaterThan( BaseControl $input, float $arg ): bool { |
| 31 |
return $input->getValue() > $arg; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Tests if input date is later than argument date |
| 36 |
* |
| 37 |
* @param BaseControl $input Form input. |
| 38 |
* @param string $date Validation argument. |
| 39 |
* |
| 40 |
* @return bool |
| 41 |
*/ |
| 42 |
public static function dateIsLater( BaseControl $input, string $date ): bool { |
| 43 |
return strtotime( $input->getValue() ) > strtotime( $date ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Tests if input date is in proper format. |
| 48 |
* |
| 49 |
* @param BaseControl $input Form input. |
| 50 |
* |
| 51 |
* @return bool |
| 52 |
*/ |
| 53 |
public static function dateIsInMysqlFormat( BaseControl $input ): bool { |
| 54 |
$date = DateTimeImmutable::createFromFormat( |
| 55 |
CoreHelper::MYSQL_DATE_FORMAT, |
| 56 |
$input->getValue() |
| 57 |
); |
| 58 |
|
| 59 |
if ( $date === false ) { |
| 60 |
return false; |
| 61 |
} |
| 62 |
|
| 63 |
return ( $input->getValue() === $date->format( CoreHelper::MYSQL_DATE_FORMAT ) ); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Tests if input time is in Clock-Time(00:00-23:59) format. |
| 68 |
* |
| 69 |
* @param BaseControl $input Form input. |
| 70 |
* @return bool |
| 71 |
*/ |
| 72 |
public static function hasClockTimeFormat( BaseControl $input ): bool { |
| 73 |
$value = $input->getValue(); |
| 74 |
$pattern = '/^(?:[01][0-9]|2[0-3]):[0-5][0-9]$/'; |
| 75 |
$result = preg_match( $pattern, $value ); |
| 76 |
|
| 77 |
if ( $result === 0 || $result === false ) { |
| 78 |
return false; |
| 79 |
} |
| 80 |
|
| 81 |
return true; |
| 82 |
} |
| 83 |
} |
| 84 |
|