Filters files array and returns values ony for existing files

@param array $files         array with id's and arguments for pivot table
@param array $existingFiles array with id's of existing files

@return array Array with records for existing files
505134
protected function filterExistingFiles(array $files, array $existingFiles)
    {
        return array_where($files, function ($value, $key) use ($existingFiles) {
            if (is_array($value)) {
                return in_array($key, $existingFiles);
            }

            return in_array($value, $existingFiles);
        });
    }
343779
public static function capture($files = null)
    {
        $files = $files ?? $_FILES;
        if (!is_array($files)) {
            return null;
        }

        $keys = array_keys($files);
        sort($keys);
        $multi = $keys !== ['error', 'name', 'size', 'tmp_name', 'type'];
        if (!$multi && is_array($files['name'])) {
            $multi = true;
            $files = array_map(function ($index) use ($files) {
                return array_combine(array_keys($files), array_column($files, $index));
            }, array_keys($files['name']));
        }

        if ($multi) {
            return array_filter(array_map([static::class, 'capture'], $files));
        }

        return new static($files['tmp_name'], $files['name'], $files['type'], $files['error']);
    }
550103
protected static function parseFiles(array $files)
    {
        $normalizedFiles = array();

        foreach($files as $name => $file)
        {
            if(is_array($file) && !isset($file['tmp_name']))
                $normalizedFiles[$name] = static::parseFiles($file);
            else if(isset($file['tmp_name']) && is_array($file['tmp_name']))
                $normalizedFiles[$name] = static::normalizeFilesTree($file);
            else if(isset($file['tmp_name']))
                $normalizedFiles[$name] = new static(
                    $file['name'], $file['type'], $file['tmp_name'], $file['error'], $file['size']
                    );
        }

        return $normalizedFiles;
    }
1089630
private function removeBrokenFiles(array $files)
    {
        // Recursive logic to replace broken arrays with empty ones (which to be removed later)
        if (isset($files['error'])) {
            return $files['error'] == 4 ? array() : $files;
        }

        foreach ($files as $key => $value) {
            // Recursive call
            $files[$key] = call_user_func(array($this, __FUNCTION__), $value);
        }

        // Now remove empty arrays
        return array_filter($files);
    }
246744
protected function processNewFiles(array $files)
    {
        foreach ($files as $file) {
            if (!file_exists($file)) {
                //This is bad
                throw new Exception('Cant find File: '.$file);
            }
            include $file;
            
            if (!isset($arrUpdate)) {
                throw new Exception('Array $arrUpdate does not exist in '.$file);
            }
            
            try {
                $this->processArray($arrUpdate);
                $this->addFileToProcessedList($file);
            } catch (Exception $ex) {
                echo $ex->getMessage();
            }
            
            unset($arrUpdate);
        }
    }
1345616
protected function deletingFilesNamesList($files) : array
	{
		return $files->flatMap(function($item) {
			$path_parts = pathinfo($item);
			$result[] = $path_parts['filename'];

			return $result;
		})->filter(function($item) {
			return trim($item) != '';
		})->all();
	}
260977
public function getFilesFromFal(&$files, $directoryArray)
    {
        foreach ($directoryArray as $directory) {
            $folder = $this->storage->getFolder($directory);

            if ($folder->getName() != '_temp_') {
                $filesInFolder = $folder->getFiles();
                if (count($filesInFolder)) {
                    foreach ($filesInFolder as $file) {
                        if (GeneralUtility::inList($this->pObj->indexerConfig['fileext'], $file->getExtension())) {
                            $files[] = $file;
                        }
                    }
                }

                // do recursion
                $subfolders = $folder->getSubFolders();
                if (count($subfolders)) {
                    foreach ($subfolders as $subfolder) {
                        $this->getFilesFromFal($files, array($subfolder->getIdentifier()));
                    }
                }
            }
        }
    }
591501
protected function filter($filters)
    {
        $results    = [];
        $documents  = $org_docs = $this->documents;
        if (isset($filters['and']) && !empty($filters['and']))
        {
            foreach($filters['and'] as $filter)
            {
                list($field, $operator, $value) = $filter;
                $documents = array_values(array_filter($documents, function ($document) use ($field, $operator, $value) {
                    return $this->match($document, $field, $operator, $value);
                }));
                $results = $documents;
            }
        }

        if (isset($filters['or']) && !empty($filters['or']))
        {
            foreach($filters['or'] as $filter)
            {
                list($field, $operator, $value) = $filter;
                $documents = array_values(array_filter($org_docs, function ($document) use ($field, $operator, $value) {
                    return $this->match($document, $field, $operator, $value);
                }));
                $results = array_unique(array_merge($results, $documents), SORT_REGULAR);
            }
        }
        $this->documents = $results;
    }
