# ExtensionGuesser

class ExtensionGuesser implements ExtensionGuesserInterface

A singleton mime type to file extension guesser.

A default guesser is provided. You can register custom guessers by calling the register() method on the singleton instance:

    $guesser = ExtensionGuesser::getInstance();
    $guesser->register(new MyCustomExtensionGuesser());

The last registered guesser is preferred over previously registered ones.

## Properties

`protected array` | `$guessers` | All registered ExtensionGuesserInterface instances. |

## Methods

`public static` | `ExtensionGuesser` | `getInstance()` | Returns the singleton instance. |
`public` | `` | `register(ExtensionGuesserInterface $guesser)` | Registers a new extension guesser. |
`public` | `string` | `guess(string $mimeType)` | Tries to guess the extension. |

## Details

at line 47

### `getInstance()`

`public static ExtensionGuesser getInstance()`

Returns the singleton instance.

#### Return Value

`ExtensionGuesser` |

at line 69

### `register()`

`public register(ExtensionGuesserInterface $guesser)`

Registers a new extension guesser.

When guessing, this guesser is preferred over previously registered ones.

#### Parameters

`ExtensionGuesserInterface` | `$guesser` |

at line 86

### `guess()`

`public string guess(string $mimeType)`

Tries to guess the extension.

The mime type is passed to each registered mime type guesser in reverse order of their registration (last registered is queried first). Once a guesser returns a value that is not NULL, this method terminates and returns the value.

#### Parameters

`string` | `$mimeType` | The mime type

#### Return Value

`string` | The guessed extension or NULL, if none could be guessed

## Source code

    <?php

    /*
     * This file is part of the Symfony package.
     *
     * (c) Fabien Potencier <fabien@symfony.com>
     *
     * For the full copyright and license information, please view the LICENSE
     * file that was distributed with this source code.
     */

    namespace Symfony\Component\HttpFoundation\File\MimeType;

    /**
     * A singleton mime type to file extension guesser.
     *
     * A default guesser is provided.
     * You can register custom guessers by calling the register()
     * method on the singleton instance:
     *
     *     $guesser = ExtensionGuesser::getInstance();
     *     $guesser->register(new MyCustomExtensionGuesser());
     *
     * The last registered guesser is preferred over previously registered ones.
     */
    class ExtensionGuesser implements ExtensionGuesserInterface
    {
        /**
         * The singleton instance.
         *
         * @var ExtensionGuesser
         */
        private static $instance = null;

        /**
         * All registered ExtensionGuesserInterface instances.
         *
         * @var array
         */
        protected $guessers = array();

        /**
         * Returns the singleton instance.
         *
         * @return self
         */
        public static function getInstance()
        {
            if (null === self::$instance) {
                self::$instance = new self();
            }

            return self::$instance;
        }

        /**
         * Registers all natively provided extension guessers.
         */
        private function __construct()
        {
            $this->register(new MimeTypeExtensionGuesser());
        }

        /**
         * Registers a new extension guesser.
         *
         * When guessing, this guesser is preferred over previously registered ones.
         */
        public function register(ExtensionGuesserInterface $guesser)
        {
            array_unshift($this->guessers, $guesser);
        }

        /**
         * Tries to guess the extension.
         *
         * The mime type is passed to each registered mime type guesser in reverse order
         * of their registration (last registered is queried first). Once a guesser
         * returns a value that is not NULL, this method terminates and returns the
         * value.
         *
         * @param string $mimeType The mime type
         *
         * @return string The guessed extension or NULL, if none could be guessed
         */
        public function guess($mimeType)
        {
            foreach ($this->guessers as $guesser) {
                if (null !== $extension = $guesser->guess($mimeType)) {
                    return $extension;
                }
            }
        }
    }