Skip to content

Instantly share code, notes, and snippets.

@mbaumgartl
Created March 30, 2020 18:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbaumgartl/d3b4555c0d7b4259ec43f1ade6ca30e0 to your computer and use it in GitHub Desktop.
Save mbaumgartl/d3b4555c0d7b4259ec43f1ade6ca30e0 to your computer and use it in GitHub Desktop.
<?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