Skip to content

Commit e3a6e9e

Browse files
author
Florian Krämer
committed
Adding uploadError() and mimeType() to the Validation class
1 parent 1c0f97e commit e3a6e9e

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

lib/Cake/Test/Case/Utility/ValidationTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2166,4 +2166,30 @@ public function testDatetime() {
21662166
$this->assertFalse(Validation::datetime('31 11 2006 1:00pm', 'dmy'));
21672167
}
21682168

2169+
/**
2170+
* testMimeType method
2171+
*
2172+
* @return void
2173+
*/
2174+
public function testMimeType() {
2175+
$file = CORE_PATH . 'Cake' . DS . 'Test' . DS . 'test_app' . DS . 'webroot' . DS . 'img' . DS . 'cake.power.gif';
2176+
$this->assertTrue(Validation::mimeType($file, array('image/gif')));
2177+
$this->assertTrue(Validation::mimeType(array('tmp_name' => $file), array('image/gif')));
2178+
2179+
$this->assertFalse(Validation::mimeType($file, array('image/png')));
2180+
$this->assertFalse(Validation::mimeType(array('tmp_name' => $file), array('image/png')));
2181+
}
2182+
2183+
/**
2184+
* testMimeType method
2185+
*
2186+
* @return void
2187+
*/
2188+
public function testUploadError() {
2189+
$this->assertTrue(Validation::uploadError(0));
2190+
$this->assertTrue(Validation::uploadError(array('error' => 0)));
2191+
2192+
$this->assertFalse(Validation::uploadError(2));
2193+
$this->assertFalse(Validation::uploadError(array('error' => 2)));
2194+
}
21692195
}

lib/Cake/Utility/Validation.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919

2020
App::uses('Multibyte', 'I18n');
21+
App::uses('File', 'Utility');
2122
// Load multibyte if the extension is missing.
2223
if (!function_exists('mb_strlen')) {
2324
class_exists('Multibyte');
@@ -858,6 +859,39 @@ public static function luhn($check, $deep = false) {
858859
return ($sum % 10 == 0);
859860
}
860861

862+
/**
863+
* Checks the mime type of a file
864+
*
865+
* @param string|array $check
866+
* @param array $mimeTypes to check for
867+
* @return boolean Success
868+
*/
869+
public static function mimeType($check, $mimeTypes = array()) {
870+
if (is_array($check) && isset($check['tmp_name'])) {
871+
$check = $check['tmp_name'];
872+
}
873+
874+
$File = new File($check);
875+
$info = $File->info();
876+
877+
return in_array($info['mime'], $mimeTypes);
878+
}
879+
880+
/**
881+
* Checking for upload errors
882+
*
883+
* @param string|array $check
884+
* @retrun boolean
885+
* @see http://www.php.net/manual/en/features.file-upload.errors.php
886+
*/
887+
public static function uploadError($check) {
888+
if (is_array($check) && isset($check['error'])) {
889+
$check = $check['error'];
890+
}
891+
892+
return $check === UPLOAD_ERR_OK;
893+
}
894+
861895
/**
862896
* Lazily populate the IP address patterns used for validations
863897
*

0 commit comments

Comments
 (0)