diff --git a/src/Helpers/DataAttributesHelper.php b/src/Helpers/DataAttributesHelper.php index 68c0ac8..d76b658 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); @@ -93,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; } 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 03ddffe..850284a 100644 --- a/src/JsonApiTransformer.php +++ b/src/JsonApiTransformer.php @@ -96,9 +96,9 @@ 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) + DataLinksHelper::setResponseDataRelationship($this->mappings, $value, $value, $this->attributesCase) ), ];