From 65e8ea9a4e6c56f56f9506edbf2e88fae5a3ce5e Mon Sep 17 00:00:00 2001 From: m1n0 Date: Thu, 9 Feb 2017 20:17:21 +0100 Subject: [PATCH 1/3] Make attributes case configurable. --- src/Helpers/DataAttributesHelper.php | 9 +++++++-- src/JsonApiTransformer.php | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Helpers/DataAttributesHelper.php b/src/Helpers/DataAttributesHelper.php index 68c0ac8..853ac95 100644 --- a/src/Helpers/DataAttributesHelper.php +++ b/src/Helpers/DataAttributesHelper.php @@ -72,17 +72,22 @@ class DataAttributesHelper /** * @param \NilPortugues\Api\Mapping\Mapping[] $mappings * @param array $array + * @param string $attributesCase * * @return array */ - public static function setResponseDataAttributes(array &$mappings, array &$array) + public static function setResponseDataAttributes(array &$mappings, array &$array, string $attributesCase) { $attributes = []; $type = $array[Serializer::CLASS_IDENTIFIER_KEY]; $idProperties = RecursiveFormatterHelper::getIdProperties($mappings, $type); foreach ($array as $propertyName => $value) { - $keyName = self::transformToValidMemberName(RecursiveFormatterHelper::camelCaseToUnderscore($propertyName)); + if ($attributesCase == 'snake_case') { + $propertyName = RecursiveFormatterHelper::camelCaseToUnderscore($propertyName); + } + + $keyName = self::transformToValidMemberName($propertyName); if (\in_array($propertyName, $idProperties, true)) { self::addIdPropertiesInAttribute($mappings, $type, $keyName, $value, $attributes); diff --git a/src/JsonApiTransformer.php b/src/JsonApiTransformer.php index 03ddffe..ff0f8a4 100644 --- a/src/JsonApiTransformer.php +++ b/src/JsonApiTransformer.php @@ -96,7 +96,7 @@ protected function serialization(array &$value) $data = [ self::DATA_KEY => \array_merge( PropertyHelper::setResponseDataTypeAndId($this->mappings, $value), - DataAttributesHelper::setResponseDataAttributes($this->mappings, $value), + DataAttributesHelper::setResponseDataAttributes($this->mappings, $value, $this->attributesCase), DataLinksHelper::setResponseDataLinks($this->mappings, $value), DataLinksHelper::setResponseDataRelationship($this->mappings, $value, $value) ), From b3c9ef7871302ff16516f78bd4cd42152fdcbecb Mon Sep 17 00:00:00 2001 From: m1n0 Date: Fri, 3 Mar 2017 09:17:17 +0100 Subject: [PATCH 2/3] Apply the attributes case to relationships as well. --- src/Helpers/DataLinksHelper.php | 11 ++++++++--- src/JsonApiTransformer.php | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Helpers/DataLinksHelper.php b/src/Helpers/DataLinksHelper.php index 1228ea0..8c10cdd 100644 --- a/src/Helpers/DataLinksHelper.php +++ b/src/Helpers/DataLinksHelper.php @@ -80,7 +80,7 @@ public static function setResponseDataLinks(array &$mappings, array &$value) * * @return array */ - public static function setResponseDataRelationship(array &$mappings, array &$array, array $parent) + public static function setResponseDataRelationship(array &$mappings, array &$array, array $parent, string $attributesCase) { $data = [JsonApiTransformer::RELATIONSHIPS_KEY => []]; @@ -109,7 +109,10 @@ public static function setResponseDataRelationship(array &$mappings, array &$arr $href = \str_replace($idProperties, $idValues, $selfLink); if ($selfLink != $href) { $propertyNameKey = DataAttributesHelper::transformToValidMemberName($propertyName); - $propertyNameKey = self::camelCaseToUnderscore($propertyNameKey); + + if ($attributesCase == 'snake_case') { + $propertyNameKey = RecursiveFormatterHelper::camelCaseToUnderscore($propertyNameKey); + } $newData[JsonApiTransformer::RELATIONSHIPS_KEY][$propertyNameKey][JsonApiTransformer::LINKS_KEY][JsonApiTransformer::SELF_LINK][JsonApiTransformer::LINKS_HREF] = $href; } @@ -119,7 +122,9 @@ public static function setResponseDataRelationship(array &$mappings, array &$arr if (!empty($newData[JsonApiTransformer::RELATIONSHIPS_KEY][$propertyName])) { $propertyNameKey = DataAttributesHelper::transformToValidMemberName($propertyName); - $propertyNameKey = self::camelCaseToUnderscore($propertyNameKey); + if ($attributesCase == 'snake_case') { + $propertyNameKey = RecursiveFormatterHelper::camelCaseToUnderscore($propertyNameKey); + } if (!empty($d[Serializer::CLASS_IDENTIFIER_KEY])) { $type = $d[Serializer::CLASS_IDENTIFIER_KEY]; diff --git a/src/JsonApiTransformer.php b/src/JsonApiTransformer.php index ff0f8a4..850284a 100644 --- a/src/JsonApiTransformer.php +++ b/src/JsonApiTransformer.php @@ -98,7 +98,7 @@ protected function serialization(array &$value) PropertyHelper::setResponseDataTypeAndId($this->mappings, $value), DataAttributesHelper::setResponseDataAttributes($this->mappings, $value, $this->attributesCase), DataLinksHelper::setResponseDataLinks($this->mappings, $value), - DataLinksHelper::setResponseDataRelationship($this->mappings, $value, $value) + DataLinksHelper::setResponseDataRelationship($this->mappings, $value, $value, $this->attributesCase) ), ]; From 65b9debaa0f48a829bc51442186502b2880840cc Mon Sep 17 00:00:00 2001 From: m1n0 Date: Fri, 26 May 2017 11:10:07 +0200 Subject: [PATCH 3/3] Configurable attributes case for indented arrays and objects as well --- src/Helpers/DataAttributesHelper.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Helpers/DataAttributesHelper.php b/src/Helpers/DataAttributesHelper.php index 853ac95..d76b658 100644 --- a/src/Helpers/DataAttributesHelper.php +++ b/src/Helpers/DataAttributesHelper.php @@ -98,7 +98,11 @@ public static function setResponseDataAttributes(array &$mappings, array &$array && empty($mappings[$value[Serializer::CLASS_IDENTIFIER_KEY]]) ) { $copy = $value; - self::recursiveSetKeysToUnderScore($copy); + + if ($attributesCase == 'snake_case') { + self::recursiveSetKeysToUnderScore($copy); + } + $attributes[$keyName] = $copy; continue; }