Skip to content

Commit

Permalink
Add DateTime input filter.
Browse files Browse the repository at this point in the history
  • Loading branch information
paragonie-security committed Feb 22, 2018
1 parent 7eb95d9 commit c9aa97c
Show file tree
Hide file tree
Showing 3 changed files with 197 additions and 7 deletions.
8 changes: 1 addition & 7 deletions src/Filter/BoolFilter.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
<?php
/**
* Created by PhpStorm.
* User: Scott
* Date: 2/5/2018
* Time: 3:07 PM
*/

declare(strict_types=1);
namespace ParagonIE\Ionizer\Filter;

use ParagonIE\Ionizer\Contract\FilterInterface;
Expand Down
62 changes: 62 additions & 0 deletions src/Filter/Special/DateTimeFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
declare(strict_types=1);
namespace ParagonIE\Ionizer\Filter\Special;

use ParagonIE\Ionizer\Filter\StringFilter;
use ParagonIE\Ionizer\InvalidDataException;

/**
* Class DateTimeFilter
* @package ParagonIE\Ionizer\Filter\Special
*/
class DateTimeFilter extends StringFilter
{
/** @var string $dateTimeFormat */
protected $dateTimeFormat;

/** @var \DateTimeZone|null $tz */
protected $tz;

/**
* DateTimeFilter constructor.
*
* @param string $format
* @param \DateTimeZone|null $tz
*/
public function __construct(
string $format = \DateTime::ATOM,
\DateTimeZone $tz = null
) {
$this->dateTimeFormat = $format;
$this->tz = $tz;
}

/**
* Apply all of the callbacks for this filter.
*
* @param string|null $data
* @param int $offset
* @return mixed
* @throws \TypeError
* @throws InvalidDataException
*/
public function applyCallbacks($data = null, int $offset = 0)
{
if ($offset === 0) {
if (!\is_null($data)) {
try {
/** @var string $dt */
$data = (new \DateTime($data, $this->tz))
->format($this->dateTimeFormat);
} catch (\Exception $ex) {
throw new InvalidDataException(
'Invalid date/time',
0,
$ex
);
}
}
}
return parent::applyCallbacks($data, $offset);
}
}
134 changes: 134 additions & 0 deletions tests/SpecialTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use ParagonIE\Ionizer\Filter\Special\{
CreditCardNumberFilter,
DateTimeFilter,
EmailAddressFilter
};
use ParagonIE\Ionizer\GeneralFilterContainer;
Expand Down Expand Up @@ -44,6 +45,139 @@ public function testCreditCardNumberFilter()
);
}

/**
* @throws Error
* @throws InvalidDataException
*/
public function testDateTimeFilter()
{
$filter = (new GeneralFilterContainer())
->addFilter(
'dob', new DateTimeFilter(
'm/d/Y',
new DateTimeZone('Etc/GMT')
)
)->addFilter(
'published',
new DateTimeFilter(
DateTime::ATOM,
new DateTimeZone('Etc/GMT')
)
)->addFilter(
'chicago',
new DateTimeFilter(
DateTime::ATOM,
new DateTimeZone('America/Chicago')
)
)->addFilter(
'london',
new DateTimeFilter(
DateTime::ATOM,
new DateTimeZone('Europe/London')
)
)->addFilter(
'newyork',
new DateTimeFilter(
DateTime::ATOM,
new DateTimeZone('America/New_York')
)
);

if (!($filter instanceof GeneralFilterContainer)) {
$this->fail('Type error');
}
$testCases = $this->getDateTimeTestCases();
foreach ($testCases as $index => $tc) {
list($before, $after) = $tc;
$this->assertEquals($after, $filter($before), $index);
}
}

/**
* @return array
*/
private function getDateTimeTestCases(): array
{
return [
[
[
'chicago' => '1970-01-01',
'dob' => '1970-01-01',
'london' => '1970-01-01',
'newyork' => '1970-01-01',
'published' => '1970-01-01'
],
[
'chicago' => '1970-01-01T00:00:00-06:00',
'dob' => '01/01/1970',
'london' => '1970-01-01T00:00:00+01:00',
'newyork' => '1970-01-01T00:00:00-05:00',
'published' => '1970-01-01T00:00:00+00:00'
]
], [
[
'chicago' => '12/25/2017',
'dob' => '12/25/2017',
'london' => '12/25/2017',
'newyork' => '12/25/2017',
'published' => '12/25/2017'
],
[
'chicago' => '2017-12-25T00:00:00-06:00',
'dob' => '12/25/2017',
'london' => '2017-12-25T00:00:00+00:00',
'newyork' => '2017-12-25T00:00:00-05:00',
'published' => '2017-12-25T00:00:00+00:00'
]
], [
[
'chicago' => '1991-02-29',
'dob' => '1991-02-29',
'london' => '1991-02-29',
'newyork' => '1991-02-29',
'published' => '1991-02-29'
],
[
'chicago' => '1991-03-01T00:00:00-06:00',
'dob' => '03/01/1991',
'london' => '1991-03-01T00:00:00+00:00',
'newyork' => '1991-03-01T00:00:00-05:00',
'published' => '1991-03-01T00:00:00+00:00'
]
], [
[
'chicago' => '1992-02-29',
'dob' => '1992-02-29',
'london' => '1992-02-29',
'newyork' => '1992-02-29',
'published' => '1992-02-29'
],
[
'chicago' => '1992-02-29T00:00:00-06:00',
'dob' => '02/29/1992',
'london' => '1992-02-29T00:00:00+00:00',
'newyork' => '1992-02-29T00:00:00-05:00',
'published' => '1992-02-29T00:00:00+00:00'
]
], [
[
'chicago' => '12/25/2017 11:33 AM',
'dob' => '12/25/2017 11:33 AM',
'london' => '12/25/2017 11:33 AM',
'newyork' => '12/25/2017 11:33 AM',
'published' => '12/25/2017 11:33 AM'
],
[
'chicago' => '2017-12-25T11:33:00-06:00',
'dob' => '12/25/2017',
'london' => '2017-12-25T11:33:00+00:00',
'newyork' => '2017-12-25T11:33:00-05:00',
'published' => '2017-12-25T11:33:00+00:00'
]
]
];
}

/**
* @throws Error
* @throws InvalidDataException
Expand Down

0 comments on commit c9aa97c

Please sign in to comment.