-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
2 changed files
with
47 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
namespace Kit\FormBundle\Form\DataTransformer; | ||
|
||
use Symfony\Component\Form\DataTransformerInterface; | ||
use Symfony\Component\Form\Exception\TransformationFailedException; | ||
|
||
class DateTimeTransformer implements DataTransformerInterface | ||
{ | ||
/** | ||
* Transforms an object (DateTime) to a string (date). | ||
* | ||
* @param \DateTime|string $dateTime | ||
* @return string | ||
*/ | ||
public function transform($dateTime) | ||
{ | ||
if($dateTime instanceof \DateTime){ | ||
return $dateTime->format('Y-m-d H:i:s'); | ||
} | ||
|
||
return $dateTime; | ||
} | ||
|
||
/** | ||
* Transforms a string (date) to an object (DateTime). | ||
* | ||
* @param string $dateStr | ||
* @return string | ||
* @throws TransformationFailedException if object (issue) is not found. | ||
*/ | ||
public function reverseTransform($dateStr) | ||
{ | ||
try{ | ||
$dateTime = new \DateTime($dateStr); | ||
}catch (\Exception $e) { | ||
throw new TransformationFailedException(sprintf( | ||
'DateTime convert error!', | ||
$e->getMessage() | ||
)); | ||
} | ||
|
||
return $dateTime; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,13 +1,11 @@ | ||
services: | ||
_defaults: | ||
autowire: true | ||
autoconfigure: true | ||
# kit_form.example: | ||
# class: Kit\FormBundle\Example | ||
# arguments: ["@service_id", "plain_value", "%parameter%"] | ||
kit_form.form.data_transformer_date: | ||
class: Kit\FormBundle\Form\DataTransformer\DateTransformer | ||
Kit\FormBundle\Form\DataTransformer: ~ | ||
Kit\FormBundle\Form\DataTransformer\DateTransformer: "@kit_form.form.data_transformer_date" | ||
Kit\FormBundle\Form\DataTransformer\DateTransformer: | ||
autowire: true | ||
kit_form.form.datatime_transformer_date: | ||
class: Kit\FormBundle\Form\DataTransformer\DateTimeTransformer | ||
Kit\FormBundle\Form\DataTransformer\DateTransformer: "@kit_form.form.datatime_transformer_date" |