984218
protected function normalizeMultiUpload(array $files): array
	{
		$normalized = [];

		$keys = array_keys($files);

		$count = count($files['name']);

		for($i = 0; $i < $count; $i++)
		{
			foreach($keys as $key)
			{
				$normalized[$i][$key] = $files[$key][$i];
			}
		}

		return $normalized;
	}
896249
protected static function normalizeUploadedFiles(array $uploadedFiles)
    {
        $files = [];

        foreach ($uploadedFiles as $key => $value) {

            if ($value instanceof UploadedFileInterface) {

                $files[$key] = $value;

            } elseif (is_array($value) && isset($value['tmp_name'])) {

                $files[$key] = static::createUploadedFileFromSpec($value);

            } elseif (is_array($value)) {
                
                $files[$key]  = static::normalizeUploadedFiles($value);

            } else {

                throw new \InvalidArgumentException('Invalid uploaded files value');
            }
        }

        return $files;
    }
690585
protected function convertUploadedFiles(array $files)
    {
        $fileObjects = [];
        
        foreach ($files as $fieldName => $file) {
            if ($file instanceof UploadedFileInterface) {
                $fileObjects[$fieldName] = $file;
            } elseif (!isset($file['tmp_name']) && !isset($file['error'])) {
                $fileObjects[$fieldName] = $this->convertUploadedFiles($file);
            } else {
                $fileObjects[$fieldName] = new UploadedFile($file);
            }
        }
        
        return $fileObjects;
    }
1055999
protected function filterFiles($files)
    {
        if (! $files) {
            return;
        }

        foreach ($files as $key => $file) {
            if (is_array($file)) {
                $files[$key] = $this->filterFiles($files[$key]);
            }

            if (empty($files[$key])) {
                unset($files[$key]);
            }
        }

        return $files;
    }
963059
protected function convertToUploadedFileObjects(array $files): array
	{
		$uploadedFiles = [];

		foreach($files as $name => $file)
		{
			if(is_array($file['name']))
			{
				foreach($this->normalizeMultiUpload($file) as $file)
				{
					$uploadedFiles[$name][] = $this->createUploadedFile($file);
				}
			}
			else
			{
				$uploadedFiles[$name] = $this->createUploadedFile($file);
			}
		}

		return $uploadedFiles;
	}
734121
public function setFiles($files = array())
    {
        if (count($files) === 0) {
            $this->_files = $_FILES;
        } else {
            $this->_files = $files;
        }

        foreach($this->_files as $file => $content) {
            if (!isset($content['error'])) {
                unset($this->_files[$file]);
            }
        }

        return $this;
    }
404214
protected function _getFileData($_files) {
		$return_files = [];
		if (is_array($_files)) {
			foreach ($_files as $fkey => $fvalue) {
				if (is_array($fvalue)) {
					foreach ($fvalue as $varname => $varpair) {
						if (is_array($varpair)) {
							foreach ($varpair as $fieldname => $varvalue) {
								$return_files[$fkey][$fieldname][$varname]=$varvalue;
							}
						} else {
							$return_files[$fkey] = $fvalue;
						}
					}
				}
			}
		}
		return $return_files;
	}
1347718
public function countFileReferences(array $files)
    {
        if (empty($files)) {
            return [];
        }

        $selectQuery = $this->connection->createQueryBuilder();
        $selectQuery
            ->select(
                $this->connection->quoteIdentifier('filename'),
                $this->connection->quoteIdentifier('mime_type'),
                sprintf(
                    'COUNT(%s) AS count',
                    $this->connection->quoteIdentifier('contentobject_attribute_id')
                )
            )
            ->from($this->connection->quoteIdentifier($this->getStorageTable()))
            ->where(
                $selectQuery->expr()->in(
                    $this->connection->quoteIdentifier('filename'),
                    ':filenames'
                )
            )
            ->groupBy(
                $this->connection->quoteIdentifier('filename'),
                $this->connection->quoteIdentifier('mime_type')
            )
            ->setParameter(
                ':filenames',
                array_map(
                    [$this, 'removeMimeFromPath'],
                    $files
                ),
                Connection::PARAM_STR_ARRAY
            )
        ;

        $statement = $selectQuery->execute();

        $countMap = [];
        foreach ($statement->fetchAll(PDO::FETCH_ASSOC) as $row) {
            $path = $this->prependMimeToPath($row['filename'], $row['mime_type']);
            $countMap[$path] = (int)$row['count'];
        }

        // Complete counts
        foreach ($files as $path) {
            // This is already the correct path
            if (!isset($countMap[$path])) {
                $countMap[$path] = 0;
            }
        }

        return $countMap;
    }
123229
public function findRequiredFiles(array $files)
    {
        foreach ($files as $file) {
            if (file_exists($this->plugin->directory.'/'.$file)) {
                $this->addSuccess(sprintf('Found required file: %s', $file));
            } else {
                $this->addError(sprintf('Failed to find required file: %s', $file));
            }
        }
    }