/**
  * @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) {
         $keyName = self::transformToValidMemberName(RecursiveFormatterHelper::camelCaseToUnderscore($propertyName));
         if (\in_array($propertyName, $idProperties, true)) {
             self::addIdPropertiesInAttribute($mappings, $type, $keyName, $value, $attributes);
             continue;
         }
         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 (false === self::hasClassIdentifierKey($value)) {
                 $attributes[$keyName] = $value;
             }
         }
     }
     //Guarantee it always returns the same attribute order. No matter what.
     ksort($attributes, SORT_STRING);
     return [JsonApiTransformer::ATTRIBUTES_KEY => $attributes];
 }
예제 #2
0
 /**
  * @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));
 }
예제 #3
0
 /**
  * @param \NilPortugues\Api\Mapping\Mapping[] $mappings
  * @param array                               $parent
  * @param string                              $propertyName
  * @param string                              $type
  * @param array                               $data
  * @param array                               $value
  */
 protected static function relationshipLinksSelf(array &$mappings, array &$parent, $propertyName, $type, array &$data, array &$value)
 {
     if (!in_array($propertyName, RecursiveFormatterHelper::getIdProperties($mappings, $type), true)) {
         $data[JsonApiTransformer::RELATIONSHIPS_KEY][$propertyName] = array_merge(array_filter([JsonApiTransformer::LINKS_KEY => self::setResponseDataRelationshipSelfLinks($propertyName, $mappings, $parent)]), array_filter([JsonApiTransformer::DATA_KEY => PropertyHelper::setResponseDataTypeAndId($mappings, $value)]));
         if (count($data[JsonApiTransformer::RELATIONSHIPS_KEY][$propertyName]) === 0) {
             unset($data[JsonApiTransformer::RELATIONSHIPS_KEY][$propertyName]);
         }
     }
 }