Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Show file tree
Hide file tree
Showing 28 changed files with 351 additions and 31 deletions.
9 changes: 7 additions & 2 deletions src/File/Crc32.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,16 @@ public function addCrc32($options)
* Returns true if and only if the given file confirms the set hash
*
* @param string|array $value Filename to check for hash
* @param array $file File data from \Zend\File\Transfer\Transfer (optional)
* @return bool
*/
public function isValid($value)
public function isValid($value, $file = null)
{
if (is_array($value)) {
if (is_string($value) && is_array($file)) {
// Legacy Zend\Transfer API support
$filename = $file['name'];
$file = $file['tmp_name'];
} elseif (is_array($value)) {
if (!isset($value['tmp_name']) || !isset($value['name'])) {
throw new Exception\InvalidArgumentException(
'Value array must be in $_FILES format'
Expand Down
9 changes: 7 additions & 2 deletions src/File/ExcludeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,16 @@ class ExcludeExtension extends Extension
* set extension list
*
* @param string|array $value Real file to check for extension
* @param array $file File data from \Zend\File\Transfer\Transfer (optional)
* @return bool
*/
public function isValid($value)
public function isValid($value, $file = null)
{
if (is_array($value)) {
if (is_string($value) && is_array($file)) {
// Legacy Zend\Transfer API support
$filename = $file['name'];
$file = $file['tmp_name'];
} elseif (is_array($value)) {
if (!isset($value['tmp_name']) || !isset($value['name'])) {
throw new Exception\InvalidArgumentException(
'Value array must be in $_FILES format'
Expand Down
10 changes: 8 additions & 2 deletions src/File/ExcludeMimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,17 @@ class ExcludeMimeType extends MimeType
* mime types will not be accepted like "image/gif", "image/jpeg" and so on.
*
* @param string|array $value Real file to check for mimetype
* @param array $file File data from \Zend\File\Transfer\Transfer (optional)
* @return bool
*/
public function isValid($value)
public function isValid($value, $file = null)
{
if (is_array($value)) {
if (is_string($value) && is_array($file)) {
// Legacy Zend\Transfer API support
$filename = $file['name'];
$filetype = $file['type'];
$file = $file['tmp_name'];
} elseif (is_array($value)) {
if (!isset($value['tmp_name']) || !isset($value['name']) || !isset($value['type'])) {
throw new Exception\InvalidArgumentException(
'Value array must be in $_FILES format'
Expand Down
10 changes: 8 additions & 2 deletions src/File/Exists.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,17 @@ public function addDirectory($directory)
* Returns true if and only if the file already exists in the set directories
*
* @param string|array $value Real file to check for existence
* @param array $file File data from \Zend\File\Transfer\Transfer (optional)
* @return bool
*/
public function isValid($value)
public function isValid($value, $file = null)
{
if (is_array($value)) {
if (is_string($value) && is_array($file)) {
// Legacy Zend\Transfer API support
$filename = $file['name'];
$file = $file['tmp_name'];
$this->setValue($filename);
} elseif (is_array($value)) {
if (!isset($value['tmp_name']) || !isset($value['name'])) {
throw new Exception\InvalidArgumentException(
'Value array must be in $_FILES format'
Expand Down
9 changes: 7 additions & 2 deletions src/File/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,16 @@ public function addExtension($extension)
* set extension list
*
* @param string|array $value Real file to check for extension
* @param array $file File data from \Zend\File\Transfer\Transfer (optional)
* @return bool
*/
public function isValid($value)
public function isValid($value, $file = null)
{
if (is_array($value)) {
if (is_string($value) && is_array($file)) {
// Legacy Zend\Transfer API support
$filename = $file['name'];
$file = $file['tmp_name'];
} elseif (is_array($value)) {
if (!isset($value['tmp_name']) || !isset($value['name'])) {
throw new Exception\InvalidArgumentException(
'Value array must be in $_FILES format'
Expand Down
9 changes: 7 additions & 2 deletions src/File/Hash.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,16 @@ public function addHash($options)
* Returns true if and only if the given file confirms the set hash
*
* @param string|array $value File to check for hash
* @param array $file File data from \Zend\File\Transfer\Transfer (optional)
* @return bool
*/
public function isValid($value)
public function isValid($value, $file = null)
{
if (is_array($value)) {
if (is_string($value) && is_array($file)) {
// Legacy Zend\Transfer API support
$filename = $file['name'];
$file = $file['tmp_name'];
} elseif (is_array($value)) {
if (!isset($value['tmp_name']) || !isset($value['name'])) {
throw new Exception\InvalidArgumentException(
'Value array must be in $_FILES format'
Expand Down
9 changes: 7 additions & 2 deletions src/File/ImageSize.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,16 @@ public function setImageHeight($options)
* not bigger than max
*
* @param string|array $value Real file to check for image size
* @param array $file File data from \Zend\File\Transfer\Transfer (optional)
* @return bool
*/
public function isValid($value)
public function isValid($value, $file = null)
{
if (is_array($value)) {
if (is_string($value) && is_array($file)) {
// Legacy Zend\Transfer API support
$filename = $file['name'];
$file = $file['tmp_name'];
} elseif (is_array($value)) {
if (!isset($value['tmp_name']) || !isset($value['name'])) {
throw new Exception\InvalidArgumentException(
'Value array must be in $_FILES format'
Expand Down
9 changes: 7 additions & 2 deletions src/File/Md5.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,16 @@ public function addMd5($options)
* Returns true if and only if the given file confirms the set hash
*
* @param string|array $value Filename to check for hash
* @param array $file File data from \Zend\File\Transfer\Transfer (optional)
* @return bool
*/
public function isValid($value)
public function isValid($value, $file = null)
{
if (is_array($value)) {
if (is_string($value) && is_array($file)) {
// Legacy Zend\Transfer API support
$filename = $file['name'];
$file = $file['tmp_name'];
} elseif (is_array($value)) {
if (!isset($value['tmp_name']) || !isset($value['name'])) {
throw new Exception\InvalidArgumentException(
'Value array must be in $_FILES format'
Expand Down
10 changes: 8 additions & 2 deletions src/File/MimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,11 +342,17 @@ public function addMimeType($mimetype)
* mime types will be accepted like "image/gif", "image/jpeg" and so on.
*
* @param string|array $value Real file to check for mimetype
* @param array $file File data from \Zend\File\Transfer\Transfer (optional)
* @return bool
*/
public function isValid($value)
public function isValid($value, $file = null)
{
if (is_array($value)) {
if (is_string($value) && is_array($file)) {
// Legacy Zend\Transfer API support
$filename = $file['name'];
$filetype = $file['type'];
$file = $file['tmp_name'];
} elseif (is_array($value)) {
if (!isset($value['tmp_name']) || !isset($value['name']) || !isset($value['type'])) {
throw new Exception\InvalidArgumentException(
'Value array must be in $_FILES format'
Expand Down
10 changes: 8 additions & 2 deletions src/File/NotExists.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,17 @@ class NotExists extends Exists
* Returns true if and only if the file does not exist in the set destinations
*
* @param string|array $value Real file to check for existence
* @param array $file File data from \Zend\File\Transfer\Transfer (optional)
* @return bool
*/
public function isValid($value)
public function isValid($value, $file = null)
{
if (is_array($value)) {
if (is_string($value) && is_array($file)) {
// Legacy Zend\Transfer API support
$filename = $file['name'];
$file = $file['tmp_name'];
$this->setValue($filename);
} elseif (is_array($value)) {
if (!isset($value['tmp_name']) || !isset($value['name'])) {
throw new Exception\InvalidArgumentException(
'Value array must be in $_FILES format'
Expand Down
9 changes: 7 additions & 2 deletions src/File/Sha1.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,16 @@ public function addSha1($options)
* Returns true if and only if the given file confirms the set hash
*
* @param string $value|array Filename to check for hash
* @param array $file File data from \Zend\File\Transfer\Transfer (optional)
* @return bool
*/
public function isValid($value)
public function isValid($value, $file = null)
{
if (is_array($value)) {
if (is_string($value) && is_array($file)) {
// Legacy Zend\Transfer API support
$filename = $file['name'];
$file = $file['tmp_name'];
} elseif (is_array($value)) {
if (!isset($value['tmp_name']) || !isset($value['name'])) {
throw new Exception\InvalidArgumentException(
'Value array must be in $_FILES format'
Expand Down
9 changes: 7 additions & 2 deletions src/File/Size.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,16 @@ protected function setSize($size)
* not bigger than max (when max is not null).
*
* @param string|array $value File to check for size
* @param array $file File data from \Zend\File\Transfer\Transfer (optional)
* @return bool
*/
public function isValid($value)
public function isValid($value, $file = null)
{
if (is_array($value)) {
if (is_string($value) && is_array($file)) {
// Legacy Zend\Transfer API support
$filename = $file['name'];
$file = $file['tmp_name'];
} elseif (is_array($value)) {
if (!isset($value['tmp_name']) || !isset($value['name'])) {
throw new Exception\InvalidArgumentException(
'Value array must be in $_FILES format'
Expand Down
11 changes: 8 additions & 3 deletions src/File/WordCount.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,17 @@ public function setMax($max)
* Returns true if and only if the counted words are at least min and
* not bigger than max (when max is not null).
*
* @param string $value|array Filename to check for word count
* @param string|array $value Filename to check for word count
* @param array $file File data from \Zend\File\Transfer\Transfer (optional)
* @return bool
*/
public function isValid($value)
public function isValid($value, $file = null)
{
if (is_array($value)) {
if (is_string($value) && is_array($file)) {
// Legacy Zend\Transfer API support
$filename = $file['name'];
$file = $file['tmp_name'];
} elseif (is_array($value)) {
if (!isset($value['tmp_name']) || !isset($value['name'])) {
throw new Exception\InvalidArgumentException(
'Value array must be in $_FILES format'
Expand Down
17 changes: 17 additions & 0 deletions test/File/Crc32Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,23 @@ public function testBasic($options, $isValidParam, $expected, $messageKey)
}
}

/**
* Ensures that the validator follows expected behavior for legacy Zend\Transfer API
*
* @dataProvider basicBehaviorDataProvider
* @return void
*/
public function testLegacy($options, $isValidParam, $expected, $messageKey)
{
if (is_array($isValidParam)) {
$validator = new File\Crc32($options);
$this->assertEquals($expected, $validator->isValid($isValidParam['tmp_name'], $isValidParam));
if (!$expected) {
$this->assertTrue(array_key_exists($messageKey, $validator->getMessages()));
}
}
}

/**
* Ensures that getCrc32() returns expected value
*
Expand Down
17 changes: 17 additions & 0 deletions test/File/ExcludeExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,23 @@ public function testBasic($options, $isValidParam, $expected, $messageKey)
}
}

/**
* Ensures that the validator follows expected behavior for legacy Zend\Transfer API
*
* @dataProvider basicBehaviorDataProvider
* @return void
*/
public function testLegacy($options, $isValidParam, $expected, $messageKey)
{
if (is_array($isValidParam)) {
$validator = new File\ExcludeExtension($options);
$this->assertEquals($expected, $validator->isValid($isValidParam['tmp_name'], $isValidParam));
if (!$expected) {
$this->assertTrue(array_key_exists($messageKey, $validator->getMessages()));
}
}
}

public function testCaseTesting()
{
$files = array(
Expand Down
15 changes: 15 additions & 0 deletions test/File/ExcludeMimeTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,21 @@ public function testBasic($options, $isValidParam, $expected)
$this->assertEquals($expected, $validator->isValid($isValidParam));
}

/**
* Ensures that the validator follows expected behavior for legacy Zend\Transfer API
*
* @dataProvider basicBehaviorDataProvider
* @return void
*/
public function testLegacy($options, $isValidParam, $expected)
{
if (is_array($isValidParam)) {
$validator = new ExcludeMimeType($options);
$validator->enableHeaderCheck();
$this->assertEquals($expected, $validator->isValid($isValidParam['tmp_name'], $isValidParam));
}
}

/**
* Ensures that getMimeType() returns expected value
*
Expand Down
14 changes: 14 additions & 0 deletions test/File/ExistsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ public function testBasic($options, $isValidParam, $expected)
$this->assertEquals($expected, $validator->isValid($isValidParam));
}

/**
* Ensures that the validator follows expected behavior for legacy Zend\Transfer API
*
* @dataProvider basicBehaviorDataProvider
* @return void
*/
public function testLegacy($options, $isValidParam, $expected)
{
if (is_array($isValidParam)) {
$validator = new File\Exists($options);
$this->assertEquals($expected, $validator->isValid($isValidParam['tmp_name'], $isValidParam));
}
}

/**
* Ensures that getDirectory() returns expected value
*
Expand Down
17 changes: 17 additions & 0 deletions test/File/ExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,23 @@ public function testBasic($options, $isValidParam, $expected, $messageKey)
}
}

/**
* Ensures that the validator follows expected behavior for legacy Zend\Transfer API
*
* @dataProvider basicBehaviorDataProvider
* @return void
*/
public function testLegacy($options, $isValidParam, $expected, $messageKey)
{
if (is_array($isValidParam)) {
$validator = new File\Extension($options);
$this->assertEquals($expected, $validator->isValid($isValidParam['tmp_name'], $isValidParam));
if (!$expected) {
$this->assertTrue(array_key_exists($messageKey, $validator->getMessages()));
}
}
}

/**
* @return void
*/
Expand Down
17 changes: 17 additions & 0 deletions test/File/HashTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,23 @@ public function testBasic($options, $isValidParam, $expected, $messageKey)
}
}

/**
* Ensures that the validator follows expected behavior for legacy Zend\Transfer API
*
* @dataProvider basicBehaviorDataProvider
* @return void
*/
public function testLegacy($options, $isValidParam, $expected, $messageKey)
{
if (is_array($isValidParam)) {
$validator = new File\Hash($options);
$this->assertEquals($expected, $validator->isValid($isValidParam['tmp_name'], $isValidParam));
if (!$expected) {
$this->assertTrue(array_key_exists($messageKey, $validator->getMessages()));
}
}
}

/**
* Ensures that getHash() returns expected value
*
Expand Down
Loading

0 comments on commit 49a7844

Please sign in to comment.