Example #1
0
 /**
  * {@inheritdoc}
  */
 public function load($filename)
 {
     $mediaTypes = new MediaTypeCollection();
     $xml = simplexml_load_file($filename);
     foreach ($xml->mediatype as $mediatypeNode) {
         $attrs = $mediatypeNode->attributes();
         $name = (string) $attrs['name'];
         $category = (string) $attrs['category'];
         $mimetypes = [];
         if ($mediatypeNode->mimetypes->count() && $mediatypeNode->mimetypes->mimetype->count()) {
             foreach ($mediatypeNode->mimetypes->mimetype as $mimetypeNode) {
                 $mimetypes[] = (string) $mimetypeNode;
             }
         }
         $attributes = [];
         if ($mediatypeNode->attributes->count() && $mediatypeNode->attributes->attribute->count()) {
             foreach ($mediatypeNode->attributes->attribute as $attributeNode) {
                 $attrs = $attributeNode->attributes();
                 $attributes[(string) $attrs['key']] = (string) $attributeNode;
             }
         }
         $mediaType = new MediaType($name, $category, $mimetypes, $attributes);
         $mediaTypes->add($mediaType);
     }
     return $mediaTypes;
 }
 public function testGetCollection()
 {
     $mediaTypes = new MediaTypeCollection();
     $mediaTypes->add(new MediaType('gif', 'image', ['image/gif']));
     $classifier = new MediaClassifier($mediaTypes, 'image:gif');
     $this->assertSame($mediaTypes, $classifier->getCollection());
 }
 /**
  * @return MediaType|null
  */
 public function classify($filename)
 {
     $file = new File($filename);
     $mimetype = $file->getMimeType();
     $mediaType = null;
     if ($mimetype) {
         $mediaType = $this->mediaTypes->lookup($mimetype);
     }
     if (!$mediaType) {
         $mediaType = $this->mediaTypes->get($this->fallbackMediaType);
     }
     return $mediaType;
 }
 public function testHash()
 {
     $mediaType = $this->prophesize('Temp\\MediaClassifier\\Model\\MediaType');
     $mediaType->__toString()->willReturn('abc');
     $mediaType->getMimetypes()->willReturn([]);
     $mediaType->getHash()->willReturn('abc')->shouldBeCalled();
     $this->mediatypes->add($mediaType->reveal());
     $this->assertNotEmpty($this->mediatypes->getHash());
 }