public function convertFrom($source, $targetType, array $convertedChildProperties = [], PropertyMappingConfigurationInterface $configuration = null)
{
if (is_array($source)) {
return $source;
}
if (is_string($source)) {
if ($source === '') {
return [];
} else {
$stringFormat = $this->getStringFormat($configuration);
switch ($stringFormat) {
case self::STRING_FORMAT_CSV:
return explode($this->getStringDelimiter($configuration), $source);
case self::STRING_FORMAT_JSON:
return json_decode($source, true);
default:
throw new InvalidPropertyMappingConfigurationException(sprintf('Conversion from string to array failed due to invalid string format setting "%s"', $stringFormat), 1404903208);
}
}
}
if ($source instanceof PersistentResource) {
$exportType = $this->getResourceExportType($configuration);
switch ($exportType) {
case self::RESOURCE_EXPORT_TYPE_BASE64:
return ['filename' => $source->getFilename(), 'data' => base64_encode(file_get_contents('resource://' . $source->getSha1()))];
case self::RESOURCE_EXPORT_TYPE_FILE:
$sourceStream = $source->getStream();
if ($sourceStream === false) {
throw new InvalidSourceException(sprintf('Could not get stream of resource "%s" (%s). This might be caused by a broken resource object and can be fixed by running the "resource:clean" command.', $source->getFilename(), $source->getSha1()), 1435842312);
}
$targetStream = fopen($configuration->getConfigurationValue(ArrayConverter::class, self::CONFIGURATION_RESOURCE_SAVE_PATH) . '/' . $source->getSha1(), 'w');
stream_copy_to_stream($sourceStream, $targetStream);
fclose($targetStream);
fclose($sourceStream);
return ['filename' => $source->getFilename(), 'hash' => $source->getSha1()];
default:
throw new InvalidPropertyMappingConfigurationException(sprintf('Conversion from PersistentResource to array failed due to invalid resource export type setting "%s"', $exportType), 1404903210);
}
}
throw new TypeConverterException('Conversion to array failed for unknown reason', 1404903387);
}