Created
February 17, 2011 12:35
-
-
Save skurfuerst/831630 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected function determineTypeConverter($source, $targetType, \F3\FLOW3\Property\PropertyMappingConfigurationInterface $configuration) { | |
if ($configuration->getTypeConverter() !== NULL) return $configuration->getTypeConverter(); | |
$sourceType = $this->determineSourceType($source); | |
if (!is_string($targetType)) { | |
throw new \F3\FLOW3\Property\Exception\InvalidTargetException('The target type was no string, but of type "' . gettype($targetType) . '"', 1297941727); | |
} | |
if (in_array($targetType, array('array', 'string', 'float', 'integer', 'boolean'))) { | |
$converter = $this->findEligibleConverterWithHighestPriority($this->typeConverters[$sourceType][$targetType], $source, $targetType); | |
} else { | |
$converter = $this->findObjectTypeConverter($source, $sourceType, $targetType); | |
} | |
if (!is_object($converter) || !($converter instanceof \F3\FLOW3\Property\TypeConverterInterface)) { | |
throw new \F3\FLOW3\Property\Exception\TypeConverterException('No converter found which can be used to convert from "' . $sourceType . '" to "' . $targetType . '".'); | |
} | |
return $converter; | |
} | |
protected function findObjectTypeConverter($source, $sourceType, $targetClass) { | |
$convertersForSource = $this->typeConverters[$sourceType]; | |
if (isset($convertersForSource[$targetClass])) { | |
$converter = $this->findEligibleConverterWithHighestPriority($convertersForSource[$targetClass], $source, $targetClass); | |
if ($converter !== NULL) return $converter; | |
} | |
foreach (class_parents($targetClass) as $parentClass) { | |
if (!isset($convertersForSource[$parentClass])) continue; | |
$converter = $this->findEligibleConverterWithHighestPriority($convertersForSource[$parentClass], $source, $targetClass); | |
if ($converter !== NULL) return $converter; | |
} | |
$converters = $this->getConvertersForInterfaces($convertersForSource, class_implements($targetClass)); | |
return $this->findEligibleConverterWithHighestPriority($converters, $source, $targetClass); | |
} | |
protected function findEligibleConverterWithHighestPriority($converters, $source, $targetType) { | |
krsort($converters); | |
reset($converters); | |
foreach ($converters as $converter) { | |
if ($converter->canConvert($source, $targetType)) { | |
return $converter; | |
} | |
} | |
return NULL; | |
} | |
protected function getConvertersForInterfaces($convertersForSource, $interfaceNames) { | |
$convertersForInterface = array(); | |
foreach ($interfaceNames as $implementedInterface) { | |
if (isset($convertersForSource[$implementedInterface])) { | |
foreach ($convertersForSource[$implementedInterface] as $priority => $converter) { | |
if (isset($convertersForInterface[$priority])) { | |
// Exception | |
} | |
$convertersForInterface[$priority] = $converter; | |
} | |
} | |
} | |
return $convertersForInterface; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment