function testShortMime() { // Valid short ones assert(Mime::JSON === Mime::getFullMime('json')); assert(Mime::XML === Mime::getFullMime('xml')); assert(Mime::HTML === Mime::getFullMime('html')); // Valid long ones assert(Mime::JSON === Mime::getFullMime(Mime::JSON)); assert(Mime::XML === Mime::getFullMime(Mime::XML)); assert(Mime::HTML === Mime::getFullMime(Mime::HTML)); // No false positives assert(Mime::XML !== Mime::getFullMime(Mime::HTML)); assert(Mime::JSON !== Mime::getFullMime(Mime::XML)); assert(Mime::HTML !== Mime::getFullMime(Mime::JSON)); }
/** * After we've parse the headers, let's clean things * up a bit and treat some headers specially */ public function _interpretHeaders() { // Parse the Content-Type and charset $content_type = isset($this->headers['Content-Type']) ? $this->headers['Content-Type'] : ''; $content_type = explode(';', $content_type); $this->content_type = $content_type[0]; if (count($content_type) == 2 && strpos($content_type[1], '=') !== false) { list($nill, $this->charset) = explode('=', $content_type[1]); } // RFC 2616 states "text/*" Content-Types should have a default // charset of ISO-8859-1. "application/*" and other Content-Types // are assumed to have UTF-8 unless otherwise specified. // http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7.1 // http://www.w3.org/International/O-HTTP-charset.en.php if (!isset($this->charset)) { $this->charset = substr($this->content_type, 5) === 'text/' ? 'iso-8859-1' : 'utf-8'; } // Is vendor type? Is personal type? if (strpos($this->content_type, '/') !== false) { list($type, $sub_type) = explode('/', $this->content_type); $this->is_mime_vendor_specific = substr($sub_type, 0, 4) === 'vnd.'; $this->is_mime_personal = substr($sub_type, 0, 4) === 'prs.'; } // Parent type (e.g. xml for application/vnd.github.message+xml) $this->parent_type = $this->content_type; if (strpos($this->content_type, '+') !== false) { list($vendor, $this->parent_type) = explode('+', $this->content_type, 2); $this->parent_type = Mime::getFullMime($this->parent_type); } }
/** * Magic method allows for neatly setting other headers in a * similar syntax as the other setters. This method also allows * for the sends* syntax. * @return Request this * @param string $method "missing" method name called * the method name called should be the name of the header that you * are trying to set in camel case without dashes e.g. to set a * header for Content-Type you would use contentType() or more commonly * to add a custom header like X-My-Header, you would use xMyHeader(). * To promote readability, you can optionally prefix these methods with * "with" (e.g. withXMyHeader("blah") instead of xMyHeader("blah")). * @param array $args in this case, there should only ever be 1 argument provided * and that argument should be a string value of the header we're setting */ public function __call($method, $args) { // This method supports the sends* methods // like sendsJSON, sendsForm //!method_exists($this, $method) && if (substr($method, 0, 5) === 'sends') { $mime = strtolower(substr($method, 5)); if (Mime::supportsMimeType($mime)) { $this->sends(Mime::getFullMime($mime)); return $this; } // else { // throw new \Exception("Unsupported Content-Type $mime"); // } } if (substr($method, 0, 7) === 'expects') { $mime = strtolower(substr($method, 7)); if (Mime::supportsMimeType($mime)) { $this->expects(Mime::getFullMime($mime)); return $this; } // else { // throw new \Exception("Unsupported Content-Type $mime"); // } } // This method also adds the custom header support as described in the // method comments if (count($args) === 0) { return; } // Strip the sugar. If it leads with "with", strip. // This is okay because: No defined HTTP headers begin with with, // and if you are defining a custom header, the standard is to prefix it // with an "X-", so that should take care of any collisions. if (substr($method, 0, 4) === 'with') { $method = substr($method, 4); } // Precede upper case letters with dashes, uppercase the first letter of method $header = ucwords(implode('-', preg_split('/([A-Z][^A-Z]*)/', $method, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY))); $this->addHeader($header, $args[0]); return $this; }