|
<?php |
|
|
|
namespace My\FoobarCom\Form\Validation; |
|
|
|
/* * |
|
* This script belongs to the TYPO3 Flow package "Neos.Form". * |
|
* * |
|
* It is free software; you can redistribute it and/or modify it under * |
|
* the terms of the GNU Lesser General Public License, either version 3 * |
|
* of the License, or (at your option) any later version. * |
|
* * |
|
* The TYPO3 project - inspiring people to share! * |
|
* */ |
|
|
|
use Neos\Flow\Annotations as Flow; |
|
use Neos\Flow\ResourceManagement\ResourceManager; |
|
|
|
/** |
|
* The given $value is valid if it is an \Neos\Flow\ResourceManagement\PersistentResource of the configured resolution |
|
* Note: a value of NULL or empty string ('') is considered valid |
|
*/ |
|
class FileTypeValidator extends \Neos\Flow\Validation\Validator\AbstractValidator |
|
{ |
|
/** |
|
* @var ResourceManager |
|
* @Flow\Inject |
|
*/ |
|
protected $resourceManager; |
|
|
|
/** |
|
* @var array |
|
*/ |
|
protected $supportedOptions = [ |
|
'allowedExtensions' => [[], 'Array of allowed file extensions', 'array', true] |
|
]; |
|
|
|
/** |
|
* The given $value is valid if it is an \Neos\Flow\ResourceManagement\PersistentResource of the configured resolution |
|
* Note: a value of NULL or empty string ('') is considered valid |
|
* |
|
* @param \Neos\Flow\ResourceManagement\PersistentResource $resource The resource object that should be validated |
|
* @return void |
|
* @api |
|
*/ |
|
protected function isValid($resource) |
|
{ |
|
if (!$resource instanceof \Neos\Flow\ResourceManagement\PersistentResource) { |
|
$this->addError('The given value was not a Resource instance.', 1327865587); |
|
return; |
|
} |
|
$fileExtension = $resource->getFileExtension(); |
|
if ($fileExtension === null || $fileExtension === '') { |
|
$this->addError( |
|
'Die Datei <strong>"' . $resource->getFilename() . '"</strong> hat keine gültige Endung.', |
|
1327865808 |
|
); |
|
// Remove the PersistentResource to clean up |
|
$this->resourceManager->deleteResource($resource); |
|
return; |
|
} |
|
if (!in_array($fileExtension, $this->options['allowedExtensions'])) { |
|
$this->addError( |
|
'Wir können <strong>"' . $resource->getFilename( |
|
) . '"</strong> leider nicht verarbeiten.<br />Möglich sind Dateien des Typs: <strong>' . implode( |
|
', ', |
|
$this->options['allowedExtensions'] |
|
) . '</strong>', |
|
1327865764, |
|
[$resource->getFileExtension()] |
|
); |
|
// Remove the PersistentResource to clean up |
|
$this->resourceManager->deleteResource($resource); |
|
return; |
|
} |
|
} |
|
} |