Ejemplo n.º 1
0
 /**
  * @param \NilPortugues\Api\Mapping\Mapping[] $mappings
  * @param string                              $type
  * @param string                              $keyName
  * @param array                               $value
  * @param array                               $attributes
  */
 protected static function addIdPropertiesInAttribute(array &$mappings, $type, $keyName, array $value, array &$attributes)
 {
     $keepKeys = str_replace(array_values($mappings[$type]->getAliasedProperties()), array_keys($mappings[$type]->getAliasedProperties()), $mappings[$type]->getFilterKeys());
     $keepIdKeys = 0 === count($keepKeys);
     if (false !== array_search($keyName, $keepKeys, true)) {
         $keepIdKeys = false;
     }
     if ($keepIdKeys) {
         $ids = PropertyHelper::getIdValues($mappings, $value, $type);
         if (count($ids) > 0) {
             if (1 === count($ids)) {
                 $ids = array_pop($ids);
             }
             $attributes[$keyName] = $ids;
         } else {
             RecursiveFormatterHelper::formatScalarValues($value);
             $attributes[$keyName] = $value;
         }
     }
 }
Ejemplo n.º 2
0
 /**
  * @param string                              $propertyName
  * @param \NilPortugues\Api\Mapping\Mapping[] $mappings
  * @param array                               $parent
  * @param array                               $data
  */
 protected static function relationshipLinksRelated($propertyName, array &$mappings, array &$parent, array &$data)
 {
     if (!empty($parent[Serializer::CLASS_IDENTIFIER_KEY]) && !empty($data[JsonApiTransformer::RELATIONSHIPS_KEY][$propertyName])) {
         $parentType = $parent[Serializer::CLASS_IDENTIFIER_KEY];
         if (\is_scalar($parentType)) {
             if (!empty($mappings[$parentType]) && !empty($relatedUrl = $mappings[$parentType]->getRelatedUrl($propertyName))) {
                 $copy = $parent;
                 RecursiveFormatterHelper::formatScalarValues($copy);
                 if (!empty($copy[Serializer::CLASS_IDENTIFIER_KEY])) {
                     unset($copy[Serializer::CLASS_IDENTIFIER_KEY]);
                 }
                 $idProperties = array_keys($copy);
                 foreach ($idProperties as &$property) {
                     $property = sprintf('{%s}', $property);
                 }
                 $idValues = array_values($copy);
                 $url = self::buildUrl($mappings, $idProperties, $idValues, $relatedUrl, $parentType);
                 if ($url !== $relatedUrl) {
                     $data[JsonApiTransformer::RELATIONSHIPS_KEY][$propertyName][JsonApiTransformer::LINKS_KEY][JsonApiTransformer::RELATED_LINK][JsonApiTransformer::LINKS_HREF] = $url;
                 }
             }
         }
     }
 }
Ejemplo n.º 3
0
 /**
  * @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;
 }