| @@ -8,9 +8,11 @@ | ||
| 8 | 8 | declare(strict_types=1); |
| 9 | 9 | |
| 10 | 10 | namespace Packetery\Module; |
| 11 | 11 | |
| 12 | -use PacketeryNette\Forms\Controls\BaseControl; | |
| 12 | +use DateTimeImmutable; | |
| 13 | +use Packetery\Core\CoreHelper; | |
| 14 | +use Packetery\Nette\Forms\Controls\BaseControl; | |
| 13 | 15 | |
| 14 | 16 | /** |
| 15 | 17 | * Class FormValidators |
| 16 | 18 | * |
| @@ -16,17 +18,66 @@ | ||
| 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 | - * @param \PacketeryNette\Forms\Controls\BaseControl $input Form input. | |
| 25 | - * @param float $arg Validation argument. | |
| 25 | + * @param BaseControl $input Form input. | |
| 26 | + * @param float $arg Validation argument. | |
| 26 | 27 | * |
| 27 | 28 | * @return bool |
| 28 | 29 | */ |
| 29 | 30 | public static function greaterThan( BaseControl $input, float $arg ): bool { |
| 30 | 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; | |
| 31 | 82 | } |
| 32 | 83 | } |