Mime.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace Httpful;
  3. /**
  4. * Class to organize the Mime stuff a bit more
  5. * @author Nate Good <me@nategood.com>
  6. */
  7. class Mime
  8. {
  9. const JSON = 'application/json';
  10. const XML = 'application/xml';
  11. const XHTML = 'application/html+xml';
  12. const FORM = 'application/x-www-form-urlencoded';
  13. const UPLOAD = 'multipart/form-data';
  14. const PLAIN = 'text/plain';
  15. const JS = 'text/javascript';
  16. const HTML = 'text/html';
  17. const YAML = 'application/x-yaml';
  18. const CSV = 'text/csv';
  19. /**
  20. * Map short name for a mime type
  21. * to a full proper mime type
  22. */
  23. public static $mimes = array(
  24. 'json' => self::JSON,
  25. 'xml' => self::XML,
  26. 'form' => self::FORM,
  27. 'plain' => self::PLAIN,
  28. 'text' => self::PLAIN,
  29. 'upload' => self::UPLOAD,
  30. 'html' => self::HTML,
  31. 'xhtml' => self::XHTML,
  32. 'js' => self::JS,
  33. 'javascript'=> self::JS,
  34. 'yaml' => self::YAML,
  35. 'csv' => self::CSV,
  36. );
  37. /**
  38. * Get the full Mime Type name from a "short name".
  39. * Returns the short if no mapping was found.
  40. * @param string $short_name common name for mime type (e.g. json)
  41. * @return string full mime type (e.g. application/json)
  42. */
  43. public static function getFullMime($short_name)
  44. {
  45. return array_key_exists($short_name, self::$mimes) ? self::$mimes[$short_name] : $short_name;
  46. }
  47. /**
  48. * @param string $short_name
  49. * @return bool
  50. */
  51. public static function supportsMimeType($short_name)
  52. {
  53. return array_key_exists($short_name, self::$mimes);
  54. }
  55. }