Browse Source

[ZF-8833, GENERIC] Zend_Validate:

- fixed constructor for IsImage(ZF-8833)
- fixed array handling for MimeType validators

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@20336 44c647ce-9c0f-0410-b52a-842ac1e357ba
thomas 16 years ago
parent
commit
77bcced878

+ 40 - 20
library/Zend/Validate/File/IsCompressed.php

@@ -60,28 +60,48 @@ class Zend_Validate_File_IsCompressed extends Zend_Validate_File_MimeType
     {
         if ($mimetype instanceof Zend_Config) {
             $mimetype = $mimetype->toArray();
-        } else if (empty($mimetype)) {
-            $mimetype = array(
-                'application/x-tar',
-                'application/x-cpio',
-                'application/x-debian-package',
-                'application/x-archive',
-                'application/x-arc',
-                'application/x-arj',
-                'application/x-lharc',
-                'application/x-lha',
-                'application/x-rar',
-                'application/zip',
-                'application/zoo',
-                'application/x-eet',
-                'application/x-java-pack200',
-                'application/x-compress',
-                'application/x-gzip',
-                'application/x-bzip2'
-            );
         }
 
-        $this->setMimeType($mimetype);
+        $temp    = array();
+        $default = array(
+            'application/x-tar',
+            'application/x-cpio',
+            'application/x-debian-package',
+            'application/x-archive',
+            'application/x-arc',
+            'application/x-arj',
+            'application/x-lharc',
+            'application/x-lha',
+            'application/x-rar',
+            'application/zip',
+            'application/zoo',
+            'application/x-eet',
+            'application/x-java-pack200',
+            'application/x-compress',
+            'application/x-gzip',
+            'application/x-bzip2'
+        );
+
+        if (is_array($mimetype)) {
+            $temp = $mimetype;
+            if (array_key_exists('magicfile', $temp)) {
+                unset($temp['magicfile']);
+            }
+
+            if (array_key_exists('headerCheck', $temp)) {
+                unset($temp['headerCheck']);
+            }
+
+            if (empty($temp)) {
+                $mimetype += $default;
+            }
+        }
+
+        if (empty($mimetype)) {
+            $mimetype = $default;
+        }
+
+        parent::__construct($mimetype);
     }
 
     /**

+ 44 - 24
library/Zend/Validate/File/IsImage.php

@@ -60,32 +60,52 @@ class Zend_Validate_File_IsImage extends Zend_Validate_File_MimeType
     {
         if ($mimetype instanceof Zend_Config) {
             $mimetype = $mimetype->toArray();
-        } else if (empty($mimetype)) {
-            $mimetype = array(
-                'image/x-quicktime',
-                'image/jp2',
-                'image/x-xpmi',
-                'image/x-portable-bitmap',
-                'image/x-portable-greymap',
-                'image/x-portable-pixmap',
-                'image/x-niff',
-                'image/tiff',
-                'image/png',
-                'image/x-unknown',
-                'image/gif',
-                'image/x-ms-bmp',
-                'application/dicom',
-                'image/vnd.adobe.photoshop',
-                'image/vnd.djvu',
-                'image/x-cpi',
-                'image/jpeg',
-                'image/x-ico',
-                'image/x-coreldraw',
-                'image/svg+xml'
-            );
         }
 
-        $this->setMimeType($mimetype);
+        $temp    = array();
+        $default = array(
+            'image/x-quicktime',
+            'image/jp2',
+            'image/x-xpmi',
+            'image/x-portable-bitmap',
+            'image/x-portable-greymap',
+            'image/x-portable-pixmap',
+            'image/x-niff',
+            'image/tiff',
+            'image/png',
+            'image/x-unknown',
+            'image/gif',
+            'image/x-ms-bmp',
+            'application/dicom',
+            'image/vnd.adobe.photoshop',
+            'image/vnd.djvu',
+            'image/x-cpi',
+            'image/jpeg',
+            'image/x-ico',
+            'image/x-coreldraw',
+            'image/svg+xml'
+        );
+
+        if (is_array($mimetype)) {
+            $temp = $mimetype;
+            if (array_key_exists('magicfile', $temp)) {
+                unset($temp['magicfile']);
+            }
+
+            if (array_key_exists('headerCheck', $temp)) {
+                unset($temp['headerCheck']);
+            }
+
+            if (empty($temp)) {
+                $mimetype += $default;
+            }
+        }
+
+        if (empty($mimetype)) {
+            $mimetype = $default;
+        }
+
+        parent::__construct($mimetype);
     }
 
     /**

+ 3 - 1
library/Zend/Validate/File/MimeType.php

@@ -123,10 +123,12 @@ class Zend_Validate_File_MimeType extends Zend_Validate_Abstract
 
         if (isset($mimetype['magicfile'])) {
             $this->setMagicFile($mimetype['magicfile']);
+            unset($mimetype['magicfile']);
         }
 
         if (isset($mimetype['headerCheck'])) {
-            $this->enableHeaderCheck(true);
+            $this->enableHeaderCheck($mimetype['headerCheck']);
+            unset($mimetype['headerCheck']);
         }
 
         $this->setMimeType($mimetype);

+ 13 - 0
tests/Zend/Validate/File/IsCompressedTest.php

@@ -176,6 +176,19 @@ class Zend_Validate_File_IsCompressedTest extends PHPUnit_Framework_TestCase
         $error = $validator->getMessages();
         $this->assertTrue(array_key_exists('fileIsCompressedFalseType', $error));
     }
+
+    public function testOptionsAtConstructor()
+    {
+        $validator = new Zend_Validate_File_IsCompressed(array(
+            'image/gif',
+            'image/jpg',
+            'magicfile' => __FILE__,
+            'headerCheck' => true));
+
+        $this->assertEquals(__FILE__, $validator->getMagicFile());
+        $this->assertTrue($validator->getHeaderCheck());
+        $this->assertEquals('image/gif,image/jpg', $validator->getMimeType());
+    }
 }
 
 // Call Zend_Validate_File_MimeTypeTest::main() if this source file is executed directly.

+ 13 - 0
tests/Zend/Validate/File/IsImageTest.php

@@ -176,6 +176,19 @@ class Zend_Validate_File_IsImageTest extends PHPUnit_Framework_TestCase
         $error = $validator->getMessages();
         $this->assertTrue(array_key_exists('fileIsImageFalseType', $error));
     }
+
+    public function testOptionsAtConstructor()
+    {
+        $validator = new Zend_Validate_File_IsImage(array(
+            'image/gif',
+            'image/jpg',
+            'magicfile' => __FILE__,
+            'headerCheck' => true));
+
+        $this->assertEquals(__FILE__, $validator->getMagicFile());
+        $this->assertTrue($validator->getHeaderCheck());
+        $this->assertEquals('image/gif,image/jpg', $validator->getMimeType());
+    }
 }
 
 // Call Zend_Validate_File_IsImage::main() if this source file is executed directly.

+ 13 - 0
tests/Zend/Validate/File/MimeTypeTest.php

@@ -187,6 +187,19 @@ class Zend_Validate_File_MimeTypeTest extends PHPUnit_Framework_TestCase
         $validator = new Zend_Validate_File_MimeType(array('image/gif', 'magicfile' => __FILE__));
         $this->assertEquals(__FILE__, $validator->getMagicFile());
     }
+
+    public function testOptionsAtConstructor()
+    {
+        $validator = new Zend_Validate_File_MimeType(array(
+            'image/gif',
+            'image/jpg',
+            'magicfile' => __FILE__,
+            'headerCheck' => true));
+
+        $this->assertEquals(__FILE__, $validator->getMagicFile());
+        $this->assertTrue($validator->getHeaderCheck());
+        $this->assertEquals('image/gif,image/jpg', $validator->getMimeType());
+    }
 }
 
 // Call Zend_Validate_File_MimeTypeTest::main() if this source file is executed directly.