Skip to content

Commit 6b0e2a5

Browse files
committed
Create Cookie.php
1 parent 18e0c08 commit 6b0e2a5

File tree

1 file changed

+182
-0
lines changed

1 file changed

+182
-0
lines changed

src/Cookie.php

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
<?php
2+
/**
3+
* Класс свойства cookie.
4+
* @package evas-php\evas-http
5+
* @author Egor Vasyakin <[email protected]>
6+
*/
7+
namespace Evas\Http;
8+
9+
class Cookie
10+
{
11+
/** @var string имя свойства */
12+
public $name;
13+
/** @var string|int значение свойства */
14+
public $value;
15+
16+
/** @var int метка времени истечения свойства */
17+
public $expires;
18+
/** @var int время истечения свойства */
19+
public $maxAge;
20+
21+
/** @var string|null путь действия */
22+
public $path;
23+
/** @var string|null домен действия */
24+
public $host;
25+
26+
/** @var bool поддержка только защищенного соединения */
27+
public $secure;
28+
/** @var bool поддержка только http протокола */
29+
public $httpOnly;
30+
31+
/** @static array маппинг значений свойств по умолчанию */
32+
public static $defaults = [];
33+
34+
/**
35+
* Установка значений свойств по умолчанию
36+
* @param array|null маппинг значений свойств или null для сброса
37+
*/
38+
public static function setDefaults(array $defaults = null)
39+
{
40+
static::$defaults = $defaults ?? [];
41+
}
42+
43+
/**
44+
* Конструктор.
45+
* @param string имя свойства
46+
* @param array|null маппинг других параметров свойства
47+
*/
48+
public function __construct(string $name, array $props = null)
49+
{
50+
$this->name = $name;
51+
foreach (static::$defaults as $name => $value) {
52+
$this->$name = $value;
53+
}
54+
if (!empty($props)) foreach ($props as $name => $value) {
55+
if (!empty($value)) $this->$name = $value;
56+
}
57+
}
58+
59+
/**
60+
* Установка значения совйства cookie.
61+
* @param значение
62+
* @return self
63+
*/
64+
public function withValue($value): Cookie
65+
{
66+
$this->value = $value;
67+
return $this;
68+
}
69+
70+
/**
71+
* Установка json значения свойства cookie.
72+
* @param mixed значение
73+
* @return self
74+
*/
75+
public function withJsonValue($value): Cookie
76+
{
77+
return $this->withValue(json_encode($value));
78+
}
79+
80+
/**
81+
* Установка serialize значения свойства cookie.
82+
* @param mixed значение
83+
* @return self
84+
*/
85+
public function withSerializeValue($value): Cookie
86+
{
87+
return $this->withValue(serialize($value));
88+
}
89+
90+
/**
91+
* Установка пути свойства cookie.
92+
* @param string путь
93+
* @return self
94+
*/
95+
public function withPath(string $path): Cookie
96+
{
97+
$this->path = $path;
98+
return $this;
99+
}
100+
101+
/**
102+
* Установка хоста свойства cookie.
103+
* @param string хост
104+
* @return self
105+
*/
106+
public function withHost(string $host): Cookie
107+
{
108+
$this->host = $host;
109+
return $this;
110+
}
111+
112+
/**
113+
* Установка времени действия свойства cookie.
114+
* @param int метка времени
115+
* @return self
116+
*/
117+
public function withExpires(int $expires): Cookie
118+
{
119+
$this->expires = $expires;
120+
return $this;
121+
}
122+
123+
/**
124+
* Установка времени действия свойства cookie относительно текущего времени.
125+
* @param int сдвиг метки времени
126+
* @return self
127+
*/
128+
public function withExpiresRelatively(int $relatively): Cookie
129+
{
130+
return $this->withExpires(time() + $relatively);
131+
}
132+
133+
/**
134+
* Установка времени действия свойства cookie относительно текущего времени.
135+
* @param int сдвиг метки времени
136+
* @return self
137+
*/
138+
public function withMaxAge(int $age): Cookie
139+
{
140+
$this->maxAge = $age;
141+
return $this;
142+
}
143+
144+
/**
145+
* Установка поддержки защищеного соединения для свойства cookie.
146+
* @return self
147+
*/
148+
public function withSecure(): Cookie
149+
{
150+
$this->secure = true;
151+
return $this;
152+
}
153+
154+
/**
155+
* Установка поддержки только http соединения для свойства cookie.
156+
* @return self
157+
*/
158+
public function withHttpOnly(): Cookie
159+
{
160+
$this->httpOnly = true;
161+
return $this;
162+
}
163+
164+
/**
165+
* Преобразование объекта cookie в строку http-заголовка.
166+
* @return string
167+
*/
168+
public function __toString(): string
169+
{
170+
$str = "$this->name=$this->value";
171+
if (!empty($this->expires)) {
172+
$str .= sprintf('; Expires=%s GMT', date('r', $this->expires));
173+
} else if (!empty($this->maxAge)) {
174+
$str .= "; Max-Age=$this->maxAge";
175+
}
176+
if (!empty($this->path)) $str .= "; Path=$this->path";
177+
if (!empty($this->host)) $str .= "; Domain=$this->host";
178+
if (true === $this->secure) $str .= '; Secure';
179+
if (true === $this->httpOnly) $str .= '; HttpOnly';
180+
return $str;
181+
}
182+
}

0 commit comments

Comments
 (0)