Sulu\Bundle\MediaBundle\Media\TypeManager\TypeManager::getMediaType PHP Method

getMediaType() public method

public getMediaType ( $fileMimeType )
    public function getMediaType($fileMimeType)
    {
        $name = null;
        foreach ($this->mediaTypes as $mediaType) {
            foreach ($mediaType['mimeTypes'] as $mimeType) {
                if (fnmatch($mimeType, $fileMimeType)) {
                    $name = $mediaType['type'];
                }
            }
        }
        if (!isset($this->mediaTypeEntities[$name])) {
            $mediaType = $this->objectManager->getRepository(self::ENTITY_CLASS_MEDIATYPE)->findOneByName($name);
            $this->mediaTypeEntities[$name] = $mediaType;
        }
        return $this->mediaTypeEntities[$name]->getId();
    }

Usage Example

Example #1
0
 public function testMediaTypes()
 {
     $data = ['application/pdf' => 'document', 'application/msword' => 'document', 'application/vnd.ms-excel' => 'document', 'application/zip' => 'document', 'text/html' => 'document', 'image/jpg' => 'image', 'image/jepg' => 'image', 'image/gif' => 'image', 'image/png' => 'image', 'image/vnd.adobe.photoshop' => 'image', 'video/mp4' => 'video', 'video/mov' => 'video', 'audio/mpeg' => 'audio', 'audio/mp3' => 'audio'];
     foreach ($data as $mimeType => $mediaType) {
         $mediaTypeName = null;
         $id = $this->typeManager->getMediaType($mimeType);
         $setMediaType = $this->typeManager->get($id);
         if ($setMediaType) {
             $mediaTypeName = $setMediaType->getName();
         }
         $this->assertEquals($mediaTypeName, $mediaType);
     }
 }