diff --git a/Attributes/JsonItem.php b/Attributes/JsonItem.php index 0e5b5bb..1d980cf 100644 --- a/Attributes/JsonItem.php +++ b/Attributes/JsonItem.php @@ -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 ) { } } diff --git a/FeastTests/JsonTest.php b/FeastTests/JsonTest.php index 98df9c7..d6f97b6 100644 --- a/FeastTests/JsonTest.php +++ b/FeastTests/JsonTest.php @@ -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 ); } diff --git a/FeastTests/Mocks/TestJsonItem.php b/FeastTests/Mocks/TestJsonItem.php index 2efa0c8..7ebb5cf 100644 --- a/FeastTests/Mocks/TestJsonItem.php +++ b/FeastTests/Mocks/TestJsonItem.php @@ -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; diff --git a/Json.php b/Json.php index 6cf11f1..ab974f5 100644 --- a/Json.php +++ b/Json.php @@ -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 */ @@ -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;