Skip to content

Commit

Permalink
added DateTime formatter for Datetime class. Based on instance of cla…
Browse files Browse the repository at this point in the history
…ss to be DateTime
  • Loading branch information
TeleMessage committed Oct 26, 2016
1 parent ab5f14e commit 9e2b84e
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
Binary file modified grinfeld_phpjsonable.phar
Binary file not shown.
47 changes: 47 additions & 0 deletions src/transformers/DatetimeTransformer.php
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 . "\"");
}
}
}
1 change: 1 addition & 0 deletions src/transformers/TransformerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public static function init() {
new StringTransformer(),
new ArrayTransformer(),
new MapTransformer(),
new DatetimeTransformer(),
new BeanTransformer()
);
}
Expand Down
13 changes: 13 additions & 0 deletions src/utils/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
class Configuration {

const CLASS_PROPERTY = "class_property";

const DEFAULT_CLASS_PROPERTY_VALUE = "class";

const INCLUDE_CLASS_NAME_PROPERTY = "include_class";
Expand All @@ -17,6 +18,18 @@ class Configuration {

const CLASS_TYPE_PROPERTY = "classname_strategy";

const DATE_STRATEGY_PROPERTY = "date_strategy";

const DATE_STRATEGY_TIMESTAMP = "timestamp";

const DATE_STRATEGY_STRING = "string";

const DEFAULT_DATE_STRATEGY = self::DATE_STRATEGY_TIMESTAMP;

const DATE_FORMAT_PROPERTY = "date_format";

const DEFAULT_DATE_FORMAT = "Y-m-d H:i:s"; // default mysql format

protected $properties = array();

/**
Expand Down
6 changes: 6 additions & 0 deletions test/parsers/json/WriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ public function testParse() {
$output = new StringOutputStream();
Json::encode(new SimpleBean(), $output, (new Configuration())->push(Configuration::INCLUDE_CLASS_NAME_PROPERTY, "false")->push(Configuration::EXCLUDE_NULL_PROPERTY, "false"));
$this->assertEquals("{\"val\":\"123\"}", $output->toString(), "Should be 123");

$date = new DateTime('now');
$timestamp = $date->getTimestamp();
$output = new StringOutputStream();
Json::encode(array("date" => $date), $output);
$this->assertEquals("{\"date\":$timestamp}", $output->toString(), "Should be $timestamp");
}
}

Expand Down

0 comments on commit 9e2b84e

Please sign in to comment.