-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added DateTime formatter for Datetime class. Based on instance of cla…
…ss to be DateTime
- Loading branch information
TeleMessage
committed
Oct 26, 2016
1 parent
ab5f14e
commit 9e2b84e
Showing
5 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
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,47 @@ | ||
<?php | ||
/** | ||
* Created by IntelliJ IDEA. | ||
* User: Grinfeld Mikhail | ||
* Date: 10/26/2016 | ||
* Time: 11:42 PM | ||
*/ | ||
|
||
namespace grinfeld\phpjsonable\transformers; | ||
|
||
|
||
use grinfeld\phpjsonable\utils\Configuration; | ||
use grinfeld\phpjsonable\utils\streams\OutputStream; | ||
|
||
class DatetimeTransformer implements Transformer { | ||
|
||
/** | ||
* checks if specific object matches current Transformer, i.e. it's instance of DateTime | ||
* @param $obj object to test | ||
* @return bool true if if specific object matches current Transformer, i.e. it's instance of DateTime, else false | ||
*/ | ||
public function match($obj) { | ||
return $obj instanceof \DateTime; | ||
} | ||
|
||
/** | ||
* @param $obj object to transform | ||
* @param OutputStream $output | ||
* @param Configuration $conf | ||
* @return void | ||
*/ | ||
public function transform($obj, OutputStream $output, Configuration $conf) { | ||
if ($conf == null) | ||
$conf = new Configuration(); | ||
|
||
$strategy = $conf->getString(Configuration::DATE_STRATEGY_PROPERTY, Configuration::DATE_STRATEGY_TIMESTAMP); | ||
|
||
if ($strategy == Configuration::DATE_STRATEGY_TIMESTAMP) { | ||
$res = $obj->getTimestamp(); | ||
$output->write("" . $res); | ||
} else { | ||
$format = $conf->getString(Configuration::DATE_FORMAT_PROPERTY, Configuration::DEFAULT_DATE_FORMAT); | ||
$res = $obj->format($format); | ||
$output->write("\"" . $res . "\""); | ||
} | ||
} | ||
} |
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
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