Skip to content

Commit

Permalink
Enhance the Data Provider implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
cundd committed Nov 15, 2018
1 parent 070d375 commit 9027618
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 28 deletions.
16 changes: 12 additions & 4 deletions Classes/DataProvider/DataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class DataProvider implements DataProviderInterface, ClassLoadingInterface, Sing
/**
* @var IdentityProviderInterface
*/
private $identityProvider;
protected $identityProvider;

/**
* Data Provider constructor
Expand Down Expand Up @@ -144,8 +144,7 @@ public function createModel(array $data, ResourceType $resourceType)
return $this->getEmptyModelForResourceType($resourceType);
}

// It is possible to insert Models with a defined UID
// If a UID is given save and remove it from the data array
// It is **not** allowed to insert Models with a defined UID
if (isset($data['__identity']) && $data['__identity']) {
return new \UnexpectedValueException('Invalid property "__identity"');
} elseif (isset($data['uid']) && $data['uid']) {
Expand All @@ -160,7 +159,16 @@ public function createModel(array $data, ResourceType $resourceType)

public function getModelProperty($model, $propertyKey)
{
return $this->getModelData($model->_getProperty($propertyKey));
if ($model instanceof DomainObjectInterface) {
return $this->getModelData($model->_getProperty($propertyKey));
}

$getter = 'get' . ucfirst($propertyKey);
if (is_callable([$model, $getter])) {
return $this->getModelData($model->$getter());
}

return null;
}

public function saveModel($model, ResourceType $resourceType)
Expand Down
4 changes: 3 additions & 1 deletion Classes/DataProvider/DataProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ public function fetchModel($identifier, ResourceType $resourceType);
/**
* Create a new Domain Model with the given data
*
* Even if the data contains an identifier, the existing model will **not** be loaded
* Implementations are free to decide if identifiers are accepted (e.g. an exception will be thrown for Extbase
* Models if the property `uid` or `__identity` is given. `Virtual Objects` on the other hand accept identifier
* properties)
*
* @param array $data Data of the new model
* @param ResourceType $resourceType API resource type to get the repository for
Expand Down
24 changes: 1 addition & 23 deletions Classes/DataProvider/VirtualObjectDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,29 +83,7 @@ public function createModel(array $data, ResourceType $resourceType)
return $this->getEmptyModelForResourceType($resourceType);
}

// It is possible to insert Models with a defined UID
// If a UID is given save and remove it from the data array
$uid = null;
//if (isset($data['__identity']) && $data['__identity']) {
// // Load the UID of the existing model
// $uid = $this->getUidOfModelWithIdentityForResourceType($data['__identity'], $resourceType);
//} elseif (isset($data['uid']) && $data['uid']) {
// $uid = $data['uid'];
//}
//if ($uid) {
// unset($data['__identity']);
// unset($data['uid']);
//}

// Get a fresh model
$model = $this->convertIntoModel($data, $resourceType);

if ($uid !== null) {
// Set the saved identifier
$model->_setProperty('uid', $uid);
}

return $model;
return $this->convertIntoModel($data, $resourceType);
}

public function getRepositoryClassForResourceType(ResourceType $resourceType)
Expand Down

0 comments on commit 9027618

Please sign in to comment.