/** * @param \NilPortugues\Api\Mapping\Mapping[] $mappings * @param array $array * * @return array */ public static function setResponseDataAttributes(array &$mappings, array &$array) { $attributes = []; $type = $array[Serializer::CLASS_IDENTIFIER_KEY]; $idProperties = RecursiveFormatterHelper::getIdProperties($mappings, $type); foreach ($array as $propertyName => $value) { if (\in_array($propertyName, $idProperties, true)) { continue; } $keyName = self::transformToValidMemberName(RecursiveFormatterHelper::camelCaseToUnderscore($propertyName)); if (!empty($value[Serializer::CLASS_IDENTIFIER_KEY]) && empty($mappings[$value[Serializer::CLASS_IDENTIFIER_KEY]])) { $copy = $value; self::recursiveSetKeysToUnderScore($copy); $attributes[$keyName] = $copy; continue; } if (self::isScalarValue($value) && empty($mappings[$value[Serializer::SCALAR_TYPE]])) { $attributes[$keyName] = $value; continue; } if (\is_array($value) && !array_key_exists(Serializer::CLASS_IDENTIFIER_KEY, $value)) { if (self::containsClassIdentifierKey($value)) { $attributes[$keyName] = $value; } } } return [JsonApiTransformer::ATTRIBUTES_KEY => $attributes]; }
/** * Changes all array keys to under_score format using recursion. * * @param array $array */ protected function recursiveSetKeysToUnderScore(array &$array) { $newArray = []; foreach ($array as $key => &$value) { $underscoreKey = RecursiveFormatterHelper::camelCaseToUnderscore($key); $newArray[$underscoreKey] = $value; if (\is_array($value)) { $this->recursiveSetKeysToUnderScore($newArray[$underscoreKey]); } } $array = $newArray; }
/** * @param string $aliasedClass * * @return $this */ public function setClassAlias(string $aliasedClass) { $this->classAlias = RecursiveFormatterHelper::camelCaseToUnderscore(RecursiveFormatterHelper::namespaceAsArrayKey($aliasedClass)); return $this; }