Skip to content

Commit

Permalink
[TASK] Simplify Node::getProperty()
Browse files Browse the repository at this point in the history
Releases: master
  • Loading branch information
kitsunet committed Nov 22, 2015
1 parent 38e4f9a commit 87804e1
Showing 1 changed file with 35 additions and 25 deletions.
60 changes: 35 additions & 25 deletions TYPO3.TYPO3CR/Classes/TYPO3/TYPO3CR/Domain/Model/Node.php
Expand Up @@ -814,43 +814,53 @@ public function hasProperty($propertyName)
* there if it is gettable.
*
* @param string $propertyName Name of the property
* @param boolean $returnNodesAsIdentifiers If enabled, references to nodes are returned as node identifiers instead of NodeData objects
* @param boolean $returnNodesAsIdentifiers If enabled, references to nodes are returned as node identifiers instead of NodeInterface instances
* @return mixed value of the property
* @api
*/
public function getProperty($propertyName, $returnNodesAsIdentifiers = false)
{
$value = $this->nodeData->getProperty($propertyName);
if (!empty($value)) {
if (empty($value)) {
return $value;
}

$nodeType = $this->getNodeType();
if ($nodeType->hasConfiguration('properties.' . $propertyName)) {
if (!$nodeType->hasConfiguration('properties.' . $propertyName)) {
return $value;
}

$expectedPropertyType = $nodeType->getPropertyType($propertyName);
switch ($expectedPropertyType) {
case 'references' :
if ($returnNodesAsIdentifiers === false) {
$nodes = array();
foreach ($value as $nodeIdentifier) {
$node = $this->context->getNodeByIdentifier($nodeIdentifier);
// $node can be NULL if the node is not visible according to the current content context:
if ($node !== null) {
$nodes[] = $node;
if ($expectedPropertyType === 'references') {
return $this->resolvePropertyReferences($returnNodesAsIdentifiers, $value);
}

if ($expectedPropertyType === 'reference') {
return ($returnNodesAsIdentifiers ? $value : $this->context->getNodeByIdentifier($value));
}
$value = $nodes;

return $this->propertyMapper->convert($value, $expectedPropertyType);
}
break;
case 'reference' :
if ($returnNodesAsIdentifiers === false) {
$value = $this->context->getNodeByIdentifier($value);

/**
* Maps the property value (an array of node identifiers) to the Node objects if needed.
*
* @param boolean $returnNodesAsIdentifiers
* @param array $value
* @return array
*/
protected function resolvePropertyReferences($returnNodesAsIdentifiers, $value = []) {
if ($returnNodesAsIdentifiers) {
return $value;
}
break;
default:
$value = $this->propertyMapper->convert($value, $expectedPropertyType);
break;
}
}
}
return $value;

$nodes = array_map(function($nodeIdentifier) {
return $this->context->getNodeByIdentifier($nodeIdentifier);
}, $value);

return array_filter($nodes, function($node) {
return ($node !== NULL);
});
}

/**
Expand Down

0 comments on commit 87804e1

Please sign in to comment.