10 private static $defaults = [
35 $data = self::$defaults;
37 $pieces = array_filter(array_map(
'trim', explode(
';', $cookie)));
39 if (empty($pieces[0]) || !strpos($pieces[0],
'=')) {
40 return new self($data);
44 foreach ($pieces as $part) {
45 $cookieParts = explode(
'=', $part, 2);
46 $key = trim($cookieParts[0]);
47 $value = isset($cookieParts[1])
48 ? trim($cookieParts[1],
" \n\r\t\0\x0B")
52 if (empty($data[
'Name'])) {
54 $data[
'Value'] = $value;
56 foreach (array_keys(self::$defaults) as $search) {
57 if (!strcasecmp($search, $key)) {
58 $data[$search] = $value;
66 return new self($data);
74 $this->data = array_replace(self::$defaults, $data);
86 $str = $this->data[
'Name'] .
'=' . $this->data[
'Value'] .
'; ';
87 foreach ($this->data as $k => $v) {
88 if ($k !==
'Name' && $k !==
'Value' && $v !==
null && $v !==
false) {
89 if ($k ===
'Expires') {
90 $str .=
'Expires=' . gmdate(
'D, d M Y H:i:s \G\M\T', $v) .
'; ';
92 $str .= ($v ===
true ? $k :
"{$k}={$v}") .
'; ';
97 return rtrim($str,
'; ');
112 return $this->data[
'Name'];
122 $this->data[
'Name'] = $name;
132 return $this->data[
'Value'];
142 $this->data[
'Value'] = $value;
152 return $this->data[
'Domain'];
162 $this->data[
'Domain'] = $domain;
172 return $this->data[
'Path'];
182 $this->data[
'Path'] = $path;
192 return $this->data[
'Max-Age'];
202 $this->data[
'Max-Age'] = $maxAge;
212 return $this->data[
'Expires'];
222 $this->data[
'Expires'] = is_numeric($timestamp)
224 : strtotime($timestamp);
234 return $this->data[
'Secure'];
244 $this->data[
'Secure'] = $secure;
254 return $this->data[
'Discard'];
264 $this->data[
'Discard'] = $discard;
274 return $this->data[
'HttpOnly'];
284 $this->data[
'HttpOnly'] = $httpOnly;
306 $cookiePath = $this->
getPath();
309 if ($cookiePath ===
'/' || $cookiePath ==
$requestPath) {
319 if (substr($cookiePath, -1, 1) ===
'/') {
324 return substr(
$requestPath, strlen($cookiePath), 1) ===
'/';
338 $cookieDomain = ltrim($this->
getDomain(),
'.');
341 if (!$cookieDomain || !strcasecmp($domain, $cookieDomain)) {
347 if (filter_var($domain, FILTER_VALIDATE_IP)) {
351 return (
bool) preg_match(
'/\.' . preg_quote($cookieDomain,
'/') .
'$/', $domain);
373 if (empty($name) && !is_numeric($name)) {
374 return 'The cookie name must not be empty';
379 '/[\x00-\x20\x22\x28-\x29\x2c\x2f\x3a-\x40\x5c\x7b\x7d\x7f]/',
382 return 'Cookie name must not contain invalid characters: ASCII '
383 .
'Control characters (0-31;127), space, tab and the '
384 .
'following characters: ()<>@,;:\"/?={}';
389 if (empty($value) && !is_numeric($value)) {
390 return 'The cookie value must not be empty';
397 if (empty($domain) && !is_numeric($domain)) {
398 return 'The cookie domain must not be empty';