| @@ -8,8 +8,10 @@ | ||
| 8 | 8 | declare(strict_types=1); |
| 9 | 9 | |
| 10 | 10 | namespace Packetery\Module; |
| 11 | 11 | |
| 12 | +use DateTimeImmutable; | |
| 13 | +use Packetery\Core\CoreHelper; | |
| 12 | 14 | use Packetery\Nette\Forms\Controls\BaseControl; |
| 13 | 15 | |
| 14 | 16 | /** |
| 15 | 17 | * Class FormValidators |
| @@ -16,9 +18,8 @@ | ||
| 16 | 18 | * |
| 17 | 19 | * @package Packetery\Module |
| 18 | 20 | */ |
| 19 | 21 | class FormValidators { |
| 20 | - | |
| 21 | 22 | /** |
| 22 | 23 | * Tests if input value is greater than argument. |
| 23 | 24 | * |
| 24 | 25 | * @param BaseControl $input Form input. |
| @@ -39,6 +40,44 @@ | ||
| 39 | 40 | * @return bool |
| 40 | 41 | */ |
| 41 | 42 | public static function dateIsLater( BaseControl $input, string $date ): bool { |
| 42 | 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; | |
| 43 | 82 | } |
| 44 | 83 | } |