Пример #1
0
 function testShortMime()
 {
     // Valid short ones
     $this->assertEquals(Mime::JSON, Mime::getFullMime('json'));
     $this->assertEquals(Mime::XML, Mime::getFullMime('xml'));
     $this->assertEquals(Mime::HTML, Mime::getFullMime('html'));
     $this->assertEquals(Mime::CSV, Mime::getFullMime('csv'));
     // Valid long ones
     $this->assertEquals(Mime::JSON, Mime::getFullMime(Mime::JSON));
     $this->assertEquals(Mime::XML, Mime::getFullMime(Mime::XML));
     $this->assertEquals(Mime::HTML, Mime::getFullMime(Mime::HTML));
     $this->assertEquals(Mime::CSV, Mime::getFullMime(Mime::CSV));
     // No false positives
     $this->assertNotEquals(Mime::XML, Mime::getFullMime(Mime::HTML));
     $this->assertNotEquals(Mime::JSON, Mime::getFullMime(Mime::XML));
     $this->assertNotEquals(Mime::HTML, Mime::getFullMime(Mime::JSON));
     $this->assertNotEquals(Mime::XML, Mime::getFullMime(Mime::CSV));
 }
Пример #2
0
 /**
  * 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;
 }
Пример #3
0
 /**
  * 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) {
         /** @noinspection PhpUnusedLocalVariableInspection */
         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) {
         /** @noinspection PhpUnusedLocalVariableInspection */
         list($type, $sub_type) = explode('/', $this->content_type);
         $this->is_mime_vendor_specific = 0 === strpos($sub_type, 'vnd.');
         $this->is_mime_personal = 0 === strpos($sub_type, 'prs.');
     }
     // Parent type (e.g. xml for application/vnd.github.message+xml)
     $this->parent_type = $this->content_type;
     if (strpos($this->content_type, '+') !== false) {
         /** @noinspection PhpUnusedLocalVariableInspection */
         list($vendor, $this->parent_type) = explode('+', $this->content_type, 2);
         $this->parent_type = Mime::getFullMime($this->parent_type);
     }
 }
Пример #4
0
 /**
  * converts the data to the specified data type
  * 
  * @param string $type    enconding type
  * @param array $data      data to be converted
  */
 protected function respond($type, $data, $raw = false)
 {
     $this->log->debug("Trying to respond with " . print_r($data, true));
     $contentType = \Httpful\Mime::getFullMime($type);
     if ($raw) {
         header('Content-type: ' . $contentType);
         echo $data;
         return;
     }
     switch ($type) {
         case 'json':
             $method = 'toJSON';
             break;
         case 'xml':
             $method = 'toXML';
             break;
     }
     header('Content-type: ' . $contentType);
     if (isset($method) && is_callable(array($data, $method))) {
         echo call_user_func(array($data, $method));
     } else {
         try {
             $handler = new Handler();
             echo $handler->serialize($contentType, $data);
         } catch (RestException $re) {
             //everithing failed... this must be a string!
             $this->log->info("hello");
             echo $data;
         }
     }
 }