Skip to content

Commit

Permalink
Breaking changes: Enhance getModelProperty()
Browse files Browse the repository at this point in the history
- Convert the incoming property parameter into a valid property key
- Prefer to call a property getter method over calling `_getProperty()`
  • Loading branch information
cundd committed Feb 21, 2019
1 parent 8d6be0d commit 53ce98c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
36 changes: 31 additions & 5 deletions Classes/DataProvider/DataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,17 +162,29 @@ public function createModel(array $data, ResourceType $resourceType)
return $model;
}

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

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

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

if ($model instanceof DomainObjectInterface) {
$value = $model->_getProperty($propertyKey);
if (null !== $value) {
return $this->getModelData($value);
} else {
return $this->getModelData($model->_getProperty($propertyParameter));
}
}

return null;
}

Expand Down Expand Up @@ -245,6 +257,20 @@ protected function getUidOfModelWithIdentityForResourceType($identifier, Resourc
return $model ? $model->getUid() : null;
}

/**
* Convert incoming property parameter names into property keys
*
* Example:
* 'dog-name' => 'dogName'
*
* @param string $propertyParameter
* @return string
*/
protected function convertPropertyParameterToKey(string $propertyParameter): string
{
return str_replace(' ', '', ucwords(str_replace(['_', '-'], ' ', $propertyParameter)));
}

/**
* Return the configuration for property mapping
*
Expand Down
4 changes: 2 additions & 2 deletions Classes/DataProvider/DataProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ public function getModelData($model);
* Return the property data from the given Model
*
* @param object|DomainObjectInterface $model
* @param string $propertyKey
* @param string $propertyParameter
* @return mixed
*/
public function getModelProperty($model, $propertyKey);
public function getModelProperty($model, string $propertyParameter);

/**
* Add or update the given Model in the repository
Expand Down
6 changes: 5 additions & 1 deletion Classes/DataProvider/VirtualObjectDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,17 @@ public function getModelData($model)
return $properties;
}

public function getModelProperty($model, $propertyKey)
public function getModelProperty($model, string $propertyParameter)
{
/** @var VirtualObject $model */
$modelData = $model->getData();
$propertyKey = $this->convertPropertyParameterToKey($propertyParameter);
if (isset($modelData[$propertyKey])) {
return $modelData[$propertyKey];
}
if (isset($modelData[$propertyParameter])) {
return $modelData[$propertyParameter];
}

return null;
}
Expand Down

0 comments on commit 53ce98c

Please sign in to comment.