Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 23 additions & 17 deletions src/FileUploadControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ class FileUploadControl extends \Nette\Forms\Controls\UploadControl
/** @var \Zet\FileUpload\Model\UploadController */
private $controller;

/** @var string */
private $uploadModel;
/** @var string|null */
private $uploadModel = null;

/**
* Třída pro filtrování nahrávaných souborů.
*
* @var string
* @var string|null
*/
private $fileFilter;
private $fileFilter = null;

/**
* Pole vlastních definovaných parametrů.
Expand All @@ -88,8 +88,8 @@ class FileUploadControl extends \Nette\Forms\Controls\UploadControl
*/
private $params = [];

/** @var string */
private $renderer;
/** @var string|null */
private $renderer = null;

/** @var string */
private $token;
Expand Down Expand Up @@ -138,10 +138,10 @@ class FileUploadControl extends \Nette\Forms\Controls\UploadControl

/**
* @static
* @param $systemContainer
* @param array $configuration
* @param \Nette\DI\Container $systemContainer
* @param object $configuration
*/
public static function register(\Nette\DI\Container $systemContainer, $configuration = [])
public static function register(\Nette\DI\Container $systemContainer, $configuration = new \stdClass())
{
$class = __CLASS__;
\Nette\Forms\Container::extensionMethod('addFileUpload', static function (
Expand All @@ -153,9 +153,15 @@ public static function register(\Nette\DI\Container $systemContainer, $configura
/** @var FileUploadControl $component */
$component = new $class($name, $maxFiles, $maxFileSize);
$component->setContainer($systemContainer);
$component->setUploadModel($configuration->uploadModel);
$component->setFileFilter($configuration->fileFilter);
$component->setRenderer($configuration->renderer);
if (isset($configuration->uploadModel)) {
$component->setUploadModel($configuration->uploadModel);
}
if (isset($configuration->fileFilter)) {
$component->setFileFilter($configuration->fileFilter);
}
if (isset($configuration->renderer)) {
$component->setRenderer($configuration->renderer);
}

if (($configuration->translator ?? null) === null) {
$translator = $systemContainer->getByType(ITranslator::class, false);
Expand Down Expand Up @@ -385,10 +391,10 @@ public function getFileSizeString(): string


/**
* @return string
* @return string|null
* @internal
*/
public function getFileFilter(): string
public function getFileFilter(): ?string
{
return $this->fileFilter;
}
Expand Down Expand Up @@ -469,9 +475,9 @@ public function setRenderer(string $renderer): self


/**
* @return string
* @return string|null
*/
public function getRenderer(): string
public function getRenderer(): ?string
{
return $this->renderer;
}
Expand Down Expand Up @@ -670,7 +676,7 @@ private function parseIniSize(string $value): int
$units = ['k' => 1024, 'm' => 1048576, 'g' => 1073741824];
$unit = strtolower(substr($value, -1));
if (is_numeric($unit) || !isset($units[$unit])) {
return $value;
return intval($value);
}

return ((int) $value) * $units[$unit];
Expand Down
12 changes: 9 additions & 3 deletions src/Model/UploadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,17 @@ public function getRenderer(): BaseRenderer
{
if ($this->renderer === null) {
$rendererClass = $this->uploadControl->getRenderer();
$this->renderer = new $rendererClass($this->uploadControl, $this->uploadControl->getTranslator());
if ($rendererClass !== null) {
$this->renderer = new $rendererClass($this->uploadControl, $this->uploadControl->getTranslator());

if (!($this->renderer instanceof BaseRenderer)) {
if (!($this->renderer instanceof BaseRenderer)) {
throw new InvalidStateException(
'Renderer musí být instancí třídy `\\Zet\\FileUpload\\Template\\BaseRenderer`.'
);
}
} else {
throw new InvalidStateException(
'Renderer musí být instancí třídy `\\Zet\\FileUpload\\Template\\BaseRenderer`.'
'Musí být nastavena třída na renderování.'
);
}
}
Expand Down