-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7eb95d9
commit c9aa97c
Showing
3 changed files
with
197 additions
and
7 deletions.
There are no files selected for viewing
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
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
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); | ||
} | ||
} |
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