Created
March 30, 2020 18:51
-
-
Save mbaumgartl/d3b4555c0d7b4259ec43f1ade6ca30e0 to your computer and use it in GitHub Desktop.
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
<?php | |
declare(strict_types=1); | |
require_once __DIR__ . '/vendor/autoload.php'; | |
use Laminas\Filter; | |
use Laminas\Validator; | |
final class InputFilter extends \Laminas\InputFilter\InputFilter | |
{ | |
public function __construct() | |
{ | |
$filters = [ | |
['name' => Filter\StringTrim::class], | |
['name' => Filter\DateTimeFormatter::class] | |
]; | |
$validators = [ | |
['name' => Validator\NotEmpty::class], | |
[ | |
'name' => Validator\Date::class, | |
'options' => ['format' => DateTime::ATOM] | |
] | |
]; | |
$this->add([ | |
'type' => \Laminas\InputFilter\InputFilter::class, | |
'start' => [ | |
'required' => true, | |
'break_chain_on_failure' => true, | |
'filters' => $filters, | |
'validators' => $validators | |
], | |
'end' => [ | |
'required' => true, | |
'filters' => $filters, | |
'validators' => array_merge($validators, [ | |
[ | |
'name' => Validator\Callback::class, | |
'options' => [ | |
'callback' => static function (string $value, array $context = []): bool { | |
$start = null; | |
try { | |
$start = new DateTimeImmutable($context['start']); | |
} catch (Exception $e) { | |
return false; | |
} | |
return DateTimeImmutable::createFromFormat(DateTime::ATOM, $value) > $start; | |
} | |
] | |
] | |
]) | |
] | |
], 'duration'); | |
} | |
} | |
$inputFilter = new InputFilter(); | |
$inputFilter->setData([ | |
'duration' => [ | |
'start' => '2020-03-31T18:00', | |
'end' => '2020-03-31T21:59:59', | |
] | |
]); | |
$inputFilter->isValid(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment