Further modules included.
[yaffs-website] / web / modules / contrib / filefield_sources / src / File / MimeType / ExtensionMimeTypeGuesser.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\filefield_sources\File\MimeType\ExtensionMimeTypeGuesser.
6  */
7
8 namespace Drupal\filefield_sources\File\MimeType;
9
10 use Drupal\Core\File\MimeType\ExtensionMimeTypeGuesser as CoreExtensionMimeTypeGuesser;
11
12 /**
13  * Add methods to core guesser.
14  */
15 class ExtensionMimeTypeGuesser extends CoreExtensionMimeTypeGuesser {
16
17   /**
18    * Convert mime type to extension.
19    *
20    * @param string $mimetype
21    *   Mime type.
22    *
23    * @return string|bool
24    *   Return extension if found, FALSE otherwise.
25    */
26   public function convertMimeTypeToExtension($mimetype) {
27     $this->checkDefaultMapping();
28
29     $mime_key = array_search($mimetype, $this->mapping['mimetypes']);
30     $extension = array_search($mime_key, $this->mapping['extensions']);
31
32     return $extension;
33   }
34
35   /**
36    * Convert mime type to most common extension.
37    *
38    * @param string $mimetype
39    *   Mime type.
40    *
41    * @return string|bool
42    *   Return extension if found, FALSE otherwise.
43    */
44   public function convertMimeTypeToMostCommonExtension($mimetype) {
45     $this->checkDefaultMapping();
46
47     $extension = FALSE;
48     if (isset($mimetype)) {
49       // See if this matches a known MIME type.
50       $mime_key = array_search($mimetype, $this->mapping['mimetypes']);
51       if ($mime_key !== FALSE) {
52         // If we have a match, get this list of likely extensions. For some
53         // reason Drupal lists the "most common" extension last for most file
54         // types including php, jpg, and doc.
55         if ($extensions = array_keys($this->mapping['extensions'], $mime_key)) {
56           $extension = end($extensions);
57         }
58       }
59     }
60     return $extension;
61   }
62
63   /**
64    * Check for default mapping.
65    */
66   private function checkDefaultMapping() {
67     if ($this->mapping === NULL) {
68       $mapping = $this->defaultMapping;
69       // Allow modules to alter the default mapping.
70       $this->moduleHandler->alter('file_mimetype_mapping', $mapping);
71       $this->mapping = $mapping;
72     }
73   }
74
75 }