1: <?php
2:
3: namespace PHPixie\HTTP\Context\Cookies;
4:
5: /**
6: * Cookie update data
7: */
8: class Update {
9:
10: /**
11: * @var string
12: */
13: protected $name;
14:
15: /**
16: * @var mixed
17: */
18: protected $value;
19:
20: /**
21: * @var int|null
22: */
23: protected $expires;
24:
25: /**
26: * @var string
27: */
28: protected $path;
29:
30: /**
31: * @var string|null
32: */
33: protected $domain;
34:
35: /**
36: * @var bool
37: */
38: protected $secure;
39:
40: /**
41: * @var bool
42: */
43: protected $httpOnly;
44:
45: /**
46: * Constructor
47: * @param string $name
48: * @param mixed $value
49: * @param int|null $expires
50: * @param string $path
51: * @param string|null $domain
52: * @param bool $secure
53: * @param bool $httpOnly
54: */
55: public function __construct(
56: $name,
57: $value,
58: $expires = null,
59: $path = '/',
60: $domain = null,
61: $secure = false,
62: $httpOnly = false
63: )
64: {
65: $this->name = $name;
66: $this->value = $value;
67: $this->expires = $expires;
68: $this->path = $path;
69: $this->domain = $domain;
70: $this->secure = $secure;
71: $this->httpOnly = $httpOnly;
72: }
73:
74: /**
75: * Name
76: * @return string
77: */
78: public function name()
79: {
80: return $this->name;
81: }
82:
83: /**
84: * Value
85: * @return mixed
86: */
87: public function value()
88: {
89: return $this->value;
90: }
91:
92: /**
93: * Expires at (timestamp)
94: * @return int|null
95: */
96: public function expires()
97: {
98: return $this->expires;
99: }
100:
101: /**
102: * Path
103: * @return string
104: */
105: public function path()
106: {
107: return $this->path;
108: }
109:
110: /**
111: * Domain
112: * @return null|string
113: */
114: public function domain()
115: {
116: return $this->domain;
117: }
118:
119: /**
120: * Whether the cookie is secure
121: * @return bool
122: */
123: public function secure()
124: {
125: return $this->secure;
126: }
127:
128: /**
129: * Whether the cookie is HTTP only
130: * @return bool
131: */
132: public function httpOnly()
133: {
134: return $this->httpOnly;
135: }
136:
137: /**
138: * Get header representation
139: * @return string
140: */
141: public function asHeader()
142: {
143: $header = urlencode($this->name).'='.urlencode((string) $this->value);
144:
145: if($this->domain !== null) {
146: $header.= '; domain='.$this->domain;
147: }
148:
149: if($this->path !== null) {
150: $header.= '; path='.$this->path;
151: }
152:
153: if($this->expires !== null) {
154: $header.= '; expires=' . gmdate('D, d-M-Y H:i:s e', $this->expires);
155: }
156:
157: if($this->secure) {
158: $header.= '; secure';
159: }
160:
161: if($this->httpOnly) {
162: $header.= '; HttpOnly';
163: }
164:
165: return $header;
166: }
167: }