Improve rcube_utils::file2class() to not return duplicates
| | |
| | | */ |
| | | public static function file2class($mimetype, $filename) |
| | | { |
| | | $mimetype = strtolower($mimetype); |
| | | $filename = strtolower($filename); |
| | | |
| | | list($primary, $secondary) = explode('/', $mimetype); |
| | | |
| | | $classes = array($primary ? $primary : 'unknown'); |
| | | |
| | | if ($secondary) { |
| | | $classes[] = $secondary; |
| | | } |
| | | if (preg_match('/\.([a-z0-9]+)$/i', $filename, $m)) { |
| | | $classes[] = $m[1]; |
| | | |
| | | if (preg_match('/\.([a-z0-9]+)$/', $filename, $m)) { |
| | | if (!in_array($m[1], $classes)) { |
| | | $classes[] = $m[1]; |
| | | } |
| | | } |
| | | |
| | | return strtolower(join(" ", $classes)); |
| | | return join(" ", $classes); |
| | | } |
| | | |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * rcube:utils::file2class() |
| | | */ |
| | | function test_file2class() |
| | | { |
| | | $test = array( |
| | | array('', '', 'unknown'), |
| | | array('text', 'text', 'text'), |
| | | array('image/png', 'image.png', 'image png'), |
| | | ); |
| | | |
| | | foreach ($test as $v) { |
| | | $result = rcube_utils::file2class($v[0], $v[1]); |
| | | $this->assertSame($v[2], $result); |
| | | } |
| | | } |
| | | } |