TYPO3 CMS  TYPO3_7-6
ArrayToValidationElementConverter.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
21 
26 {
30  protected $sourceTypes = ['array'];
31 
35  protected $targetType = 'TYPO3\\CMS\\Form\\Domain\\Model\\ValidationElement';
36 
40  protected $priority = 1;
41 
49  public function canConvertFrom($source, $targetType)
50  {
51  return is_array($source);
52  }
53 
64  public function convertFrom($source, $targetType, array $convertedChildProperties = [], PropertyMappingConfigurationInterface $configuration = null)
65  {
67  $validationElement = GeneralUtility::makeInstance(ValidationElement::class);
68  if (is_array($source)) {
79  foreach ($source as $propertyName => $value) {
80  if (is_array($value)) {
81  $uploadedFiles = [];
82  if (
83  isset($value['name'])
84  && isset($value['type'])
85  && isset($value['tmp_name'])
86  && isset($value['size'])
87  ) {
88  // if single file upload - cast to array
89  $uploadedFiles[] = $value;
90  } elseif (
91  isset($value[0]['name'])
92  && isset($value[0]['type'])
93  && isset($value[0]['tmp_name'])
94  && isset($value[0]['size'])
95  ) {
96  // multi file upload
97  $uploadedFiles = $value;
98  }
99 
100  if (!empty($uploadedFiles)) {
101  foreach ($uploadedFiles as $key => &$file) {
102  if (
103  $file['name'] === ''
104  && $file['type'] === ''
105  && $file['tmp_name'] === ''
106  && $file['size'] === 0
107  ) {
108  unset($uploadedFiles[$key]);
109  continue;
110  }
111  $fileInfo = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Type\File\FileInfo::class, $file['tmp_name']);
112  $file['type'] = $fileInfo->getMimeType();
113  $file['name'] = htmlspecialchars($file['name']);
114  }
115  $source[$propertyName] = $uploadedFiles;
116  }
117  }
118  }
119  $validationElement->setIncomingFields($source);
120  }
121 
122  return $validationElement;
123  }
124 }
convertFrom($source, $targetType, array $convertedChildProperties=[], \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration=null)