Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
httpcookies.php
1<?php
2
10namespace Bitrix\Main\Web;
11
13
15{
17 protected $values = [];
18
22 public function __construct(array $values = null)
23 {
24 if ($values !== null)
25 {
26 foreach ($values as $key => $value)
27 {
28 if (!($value instanceof Http\Cookie))
29 {
30 $value = new Http\Cookie($key, $value);
31 }
32 $this[$key] = $value;
33 }
34 }
35 }
36
41 public function implode(): string
42 {
43 $str = '';
44 foreach ($this->values as $cookie)
45 {
46 $str .= ($str == '' ? '' : '; ') . rawurlencode($cookie->getName()) . '=' . rawurlencode($cookie->getValue());
47 }
48 return $str;
49 }
50
51 public function addFromString(string $str): void
52 {
53 if (($pos = strpos($str, ';')) !== false && $pos > 0)
54 {
55 $cookie = trim(substr($str, 0, $pos));
56 }
57 else
58 {
59 $cookie = trim($str);
60 }
61 $arCookie = explode('=', $cookie, 2);
62
63 $name = rawurldecode($arCookie[0]);
64 $value = rawurldecode($arCookie[1]);
65
66 // TODO: a cookie has more attributes
67 $this[$name] = new Http\Cookie($name, $value);
68 }
69
70 public function toArray()
71 {
72 $cookies = [];
73 foreach ($this->values as $cookie)
74 {
75 $cookies[$cookie->getName()] = $cookie->getValue();
76 }
77 return $cookies;
78 }
79
80 #[\ReturnTypeWillChange]
81 public function offsetSet($offset, $value)
82 {
83 if (!($value instanceof Http\Cookie))
84 {
85 throw new ArgumentTypeException('value', Http\Cookie::class);
86 }
87 parent::offsetSet($offset, $value);
88 }
89}
__construct(array $values=null)
offsetSet($offset, $value)