Skip to content

Validator: relax parameter typehints #244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
22 changes: 15 additions & 7 deletions src/Forms/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,23 @@ public static function validateStatic(IControl $control, bool $arg): bool
/**
* Is control filled?
*/
public static function validateFilled(Controls\BaseControl $control): bool
public static function validateFilled(IControl $control): bool
{
return $control->isFilled();
if ($control instanceof Controls\BaseControl || method_exists($control, 'isFilled')) {
return $control->isFilled();
}

$value = $control->getValue();
return $value !== null && $value !== [] && $value !== '';
}


/**
* Is control not filled?
*/
public static function validateBlank(Controls\BaseControl $control): bool
public static function validateBlank(IControl $control): bool
{
return !$control->isFilled();
return !static::validateFilled($control);
}


Expand Down Expand Up @@ -318,9 +323,10 @@ public static function validateFloat(IControl $control): bool
/**
* Is file size in limit?
*/
public static function validateFileSize(Controls\UploadControl $control, $limit): bool
public static function validateFileSize(IControl $control, $limit): bool
{
foreach (static::toArray($control->getValue()) as $file) {
Validators::assert($file, Nette\Http\FileUpload::class, 'control value');
if ($file->getSize() > $limit || $file->getError() === UPLOAD_ERR_INI_SIZE) {
return false;
}
Expand All @@ -333,10 +339,11 @@ public static function validateFileSize(Controls\UploadControl $control, $limit)
* Has file specified mime type?
* @param string|string[] $mimeType
*/
public static function validateMimeType(Controls\UploadControl $control, $mimeType): bool
public static function validateMimeType(IControl $control, $mimeType): bool
{
$mimeTypes = is_array($mimeType) ? $mimeType : explode(',', $mimeType);
foreach (static::toArray($control->getValue()) as $file) {
Validators::assert($file, Nette\Http\FileUpload::class, 'control value');
$type = strtolower($file->getContentType());
if (!in_array($type, $mimeTypes, true) && !in_array(preg_replace('#/.*#', '/*', $type), $mimeTypes, true)) {
return false;
Expand All @@ -349,9 +356,10 @@ public static function validateMimeType(Controls\UploadControl $control, $mimeTy
/**
* Is file image?
*/
public static function validateImage(Controls\UploadControl $control): bool
public static function validateImage(IControl $control): bool
{
foreach (static::toArray($control->getValue()) as $file) {
Validators::assert($file, Nette\Http\FileUpload::class, 'control value');
if (!$file->isImage()) {
return false;
}
Expand Down
122 changes: 122 additions & 0 deletions tests/Forms/Validator.customControl.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

/**
* Test: Nette\Forms\Controls\BaseControl
*/

declare(strict_types=1);

use Nette\Forms\Form;
use Nette\Forms\Validator;
use Nette\Http\FileUpload;
use Nette\Utils\AssertionException;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


class CustomControl implements \Nette\Forms\IControl
{

private $value;


public function __construct($value)
{
$this->value = $value;
}


public function setValue($value)
{
$this->value = $value;
}


public function getValue()
{
return $this->value;
}


public function validate(): void
{
}


public function getErrors(): array
{
return [];
}


public function isOmitted(): bool
{
return false;
}

}


test(function () { // filled, blank
$input = new CustomControl('');
Assert::false(Validator::validateFilled($input));
Assert::true(Validator::validateBlank($input));

$input = new CustomControl(null);
Assert::false(Validator::validateFilled($input));
Assert::true(Validator::validateBlank($input));

$input = new CustomControl([]);
Assert::false(Validator::validateFilled($input));
Assert::true(Validator::validateBlank($input));

$input = new CustomControl(42);
Assert::true(Validator::validateFilled($input));
Assert::false(Validator::validateBlank($input));
});


test(function () { // file upload related validators
$input = new CustomControl(new FileUpload([
'name' => 'foo',
'size' => 1,
'tmp_name' => __FILE__,
'error' => UPLOAD_ERR_OK
]));
Assert::true(Validator::validateFileSize($input, 42));
Assert::true(Validator::validateMimeType($input, ['text/x-php']));
Assert::false(Validator::validateImage($input));

$input = new CustomControl(new FileUpload([
'name' => 'foo',
'size' => 100,
'tmp_name' => __DIR__ . '/files/logo.gif',
'error' => UPLOAD_ERR_OK
]));
Assert::false(Validator::validateFileSize($input, 42));
Assert::false(Validator::validateMimeType($input, ['text/x-php']));
Assert::true(Validator::validateImage($input));

Assert::exception(
function () : void {
Assert::false(Validator::validateFileSize(new CustomControl('foo'), 42));
},
AssertionException::class
);

Assert::exception(
function () : void {
Assert::false(Validator::validateMimeType(new CustomControl('foo'), ['plain/text']));
},
AssertionException::class
);

Assert::exception(
function () : void {
Assert::false(Validator::validateImage(new CustomControl('foo')));
},
AssertionException::class
);
});