Neos\Flow\Property\TypeConverter\ArrayConverter::convertFrom PHP Метод

convertFrom() публичный Метод

If it is a string it will be converted according to the configured string format.
public convertFrom ( mixed $source, string $targetType, array $convertedChildProperties = [], Neos\Flow\Property\PropertyMappingConfigurationInterface $configuration = null ) : array
$source mixed
$targetType string
$convertedChildProperties array
$configuration Neos\Flow\Property\PropertyMappingConfigurationInterface
Результат array
    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);
    }

Usage Example

 /**
  * @test
  * @dataProvider stringToArrayDataProvider
  */
 public function canConvertFromStringToArray($source, $expectedResult, $mappingConfiguration)
 {
     // Create a map of arguments to return values.
     $configurationValueMap = [];
     foreach ($mappingConfiguration as $setting => $value) {
         $configurationValueMap[] = [ArrayConverter::class, $setting, $value];
     }
     $propertyMappingConfiguration = $this->createMock(PropertyMappingConfiguration::class);
     $propertyMappingConfiguration->expects($this->any())->method('getConfigurationValue')->will($this->returnValueMap($configurationValueMap));
     $this->assertEquals($expectedResult, $this->converter->convertFrom($source, 'array', [], $propertyMappingConfiguration));
 }