/**
  * @param mixed $source
  * @param string $targetType
  * @param array $convertedChildProperties
  * @param PropertyMappingConfigurationInterface|null $configuration
  * @return mixed|\Netlogix\JsonApiOrg\Schema\ResourceInterface|\TYPO3\Flow\Error\Error
  */
 public function convertFrom($source, $targetType, array $convertedChildProperties = array(), PropertyMappingConfigurationInterface $configuration = null)
 {
     if (is_string($source)) {
         $sourceArray = json_decode($source, true);
         $source = is_array($sourceArray) ? $sourceArray : ['id' => $source];
     }
     if (!array_key_exists('type', $source)) {
         $dummyPayload = $this->objectManager->get($targetType);
         $typeIdentifier = $dummyPayload->getType();
         $source['type'] = $this->exposableTypeMap->getType($typeIdentifier);
     }
     if (array_key_exists('id', $source)) {
         $arguments = $source['id'];
     } else {
         $arguments = [];
     }
     $payload = $this->propertyMapper->convert($arguments, $this->exposableTypeMap->getClassName($source['type']));
     $resourceInformation = $this->resourceMapper->findResourceInformation($payload);
     $resource = $resourceInformation->getResource($payload);
     if (isset($source['attributes'])) {
         $attributes = $resource->getAttributes();
         foreach ($source['attributes'] as $fieldName => $value) {
             $attributes[$fieldName] = $value;
         }
     }
     if (isset($source['relationships'])) {
         $relationships = $resource->getRelationships();
         foreach ($source['relationships'] as $fieldName => $value) {
             $relationships[$fieldName] = $value;
         }
     }
     return $resource;
 }
Beispiel #2
0
 /**
  * Returns the resource information with the highest priority being capable
  * of dealing with the given $payload.
  *
  * The controllerContext is added to the resource information object to allow
  * it to create proper URIs.
  *
  * @todo Use runtime cache based on spl_object_hash
  * @param mixed $payload
  * @return \Netlogix\JsonApiOrg\Resource\Information\ResourceInformationInterface
  */
 public function findResourceInformation($payload)
 {
     if (is_array($payload) && isset($payload['type']) && isset($payload['id'])) {
         $payload = $this->propertyMapper->convert((string) $payload['id'], $this->exposableTypeMap->getClassName($payload['type']));
     }
     /** @var ResourceInformationInterface $resourceInformation */
     foreach ($this->resourceInformation as $resourceInformation) {
         if ($resourceInformation->canHandle($payload)) {
             return $resourceInformation;
         }
     }
     return null;
 }
 /**
  * @param $source
  * @return mixed
  */
 protected function convertIdentifierProperties($source)
 {
     $identifier = array_key_exists('id', $source) ? $source['id'] : [];
     return $this->propertyMapper->convert($identifier, $this->exposableTypeMap->getClassName($source['type']));
 }
 /**
  * @param array $relation
  * @return string
  */
 protected function getResourceUriForArrayFormat($relation)
 {
     $resource = $this->propertyMapper->convert((string) $relation['id'], $this->exposableTypeMap->getClassName($relation['type']));
     $resourceInformation = $this->resourceMapper->findResourceInformation($resource);
     return $resourceInformation->getPublicResourceUri($resource);
 }