GuzzleHttpCookieSetCookie

Set-Cookie object.

Defined (1)

The class is defined in the following location(s).

/lib/Azure/GuzzleHttp/Cookie/SetCookie.php  
  1. class SetCookie 
  2. /** @var array */ 
  3. private static $defaults = [ 
  4. 'Name' => null,  
  5. 'Value' => null,  
  6. 'Domain' => null,  
  7. 'Path' => '/',  
  8. 'Max-Age' => null,  
  9. 'Expires' => null,  
  10. 'Secure' => false,  
  11. 'Discard' => false,  
  12. 'HttpOnly' => false 
  13. ]; 
  14.  
  15. /** @var array Cookie data */ 
  16. private $data; 
  17.  
  18. /** 
  19. * Create a new SetCookie object from a string 
  20. * @param string $cookie Set-Cookie header string 
  21. * @return self 
  22. */ 
  23. public static function fromString($cookie) 
  24. // Create the default return array 
  25. $data = self::$defaults; 
  26. // Explode the cookie string using a series of semicolons 
  27. $pieces = array_filter(array_map('trim', explode(';', $cookie))); 
  28. // The name of the cookie (first kvp) must include an equal sign. 
  29. if (empty($pieces) || !strpos($pieces[0], '=')) { 
  30. return new self($data); 
  31.  
  32. // Add the cookie pieces into the parsed data array 
  33. foreach ($pieces as $part) { 
  34.  
  35. $cookieParts = explode('=', $part, 2); 
  36. $key = trim($cookieParts[0]); 
  37. $value = isset($cookieParts[1]) 
  38. ? trim($cookieParts[1], " \n\r\t\0\x0B") 
  39. : true; 
  40.  
  41. // Only check for non-cookies when cookies have been found 
  42. if (empty($data['Name'])) { 
  43. $data['Name'] = $key; 
  44. $data['Value'] = $value; 
  45. } else { 
  46. foreach (array_keys(self::$defaults) as $search) { 
  47. if (!strcasecmp($search, $key)) { 
  48. $data[$search] = $value; 
  49. continue 2; 
  50. $data[$key] = $value; 
  51.  
  52. return new self($data); 
  53.  
  54. /** 
  55. * @param array $data Array of cookie data provided by a Cookie parser 
  56. */ 
  57. public function __construct(array $data = []) 
  58. $this->data = array_replace(self::$defaults, $data); 
  59. // Extract the Expires value and turn it into a UNIX timestamp if needed 
  60. if (!$this->getExpires() && $this->getMaxAge()) { 
  61. // Calculate the Expires date 
  62. $this->setExpires(time() + $this->getMaxAge()); 
  63. } elseif ($this->getExpires() && !is_numeric($this->getExpires())) { 
  64. $this->setExpires($this->getExpires()); 
  65.  
  66. public function __toString() 
  67. $str = $this->data['Name'] . '=' . $this->data['Value'] . '; '; 
  68. foreach ($this->data as $k => $v) { 
  69. if ($k !== 'Name' && $k !== 'Value' && $v !== null && $v !== false) { 
  70. if ($k === 'Expires') { 
  71. $str .= 'Expires=' . gmdate('D, d M Y H:i:s \G\M\T', $v) . '; '; 
  72. } else { 
  73. $str .= ($v === true ? $k : "{$k}={$v}") . '; '; 
  74.  
  75. return rtrim($str, '; '); 
  76.  
  77. public function toArray() 
  78. return $this->data; 
  79.  
  80. /** 
  81. * Get the cookie name 
  82. * @return string 
  83. */ 
  84. public function getName() 
  85. return $this->data['Name']; 
  86.  
  87. /** 
  88. * Set the cookie name 
  89. * @param string $name Cookie name 
  90. */ 
  91. public function setName($name) 
  92. $this->data['Name'] = $name; 
  93.  
  94. /** 
  95. * Get the cookie value 
  96. * @return string 
  97. */ 
  98. public function getValue() 
  99. return $this->data['Value']; 
  100.  
  101. /** 
  102. * Set the cookie value 
  103. * @param string $value Cookie value 
  104. */ 
  105. public function setValue($value) 
  106. $this->data['Value'] = $value; 
  107.  
  108. /** 
  109. * Get the domain 
  110. * @return string|null 
  111. */ 
  112. public function getDomain() 
  113. return $this->data['Domain']; 
  114.  
  115. /** 
  116. * Set the domain of the cookie 
  117. * @param string $domain 
  118. */ 
  119. public function setDomain($domain) 
  120. $this->data['Domain'] = $domain; 
  121.  
  122. /** 
  123. * Get the path 
  124. * @return string 
  125. */ 
  126. public function getPath() 
  127. return $this->data['Path']; 
  128.  
  129. /** 
  130. * Set the path of the cookie 
  131. * @param string $path Path of the cookie 
  132. */ 
  133. public function setPath($path) 
  134. $this->data['Path'] = $path; 
  135.  
  136. /** 
  137. * Maximum lifetime of the cookie in seconds 
  138. * @return int|null 
  139. */ 
  140. public function getMaxAge() 
  141. return $this->data['Max-Age']; 
  142.  
  143. /** 
  144. * Set the max-age of the cookie 
  145. * @param int $maxAge Max age of the cookie in seconds 
  146. */ 
  147. public function setMaxAge($maxAge) 
  148. $this->data['Max-Age'] = $maxAge; 
  149.  
  150. /** 
  151. * The UNIX timestamp when the cookie Expires 
  152. * @return mixed 
  153. */ 
  154. public function getExpires() 
  155. return $this->data['Expires']; 
  156.  
  157. /** 
  158. * Set the unix timestamp for which the cookie will expire 
  159. * @param int $timestamp Unix timestamp 
  160. */ 
  161. public function setExpires($timestamp) 
  162. $this->data['Expires'] = is_numeric($timestamp) 
  163. ? (int) $timestamp 
  164. : strtotime($timestamp); 
  165.  
  166. /** 
  167. * Get whether or not this is a secure cookie 
  168. * @return null|bool 
  169. */ 
  170. public function getSecure() 
  171. return $this->data['Secure']; 
  172.  
  173. /** 
  174. * Set whether or not the cookie is secure 
  175. * @param bool $secure Set to true or false if secure 
  176. */ 
  177. public function setSecure($secure) 
  178. $this->data['Secure'] = $secure; 
  179.  
  180. /** 
  181. * Get whether or not this is a session cookie 
  182. * @return null|bool 
  183. */ 
  184. public function getDiscard() 
  185. return $this->data['Discard']; 
  186.  
  187. /** 
  188. * Set whether or not this is a session cookie 
  189. * @param bool $discard Set to true or false if this is a session cookie 
  190. */ 
  191. public function setDiscard($discard) 
  192. $this->data['Discard'] = $discard; 
  193.  
  194. /** 
  195. * Get whether or not this is an HTTP only cookie 
  196. * @return bool 
  197. */ 
  198. public function getHttpOnly() 
  199. return $this->data['HttpOnly']; 
  200.  
  201. /** 
  202. * Set whether or not this is an HTTP only cookie 
  203. * @param bool $httpOnly Set to true or false if this is HTTP only 
  204. */ 
  205. public function setHttpOnly($httpOnly) 
  206. $this->data['HttpOnly'] = $httpOnly; 
  207.  
  208. /** 
  209. * Check if the cookie matches a path value. 
  210. * A request-path path-matches a given cookie-path if at least one of 
  211. * the following conditions holds: 
  212. * - The cookie-path and the request-path are identical. 
  213. * - The cookie-path is a prefix of the request-path, and the last 
  214. * character of the cookie-path is %x2F ("/"). 
  215. * - The cookie-path is a prefix of the request-path, and the first 
  216. * character of the request-path that is not included in the cookie- 
  217. * path is a %x2F ("/") character. 
  218. * @param string $requestPath Path to check against 
  219. * @return bool 
  220. */ 
  221. public function matchesPath($requestPath) 
  222. $cookiePath = $this->getPath(); 
  223.  
  224. // Match on exact matches or when path is the default empty "/" 
  225. if ($cookiePath === '/' || $cookiePath == $requestPath) { 
  226. return true; 
  227.  
  228. // Ensure that the cookie-path is a prefix of the request path. 
  229. if (0 !== strpos($requestPath, $cookiePath)) { 
  230. return false; 
  231.  
  232. // Match if the last character of the cookie-path is "/" 
  233. if (substr($cookiePath, -1, 1) === '/') { 
  234. return true; 
  235.  
  236. // Match if the first character not included in cookie path is "/" 
  237. return substr($requestPath, strlen($cookiePath), 1) === '/'; 
  238.  
  239. /** 
  240. * Check if the cookie matches a domain value 
  241. * @param string $domain Domain to check against 
  242. * @return bool 
  243. */ 
  244. public function matchesDomain($domain) 
  245. // Remove the leading '.' as per spec in RFC 6265. 
  246. // http://tools.ietf.org/html/rfc6265#section-5.2.3 
  247. $cookieDomain = ltrim($this->getDomain(), '.'); 
  248.  
  249. // Domain not set or exact match. 
  250. if (!$cookieDomain || !strcasecmp($domain, $cookieDomain)) { 
  251. return true; 
  252.  
  253. // Matching the subdomain according to RFC 6265. 
  254. // http://tools.ietf.org/html/rfc6265#section-5.1.3 
  255. if (filter_var($domain, FILTER_VALIDATE_IP)) { 
  256. return false; 
  257.  
  258. return (bool) preg_match('/\.' . preg_quote($cookieDomain) . '$/', $domain); 
  259.  
  260. /** 
  261. * Check if the cookie is expired 
  262. * @return bool 
  263. */ 
  264. public function isExpired() 
  265. return $this->getExpires() && time() > $this->getExpires(); 
  266.  
  267. /** 
  268. * Check if the cookie is valid according to RFC 6265 
  269. * @return bool|string Returns true if valid or an error message if invalid 
  270. */ 
  271. public function validate() 
  272. // Names must not be empty, but can be 0 
  273. $name = $this->getName(); 
  274. if (empty($name) && !is_numeric($name)) { 
  275. return 'The cookie name must not be empty'; 
  276.  
  277. // Check if any of the invalid characters are present in the cookie name 
  278. if (preg_match( 
  279. '/[\x00-\x20\x22\x28-\x29\x2c\x2f\x3a-\x40\x5c\x7b\x7d\x7f]/',  
  280. $name) 
  281. ) { 
  282. return 'Cookie name must not contain invalid characters: ASCII ' 
  283. . 'Control characters (0-31;127), space, tab and the ' 
  284. . 'following characters: ()<>@, ;:\"/?={}'; 
  285.  
  286. // Value must not be empty, but can be 0 
  287. $value = $this->getValue(); 
  288. if (empty($value) && !is_numeric($value)) { 
  289. return 'The cookie value must not be empty'; 
  290.  
  291. // Domains must not be empty, but can be 0 
  292. // A "0" is not a valid internet domain, but may be used as server name 
  293. // in a private network. 
  294. $domain = $this->getDomain(); 
  295. if (empty($domain) && !is_numeric($domain)) { 
  296. return 'The cookie domain must not be empty'; 
  297.  
  298. return true;