Skip to content

Instantly share code, notes, and snippets.

@johannessteu
Created December 5, 2014 13:07
Show Gist options
  • Save johannessteu/f2de7db70ad0356a6315 to your computer and use it in GitHub Desktop.
Save johannessteu/f2de7db70ad0356a6315 to your computer and use it in GitHub Desktop.
Convert a Node to Model
class AbstractProductConverter extends AbstractTypeConverter {
/**
* @var PropertyMapper
* @Flow\Inject
*/
protected $propertyMapper;
/**
* {@inheritDoc}
*/
protected $targetType = 'Vendor\Package\Domain\Model\Foo';
/**
* {@inheritDoc}
*/
protected $sourceTypes = array('string');
/**
*
*/
protected $priority = 200;
/**
* @Flow\Inject
* @var NodeDataRepository
*/
protected $nodeDataRepository;
/**
* {@inheritDoc}
*/
public function canConvertFrom($source, $targetType) {
/** @var NodeInterface $node */
$node = $this->propertyMapper->convert($source, 'TYPO3\TYPO3CR\Domain\Model\NodeInterface');
if ($node instanceof NodeInterface && $this->getNodeTypeFromNode($node) != NULL) {
return TRUE;
} else {
return FALSE;
}
}
/**
* @param NodeInterface $node
* @return bool
*/
public function getNodeTypeFromNode(NodeInterface $node) {
switch($node->getNodeType()->getName()) {
case "Vendor.Package:Foo":
return 'Vendor\Package\Domain\Model\Foo';
default:
return NULL;
}
}
/**
* {@inheritDoc}
*/
public function convertFrom($source, $targetType, array $convertedChildProperties = array(), \TYPO3\Flow\Property\PropertyMappingConfigurationInterface $configuration = NULL) {
/** @var NodeInterface $node */
$node = $this->propertyMapper->convert($source, 'TYPO3\TYPO3CR\Domain\Model\NodeInterface');
$targetType = $this->getNodeTypeFromNode($node);
$source = $node->getProperties();
$source['options'] = array();
$source['nodePath'] = $node->getPath();
$product = $this->propertyMapper->convert($source, $targetType, $configuration);
return $product;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment