You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
984 B

  1. <?php
  2. namespace p3k\XRay;
  3. class MediaType {
  4. public $type;
  5. public $subtype;
  6. public $format;
  7. public $charset;
  8. // Parse a media type into component parts: type, subtype, format, charset
  9. // e.g. "application/json" => type: application, subtype: json, format: json
  10. // "application/ld+json" => type: application, subtype: "ld+json", format: json
  11. public function __construct($string) {
  12. if(strstr($string, ';')) {
  13. list($type, $parameters) = explode(';', $string, 2);
  14. $parameters = explode(';', $parameters);
  15. foreach($parameters as $p) {
  16. list($k, $v) = explode('=', trim($p));
  17. if($k == 'charset')
  18. $this->charset = $v;
  19. }
  20. } else {
  21. $type = $string;
  22. }
  23. list($type, $subtype) = explode('/', $type);
  24. $this->type = $type;
  25. $this->subtype = $subtype;
  26. $this->format = $subtype;
  27. if(strstr($subtype, '+')) {
  28. list($a, $b) = explode('+', $subtype, 2);
  29. $this->format = $b;
  30. }
  31. }
  32. }