Skip to content

Timezone support for JSON objects #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: v1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Attributes/JsonItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ class JsonItem
* @param string $dateFormat - Only used if the actual property type is a Date. This will specify the format it should be converted to in the json string.
* @param bool $included - If included is false, JSON strings from the decorated object will not include this property.
* @param bool $omitEmpty - If omitEmpty is true, JSON strings from the decorated object will skip this property if an empty string or null are detected.
* @param string|null $timezoneId
*/
public function __construct(
public ?string $name = null,
public ?string $arrayOrCollectionType = null,
public string $dateFormat = Date::ATOM,
public bool $included = true,
public bool $omitEmpty = false
public bool $omitEmpty = false,
public ?string $timezoneId = null
) {
}
}
4 changes: 2 additions & 2 deletions FeastTests/JsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ public function testMarshal(): void
$item->fourthTimestamp = DateTime::createFromFormat('U', '1617619260');
$item->thirdTimestamp->setTimezone(new DateTimeZone('America/New_York'));
$item->fourthTimestamp->setTimezone(new DateTimeZone('America/New_York'));

$item->timezoneId = 'America/New_York';
$data = Json::marshal($item);
$this->assertEquals(
'{"first_name":"FEAST","last_name":"Framework","test_item":{"first_name":"Jeremy","last_name":"Presutti","calls":4,"moreCalls":5},"second_item":{"also_first_name":"Orlando","also_last_name":"Florida"},"items":[{"first_name":"PHP","last_name":"7.4","calls":null,"stringCalls":"test"},{"first_name":"PHP","last_name":"8.0","calls":null,"moreCalls":3,"stringCalls":"other test"}],"cards":["4",5,["6"]],"otherItems":{"first":{"first_name":"Json","last_name":"Serializer","calls":null},"second":{"first_name":"Item","last_name":"Parsing","calls":null}},"thirdItems":{"test":"theTest","test2":"theTest2"},"otherSet":[{"first_name":"Json","last_name":"Serializer","calls":null},{"first_name":"Item","last_name":"Parsing","calls":null}],"thirdSet":["theTest","theTest2"],"calls":null,"count":4,"aClass":{"test":"ItWorks"},"timestamp":"20210415","otherTimestamp":"2021-04-05T06:41:00-04:00","thirdTimestamp":"2021-04-15T20:56:24-04:00","fourthTimestamp":"20210405"}',
'{"first_name":"FEAST","last_name":"Framework","test_item":{"first_name":"Jeremy","last_name":"Presutti","calls":4,"moreCalls":5},"second_item":{"also_first_name":"Orlando","also_last_name":"Florida"},"items":[{"first_name":"PHP","last_name":"7.4","calls":null,"stringCalls":"test"},{"first_name":"PHP","last_name":"8.0","calls":null,"moreCalls":3,"stringCalls":"other test"}],"cards":["4",5,["6"]],"otherItems":{"first":{"first_name":"Json","last_name":"Serializer","calls":null},"second":{"first_name":"Item","last_name":"Parsing","calls":null}},"thirdItems":{"test":"theTest","test2":"theTest2"},"otherSet":[{"first_name":"Json","last_name":"Serializer","calls":null},{"first_name":"Item","last_name":"Parsing","calls":null}],"thirdSet":["theTest","theTest2"],"calls":null,"count":4,"aClass":{"test":"ItWorks"},"timestamp":"20210415","otherTimestamp":"2021-04-05T06:41:00-04:00","thirdTimestamp":"2021-04-15T20:56:24-04:00","fourthTimestamp":"20210405","timezoneId":"America\/New_York"}',
$data
);
}
Expand Down
3 changes: 3 additions & 0 deletions FeastTests/Mocks/TestJsonItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ class TestJsonItem

#[JsonItem(dateFormat: 'Ymd')]
public DateTime $fourthTimestamp;

#[JsonItem(timezoneId: 'America/New_York')]
public string $timezoneId;

#[JsonItem(included: false)]
public string $notIncluded;
Expand Down
5 changes: 4 additions & 1 deletion Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ protected static function getClassParamInfo(
$dateFormat = Date::ATOM;
$included = true;
$omitEmpty = false;
$timezoneId = null;
$attributes = $property->getAttributes(JsonItem::class);
foreach ($attributes as $attribute) {
/** @var JsonItem $attributeObject */
Expand All @@ -201,13 +202,15 @@ protected static function getClassParamInfo(
$dateFormat = $attributeObject->dateFormat;
$included = $attributeObject->included;
$omitEmpty = $attributeObject->omitEmpty;
$timezoneId = $attributeObject->timezoneId;
}
$return[$property->getName()] = [
'name' => $name,
'type' => $type,
'dateFormat' => $dateFormat,
'included' => $included,
'omitEmpty' => $omitEmpty
'omitEmpty' => $omitEmpty,
'timezoneId' => $timezoneId
];
}
return $return;
Expand Down