/** * @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]; }
/** * @param \NilPortugues\Api\Mapping\Mapping[] $mappings * @param string $propertyName * @param string $type * * @return bool */ public static function isAttributeProperty(array &$mappings, $propertyName, $type) { return Serializer::CLASS_IDENTIFIER_KEY !== $propertyName && !in_array($propertyName, RecursiveFormatterHelper::getIdProperties($mappings, $type)); }
/** * @param array $idProperties * @param array $idValues */ protected static function removeArraysFromKeyValueReplacement(array &$idProperties, array &$idValues) { RecursiveDeleteHelper::deleteKeys($idValues, [Serializer::CLASS_IDENTIFIER_KEY]); RecursiveFormatterHelper::flattenObjectsWithSingleKeyScalars($idValues); if (is_array($idValues)) { foreach ($idValues as $key => $value) { if (is_array($value)) { unset($idProperties[$key]); unset($idValues[$key]); } } } }
/** * @param array $copy * @param string $type * * @return array */ protected function getResponseAdditionalLinks(array $copy, string $type) : array { $replacedUrls = []; if (\is_scalar($type) && !empty($this->mappings[$type])) { $otherUrls = $this->mappings[$type]->getUrls(); list($idValues, $idProperties) = RecursiveFormatterHelper::getIdPropertyAndValues($this->mappings, $copy, $type); $replacedUrls = \str_replace($idProperties, $idValues, $otherUrls); foreach ($replacedUrls as $key => $value) { if ($otherUrls[$key] === $value && false !== strpos($value, '{')) { unset($replacedUrls[$key]); } } } return $replacedUrls; }
/** * Changes all array keys to under_score format using recursion. * * @param array $array */ protected static function recursiveSetKeysToUnderScore(array &$array) { $newArray = []; foreach ($array as $key => &$value) { $underscoreKey = RecursiveFormatterHelper::camelCaseToUnderscore($key); $newArray[$underscoreKey] = $value; if (\is_array($value)) { self::recursiveSetKeysToUnderScore($newArray[$underscoreKey]); } } $array = $newArray; }
/** * @param array $response * * @return array */ protected function buildSelfLink(array &$response) { $link = []; if (!empty($type = $response[Serializer::CLASS_IDENTIFIER_KEY])) { list($idValues, $idProperties) = RecursiveFormatterHelper::getIdPropertyAndValues($this->mappings, $response, $type); $href = self::buildUrl($this->mappings, $idProperties, $idValues, $this->mappings[$type]->getResourceUrl(), $type); if ($href != $this->mappings[$type]->getResourceUrl()) { $link[self::SELF_LINK] = $href; } } return $link; }
/** * @param array $data * * @return array */ protected function postSerialization(array &$data) { RecursiveDeleteHelper::deleteKeys($data, [Serializer::CLASS_IDENTIFIER_KEY]); RecursiveDeleteHelper::deleteKeys($data, [Serializer::MAP_TYPE]); RecursiveFormatterHelper::formatScalarValues($data); AttributeFormatterHelper::flattenObjectsWithSingleKeyScalars($data); $this->recursiveSetKeysToUnderScore($data); $this->setResponseMeta($data); return $data; }
/** * @param string $aliasedClass * * @return $this */ public function setClassAlias(string $aliasedClass) { $this->classAlias = RecursiveFormatterHelper::camelCaseToUnderscore(RecursiveFormatterHelper::namespaceAsArrayKey($aliasedClass)); return $this; }
/** * @param array $value * @param array $data */ protected function setResponseLinks(array $value, array &$data) { $data[self::LINKS_KEY] = \array_filter(\array_merge($this->addHrefToLinks($this->buildLinks()), !empty($data[self::LINKS_KEY]) ? $data[self::LINKS_KEY] : [])); if (!empty($value[Serializer::CLASS_IDENTIFIER_KEY])) { $type = $value[Serializer::CLASS_IDENTIFIER_KEY]; if (\is_scalar($type) && !empty($this->mappings[$type])) { $urls = $this->mappings[$type]->getUrls(); $data[self::LINKS_KEY] = \array_filter(\array_merge(empty($data[self::LINKS_KEY]) ? [] : $data[self::LINKS_KEY], !empty($urls) ? $this->addHrefToLinks($this->getResponseAdditionalLinks($value, $type)) : [])); /* * Adds the _links:self:href link to the response. */ list($idValues, $idProperties) = RecursiveFormatterHelper::getIdPropertyAndValues($this->mappings, $value, $type); $href = DataLinksHelper::buildUrl($this->mappings, $idProperties, $idValues, $this->mappings[$type]->getResourceUrl(), $type); if ($href != $this->mappings[$type]->getResourceUrl()) { $data[self::LINKS_KEY][self::SELF_LINK][self::LINKS_HREF] = $href; } } } if (empty($data[self::LINKS_KEY])) { unset($data[self::LINKS_KEY]); } }