Example #1
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;
 }