Example #1
0
 /**
  * Creates a cookie object from a string
  * 
  * @param   string                      The cookie string
  * @return  Woops_Http_Cookie           The cookie object
  * @throws  Woops_Http_Cookie_Exception If the cookie string cannot be parsed
  */
 public static function createCookieObject($str)
 {
     // Finds the position of the first '=' character
     $equal = strpos($str, '=');
     // Checks for the '=' character
     if (!$equal) {
         // Invalid cookie - No '=' character
         throw new Woops_Http_Cookie_Exception('Invalid cookie: \'' . $str . '\'', Woops_Http_Cookie_Exception::EXCEPTION_BAD_COOKIE);
     }
     // Gets the cookie's name
     $name = trim(substr($str, 0, $equal));
     // Gets the cookie options
     $options = trim(substr($str, $equal + 1));
     // Gets the cookie's options' parts
     $parts = explode(';', $options);
     // Gets the cookie valie
     $value = trim(array_shift($parts));
     // Creates the cookie object
     $cookie = new self($name, $value);
     // Process each part
     foreach ($parts as $part) {
         // Position of the '=' character
         $equal = strpos($part, '=');
         // Checks for the '=' character
         if (!$equal) {
             // Option without a value
             $name = trim($part);
         } else {
             // Gets the name and the value of the option
             $name = trim(substr($part, 0, $equal));
             $value = trim(substr($part, $equal + 1));
         }
         // Checks the option name
         switch ($name) {
             // Expiration date
             case 'expires':
                 // Sets the expiration date (as a timestamp)
                 $cookie->setExpires(strtotime($value));
                 break;
                 // Path
             // Path
             case 'path':
                 // Sets the cookie's path
                 $cookie->setPath($value);
                 break;
                 // Domain
             // Domain
             case 'domain':
                 // Sets the cookie's domain
                 $cookie->setDomain($value);
                 break;
                 // Secure option
             // Secure option
             case 'secure':
                 // The cookie is secure
                 $cookie->setSecure(true);
                 break;
                 // HTTP only option
             // HTTP only option
             case 'HttpOnly':
                 // The cookie is accessible only through the HTTP protocol
                 $cookie->setHttpOnly(true);
                 break;
                 // Unknown option
             // Unknown option
             default:
                 break;
         }
     }
     // Returns the cookie object
     return $cookie;
 }