/** * @param \NilPortugues\Api\Mapping\Mapping[] $mappings * @param array $array * @param array $parent * * @return array */ public static function setResponseDataRelationship(array &$mappings, array &$array, array $parent) { $data = [JsonApiTransformer::RELATIONSHIPS_KEY => []]; foreach ($array as $propertyName => $value) { if (\is_array($value)) { self::addRelationshipData($mappings, $parent, $value, $propertyName, $data); if (\array_key_exists(Serializer::MAP_TYPE, $value)) { $newData = []; foreach ($value[Serializer::SCALAR_VALUE] as $d) { self::addRelationshipData($mappings, $parent, $d, $propertyName, $newData); if (!empty($d[Serializer::CLASS_IDENTIFIER_KEY])) { $type = $d[Serializer::CLASS_IDENTIFIER_KEY]; if (!in_array($propertyName, RecursiveFormatterHelper::getIdProperties($mappings, $type), true)) { $parentType = $d[Serializer::CLASS_IDENTIFIER_KEY]; list($idValues, $idProperties) = RecursiveFormatterHelper::getIdPropertyAndValues($mappings, $d, $parentType); $selfLink = $mappings[$parentType]->getRelationshipSelfUrl($propertyName); if (!empty($selfLink)) { $href = \str_replace($idProperties, $idValues, $selfLink); if ($selfLink != $href) { $propertyNameKey = DataAttributesHelper::transformToValidMemberName($propertyName); $propertyNameKey = self::camelCaseToUnderscore($propertyNameKey); $newData[JsonApiTransformer::RELATIONSHIPS_KEY][$propertyNameKey][JsonApiTransformer::LINKS_KEY][JsonApiTransformer::SELF_LINK][JsonApiTransformer::LINKS_HREF] = $href; } } } } if (!empty($newData[JsonApiTransformer::RELATIONSHIPS_KEY][$propertyName])) { $propertyNameKey = DataAttributesHelper::transformToValidMemberName($propertyName); $propertyNameKey = self::camelCaseToUnderscore($propertyNameKey); if (!empty($d[Serializer::CLASS_IDENTIFIER_KEY])) { $type = $d[Serializer::CLASS_IDENTIFIER_KEY]; $parentType = $parent[Serializer::CLASS_IDENTIFIER_KEY]; //Removes relationships related to the current resource if filtering include resources has been set. if ($mappings[$parentType]->isFilteringIncludedResources()) { foreach ($newData[JsonApiTransformer::RELATIONSHIPS_KEY][$propertyName] as $position => $includedResource) { if (false === in_array($type, $mappings[$parentType]->getIncludedResources(), true)) { unset($newData[JsonApiTransformer::RELATIONSHIPS_KEY][$propertyName][$position]); } } } $newData[JsonApiTransformer::RELATIONSHIPS_KEY][$propertyName] = array_filter($newData[JsonApiTransformer::RELATIONSHIPS_KEY][$propertyName]); if (!empty($newData[JsonApiTransformer::RELATIONSHIPS_KEY][$propertyName])) { $data[JsonApiTransformer::RELATIONSHIPS_KEY][$propertyNameKey][] = $newData[JsonApiTransformer::RELATIONSHIPS_KEY][$propertyName]; } } } } } } } return (array) \array_filter($data); }
/** * @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; }
/** * @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 */ protected function setResponseLinks(array &$data) { if (!empty($type = $data[Serializer::CLASS_IDENTIFIER_KEY])) { $data[self::LINKS_KEY] = \array_merge(CuriesHelper::buildCuries($this->curies), $this->addHrefToLinks($this->buildLinks()), !empty($data[self::LINKS_KEY]) ? $data[self::LINKS_KEY] : [], $this->addHrefToLinks($this->getResponseAdditionalLinks($data, $data[Serializer::CLASS_IDENTIFIER_KEY]))); /* * Adds the _links:self:href link to the response. */ if (!empty($type = $data[Serializer::CLASS_IDENTIFIER_KEY])) { list($idValues, $idProperties) = RecursiveFormatterHelper::getIdPropertyAndValues($this->mappings, $data, $type); $href = self::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; } } $data[self::LINKS_KEY] = \array_filter($data[self::LINKS_KEY]); if (empty($data[self::LINKS_KEY])) { unset($data[self::LINKS_KEY]); } } }
/** * @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]); } }