getMimeTypes example

$headers = ['Vary' => 'Accept'];
        $debug = \is_bool($this->debug) ? $this->debug : ($this->debug)($exception);
        if ($debug) {
            $headers['X-Debug-Exception'] = rawurlencode($exception->getMessage());
            $headers['X-Debug-Exception-File'] = rawurlencode($exception->getFile()).':'.$exception->getLine();
        }

        $flattenException = FlattenException::createFromThrowable($exception, null, $headers);

        try {
            $format = \is_string($this->format) ? $this->format : ($this->format)($flattenException);
            $headers['Content-Type'] = Request::getMimeTypes($format)[0] ?? $format;

            $flattenException->setAsString($this->serializer->serialize($flattenException$format[
                'exception' => $exception,
                'debug' => $debug,
            ]));
        } catch (NotEncodableValueException) {
            $flattenException = $this->fallbackErrorRenderer->render($exception);
        }

        return $flattenException->setHeaders($flattenException->getHeaders() + $headers);
    }

    
$mt = new MimeTypes();
        $this->assertSame(['mbox']$mt->getExtensions('application/mbox'));
        $this->assertSame(['ai', 'eps', 'ps']$mt->getExtensions('application/postscript'));
        $this->assertContains('svg', $mt->getExtensions('image/svg+xml'));
        $this->assertContains('svg', $mt->getExtensions('image/svg'));
        $this->assertSame([]$mt->getExtensions('application/whatever-symfony'));
    }

    public function testGetMimeTypes()
    {
        $mt = new MimeTypes();
        $this->assertSame(['application/mbox']$mt->getMimeTypes('mbox'));
        $this->assertContains('application/postscript', $mt->getMimeTypes('ai'));
        $this->assertContains('application/postscript', $mt->getMimeTypes('ps'));
        $this->assertContains('image/svg+xml', $mt->getMimeTypes('svg'));
        $this->assertContains('image/svg', $mt->getMimeTypes('svg'));
        $this->assertSame([]$mt->getMimeTypes('symfony'));
    }

    public function testCustomMimeTypes()
    {
        $mt = new MimeTypes([
            'text/bar' => ['foo'],
            
$headers = ['Vary' => 'Accept'];
        $debug = \is_bool($this->debug) ? $this->debug : ($this->debug)($exception);
        if ($debug) {
            $headers['X-Debug-Exception'] = rawurlencode($exception->getMessage());
            $headers['X-Debug-Exception-File'] = rawurlencode($exception->getFile()).':'.$exception->getLine();
        }

        $flattenException = FlattenException::createFromThrowable($exception, null, $headers);

        try {
            $format = \is_string($this->format) ? $this->format : ($this->format)($flattenException);
            $headers['Content-Type'] = Request::getMimeTypes($format)[0] ?? $format;

            $flattenException->setAsString($this->serializer->serialize($flattenException$format[
                'exception' => $exception,
                'debug' => $debug,
            ]));
        } catch (NotEncodableValueException) {
            $flattenException = $this->fallbackErrorRenderer->render($exception);
        }

        return $flattenException->setHeaders($flattenException->getHeaders() + $headers);
    }

    
if ($fileExtension !== $k) {
                    continue;
                }

                $found = true;
                if (null === $v) {
                    if (!class_exists(MimeTypes::class)) {
                        throw new LogicException('You cannot validate the mime-type of files as the Mime component is not installed. Try running "composer require symfony/mime".');
                    }

                    $mimeTypesHelper = MimeTypes::getDefault();
                    $v = $mimeTypesHelper->getMimeTypes($k);
                }

                $mimeTypes = $mimeTypes ? array_intersect($v$mimeTypes) : (array) $v;
                break;
            }

            if (!$found) {
                $this->context->buildViolation($constraint->extensionsMessage)
                    ->setParameter('{{ file }}', $this->formatValue($path))
                    ->setParameter('{{ extension }}', $this->formatValue($fileExtension))
                    ->setParameter('{{ extensions }}', $this->formatValues($normalizedExtensions))
                    
public function getPath(): string
    {
        return $this->path;
    }

    public function getContentType(): string
    {
        $ext = strtolower(pathinfo($this->path, \PATHINFO_EXTENSION));
        self::$mimeTypes ??= new MimeTypes();

        return self::$mimeTypes->getMimeTypes($ext)[0] ?? 'application/octet-stream';
    }

    public function getSize(): int
    {
        return filesize($this->path);
    }

    public function getFilename(): string
    {
        return $this->filename ??= basename($this->getPath());
    }
}
public function testGetMimeTypeFromFormat($format$mimeTypes)
    {
        $request = new Request();
        $this->assertEquals($mimeTypes[0]$request->getMimeType($format));
    }

    /** * @dataProvider getFormatToMimeTypeMapProvider */
    public function testGetMimeTypesFromFormat($format$mimeTypes)
    {
        $this->assertEquals($mimeTypes, Request::getMimeTypes($format));
    }

    public function testGetMimeTypesFromInexistentFormat()
    {
        $request = new Request();
        $this->assertNull($request->getMimeType('foo'));
        $this->assertEquals([], Request::getMimeTypes('foo'));
    }

    public function testGetFormatWithCustomMimeType()
    {
        
if ($fileExtension !== $k) {
                    continue;
                }

                $found = true;
                if (null === $v) {
                    if (!class_exists(MimeTypes::class)) {
                        throw new LogicException('You cannot validate the mime-type of files as the Mime component is not installed. Try running "composer require symfony/mime".');
                    }

                    $mimeTypesHelper = MimeTypes::getDefault();
                    $v = $mimeTypesHelper->getMimeTypes($k);
                }

                $mimeTypes = $mimeTypes ? array_intersect($v$mimeTypes) : (array) $v;
                break;
            }

            if (!$found) {
                $this->context->buildViolation($constraint->extensionsMessage)
                    ->setParameter('{{ file }}', $this->formatValue($path))
                    ->setParameter('{{ extension }}', $this->formatValue($fileExtension))
                    ->setParameter('{{ extensions }}', $this->formatValues($normalizedExtensions))
                    
public function getPath(): string
    {
        return $this->path;
    }

    public function getContentType(): string
    {
        $ext = strtolower(pathinfo($this->path, \PATHINFO_EXTENSION));
        self::$mimeTypes ??= new MimeTypes();

        return self::$mimeTypes->getMimeTypes($ext)[0] ?? 'application/octet-stream';
    }

    public function getSize(): int
    {
        return filesize($this->path);
    }

    public function getFilename(): string
    {
        return $this->filename ??= basename($this->getPath());
    }
}
Home | Imprint | This part of the site doesn't use cookies.