aboutsummaryrefslogtreecommitdiffstats
path: root/Mime.php
diff options
context:
space:
mode:
authorIvan Enderlin <ivan.enderlin@hoa-project.net>2013-04-12 16:36:29 +0200
committerIvan Enderlin <ivan.enderlin@hoa-project.net>2013-04-12 16:36:29 +0200
commit2a64be5adc43cc676e7964e6bc69ffc6936727e7 (patch)
tree6826b205a2e0aa5c266430ce9d1289087b81deeb /Mime.php
parentc2b54e295031562bd4bf0b1d60cd6a58f0ceda18 (diff)
downloadMime-2a64be5adc43cc676e7964e6bc69ffc6936727e7.zip
Mime-2a64be5adc43cc676e7964e6bc69ffc6936727e7.tar.gz
Mime-2a64be5adc43cc676e7964e6bc69ffc6936727e7.tar.bz2
Add the getExtensionsFromMime() method.
And remove some exceptions.
Diffstat (limited to 'Mime.php')
-rw-r--r--Mime.php41
1 files changed, 30 insertions, 11 deletions
diff --git a/Mime.php b/Mime.php
index f050906..43f0d9f 100644
--- a/Mime.php
+++ b/Mime.php
@@ -206,7 +206,10 @@ class Mime implements \Hoa\Core\Parameter\Parameterizable {
* @return void
* @throw \Hoa\Mime\Exception
*/
- public static function compute ( $magic ) {
+ public static function compute ( $magic = null ) {
+
+ if(null === $magic)
+ $magic = 'hoa://Library/Mime/Mime.types';
if(!file_exists($magic))
throw new Exception(
@@ -293,32 +296,48 @@ class Mime implements \Hoa\Core\Parameter\Parameterizable {
* @access public
* @param string $extension Extension to check.
* @return bool
- * @throw \Hoa\Mime\Exception
*/
public static function extensionExists ( $extension ) {
if(null === static::$_computed)
- throw new Exception(
- 'No MIME type has been computed.', 3);
+ static::compute();
return isset(static::$_computed[self::STRUCTURE_EXTENSION][$extension]);
}
/**
+ * Get extensions from a MIME (media/type).
+ *
+ * @access public
+ * @param string $mime MIME.
+ * @return array
+ * @throw \Hoa\Mime\Exception\MimeIsNotFound
+ */
+ public static function getExtensionsFromMime( $mime ) {
+
+ if(null === static::$_computed)
+ static::compute();
+
+ list($media, $type) = static::parseMime($mime);
+
+ if(!isset(static::$_computed[self::STRUCTURE_MEDIA_TYPE][$media][$type]))
+ throw new Exception\MimeIsNotFound(
+ 'MIME type %s does not exist.', 2, $mime);
+
+ return static::$_computed[self::STRUCTURE_MEDIA_TYPE][$media][$type];
+ }
+
+ /**
* Get MIME (media/type) from extension.
*
* @access public
* @param string $extension Extension to considere.
* @return string
- * @throw \Hoa\Mime\Exception
- * @throw \Hoa\Mime\Exception\MimeIsNotFound
*/
public static function getMimeFromExtension ( $extension ) {
if(false === static::extensionExists($extension))
- throw new Exception\MimeIsNotFound(
- 'Cannot find MIME type because extension %s does ' .
- 'not exist.', 4, $extension);
+ return null;
return static::$_computed[self::STRUCTURE_EXTENSION][$extension];
}
@@ -336,7 +355,7 @@ class Mime implements \Hoa\Core\Parameter\Parameterizable {
if(false === strpos($mime, '/'))
throw new Exception(
- 'MIME %s is not well-formed (media/type).', 5, $mime);
+ 'MIME %s is not well-formed (media/type).', 3, $mime);
return explode('/', $mime);
}
@@ -361,7 +380,7 @@ class Mime implements \Hoa\Core\Parameter\Parameterizable {
if(false === $poos = strrpos($based, '.'))
throw new Exception\MimeIsNotFound(
'Cannot find MIME type of %s, because extension is not found.',
- 2, $name);
+ 4, $name);
$this->_extension = substr($based, $poos + 1);
$this->_mime = static::getMimeFromExtension($this->_extension);