Overview

Namespaces

  • Contrib
    • Bundle
      • CoverallsBundle
        • Console
        • Entity
      • CoverallsV1Bundle
        • Api
        • Collector
        • Command
        • Config
        • Entity
          • Git
    • Component
      • File
      • Log
      • System
        • Git
  • Guzzle
    • Batch
      • Exception
    • Cache
    • Common
      • Exception
    • Http
      • Curl
      • Exception
      • Message
      • QueryAggregator
    • Inflection
    • Iterator
    • Log
    • Parser
      • Cookie
      • Message
      • UriTemplate
      • Url
    • Plugin
      • Async
      • Backoff
      • Cache
      • Cookie
        • CookieJar
        • Exception
      • CurlAuth
      • ErrorResponse
        • Exception
      • History
      • Log
      • Md5
      • Mock
      • Oauth
    • Service
      • Builder
      • Command
        • Factory
        • LocationVisitor
          • Request
          • Response
      • Description
      • Exception
      • Resource
    • Stream
  • PHP
  • Psr
    • Log
  • Symfony
    • Component
      • Config
        • Definition
          • Builder
          • Exception
        • Exception
        • Loader
        • Resource
        • Util
      • Console
        • Command
        • Formatter
        • Helper
        • Input
        • Output
        • Tester
      • EventDispatcher
        • Debug
      • Finder
        • Adapter
        • Comparator
        • Exception
        • Expression
        • Iterator
        • Shell
      • Stopwatch
      • Yaml
        • Exception

Classes

  • CookieParser

Interfaces

  • CookieParserInterface
  • Overview
  • Namespace
  • Class
  • Tree
  • Todo
 1: <?php
 2: 
 3: namespace Guzzle\Parser\Cookie;
 4: 
 5: /**
 6:  * Default Guzzle implementation of a Cookie parser
 7:  */
 8: class CookieParser implements CookieParserInterface
 9: {
10:     /**
11:      * @var array Cookie part names to snake_case array values
12:      */
13:     protected static $cookieParts = array(
14:         'domain'      => 'Domain',
15:         'path'        => 'Path',
16:         'max_age'     => 'Max-Age',
17:         'expires'     => 'Expires',
18:         'version'     => 'Version',
19:         'secure'      => 'Secure',
20:         'port'        => 'Port',
21:         'discard'     => 'Discard',
22:         'comment'     => 'Comment',
23:         'comment_url' => 'Comment-Url',
24:         'http_only'   => 'HttpOnly'
25:     );
26: 
27:     /**
28:      * {@inheritdoc}
29:      */
30:     public function parseCookie($cookie, $host = null, $path = null, $decode = false)
31:     {
32:         // Explode the cookie string using a series of semicolons
33:         $pieces = array_filter(array_map('trim', explode(';', $cookie)));
34: 
35:         // The name of the cookie (first kvp) must include an equal sign.
36:         if (empty($pieces) || !strpos($pieces[0], '=')) {
37:             return false;
38:         }
39: 
40:         // Create the default return array
41:         $data = array_merge(array_fill_keys(array_keys(self::$cookieParts), null), array(
42:             'cookies'   => array(),
43:             'data'      => array(),
44:             'path'      => $path ?: '/',
45:             'http_only' => false,
46:             'discard'   => false,
47:             'domain'    => $host
48:         ));
49:         $foundNonCookies = 0;
50: 
51:         // Add the cookie pieces into the parsed data array
52:         foreach ($pieces as $part) {
53: 
54:             $cookieParts = explode('=', $part, 2);
55:             $key = trim($cookieParts[0]);
56: 
57:             if (count($cookieParts) == 1) {
58:                 // Can be a single value (e.g. secure, httpOnly)
59:                 $value = true;
60:             } else {
61:                 // Be sure to strip wrapping quotes
62:                 $value = trim($cookieParts[1], " \n\r\t\0\x0B\"");
63:                 if ($decode) {
64:                     $value = urldecode($value);
65:                 }
66:             }
67: 
68:             // Only check for non-cookies when cookies have been found
69:             if (!empty($data['cookies'])) {
70:                 foreach (self::$cookieParts as $mapValue => $search) {
71:                     if (!strcasecmp($search, $key)) {
72:                         $data[$mapValue] = $mapValue == 'port' ? array_map('trim', explode(',', $value)) : $value;
73:                         $foundNonCookies++;
74:                         continue 2;
75:                     }
76:                 }
77:             }
78: 
79:             // If cookies have not yet been retrieved, or this value was not found in the pieces array, treat it as a
80:             // cookie. IF non-cookies have been parsed, then this isn't a cookie, it's cookie data. Cookies then data.
81:             $data[$foundNonCookies ? 'data' : 'cookies'][$key] = $value;
82:         }
83: 
84:         // Calculate the expires date
85:         if (!$data['expires'] && $data['max_age']) {
86:             $data['expires'] = time() + (int) $data['max_age'];
87:         }
88: 
89:         return $data;
90:     }
91: }
92: 
php-coveralls API documentation generated by ApiGen 2.8.0