Skip to content

Commit

Permalink
Merge pull request #158 from icetee/feature/count-psr7-2.27.x
Browse files Browse the repository at this point in the history
Supported PSR-7 in File\Count validator
  • Loading branch information
gsteel authored Nov 14, 2022
2 parents e071d04 + e9c675f commit 695bfa4
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 10 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"require": {
"php": "~8.0.0 || ~8.1.0 || ~8.2.0",
"laminas/laminas-servicemanager": "^3.12.0",
"laminas/laminas-stdlib": "^3.13"
"laminas/laminas-stdlib": "^3.13",
"psr/http-message": "^1.0.1"
},
"require-dev": {
"laminas/laminas-coding-standard": "^2.4.0",
Expand All @@ -47,7 +48,6 @@
"psalm/plugin-phpunit": "^0.18.0",
"psr/http-client": "^1.0.1",
"psr/http-factory": "^1.0.1",
"psr/http-message": "^1.0.1",
"vimeo/psalm": "^4.28"
},
"suggest": {
Expand Down
41 changes: 33 additions & 8 deletions src/File/Count.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Laminas\Validator\AbstractValidator;
use Laminas\Validator\Exception;
use Psr\Http\Message\UploadedFileInterface;
use Traversable;

use function array_key_exists;
Expand Down Expand Up @@ -177,7 +178,7 @@ public function setMax($max)
/**
* Adds a file for validation
*
* @param string|array $file
* @param string|array|UploadedFileInterface $file
* @return $this
*/
public function addFile($file)
Expand All @@ -194,6 +195,10 @@ public function addFile($file)
}
}

if ($file instanceof UploadedFileInterface && is_string($file->getClientFilename())) {
$this->files[(string) $file->getClientFilename()] = $file->getClientFilename();
}

return $this;
}

Expand All @@ -202,25 +207,30 @@ public function addFile($file)
* not bigger than max (when max is not null). Attention: When checking with set min you
* must give all files with the first call, otherwise you will get a false.
*
* @param string|array $value Filenames to check for count
* @param array $file File data from \Laminas\File\Transfer\Transfer
* @param string|array|UploadedFileInterface $value Filenames to check for count
* @param array $file File data from \Laminas\File\Transfer\Transfer
* @return bool
*/
public function isValid($value, $file = null)
{
if (($file !== null) && ! array_key_exists('destination', $file)) {
$file['destination'] = dirname($value);
}
if ($this->isUploadedFilterInterface($value)) {
$this->addFile($value);
} elseif ($file !== null) {
if (! array_key_exists('destination', $file)) {
$file['destination'] = dirname($value);
}

if (($file !== null) && array_key_exists('tmp_name', $file)) {
$value = $file['destination'] . DIRECTORY_SEPARATOR . $file['name'];
if (array_key_exists('tmp_name', $file)) {
$value = $file['destination'] . DIRECTORY_SEPARATOR . $file['name'];
}
}

if (($file === null) || ! empty($file['tmp_name'])) {
$this->addFile($value);
}

$this->count = count($this->files);

if (($this->getMax() !== null) && ($this->count > $this->getMax())) {
return $this->throwError($file, self::TOO_MANY);
}
Expand Down Expand Up @@ -254,4 +264,19 @@ protected function throwError($file, $errorType)
$this->error($errorType);
return false;
}

/**
* Checks if the type of uploaded file is UploadedFileInterface.
*
* @param string|array|UploadedFileInterface $value Filenames to check for count
* @return bool
*/
private function isUploadedFilterInterface($value)
{
if ($value instanceof UploadedFileInterface) {
return true;
}

return false;
}
}
22 changes: 22 additions & 0 deletions test/File/CountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
use Laminas\Validator\Exception\InvalidArgumentException;
use Laminas\Validator\File\Count;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\UploadedFileInterface;
use ReflectionClass;

use function basename;

/**
* @group Laminas_Validator
* @covers \Laminas\Validator\File\Count
Expand Down Expand Up @@ -239,4 +242,23 @@ public function testCanProvideMinAndMaxAsDiscreteConstructorArguments(): void
self::assertSame($min, $validator->getMin());
self::assertSame($max, $validator->getMax());
}

public function testPsr7FileTypes(): void
{
$testFile = __DIR__ . '/_files/testsize.mo';

$upload = $this->createMock(UploadedFileInterface::class);
$upload->method('getClientFilename')->willReturn(basename($testFile));

$validValidator = new Count(['min' => 1]);

$this->assertTrue($validValidator->isValid($upload));
$this->assertTrue($validValidator->isValid($upload, []));

$invalidMinValidator = new Count(['min' => 2]);
$invalidMaxValidator = new Count(['max' => 0]);

$this->assertFalse($invalidMinValidator->isValid($upload));
$this->assertFalse($invalidMaxValidator->isValid($upload));
}
}

0 comments on commit 695bfa4

Please sign in to comment.