Skip to content
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
15 changes: 12 additions & 3 deletions src/Helpers/DataAttributesHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down
11 changes: 8 additions & 3 deletions src/Helpers/DataLinksHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 => []];

Expand Down Expand Up @@ -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;
}
Expand All @@ -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];
Expand Down
4 changes: 2 additions & 2 deletions src/JsonApiTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
),
];

Expand Down