1: <?php
2:
3: namespace PHPixie\HTTP\Context;
4: use PHPixie\HTTP\Builder;
5:
6: 7: 8:
9: class Cookies
10: {
11: 12: 13:
14: protected $builder;
15:
16: 17: 18:
19: protected $cookies;
20:
21: 22: 23:
24: protected $updates = array();
25:
26: 27: 28: 29: 30:
31: public function __construct($builder, $cookies = array())
32: {
33: $this->builder = $builder;
34: $this->cookies = $cookies;
35: }
36:
37: 38: 39: 40: 41: 42:
43: public function get($name, $default = null)
44: {
45: if($this->exists($name)) {
46: return $this->cookies[$name];
47: }
48:
49: return $default;
50: }
51:
52: 53: 54: 55: 56: 57:
58: public function getRequired($name)
59: {
60: if($this->exists($name)) {
61: return $this->cookies[$name];
62: }
63:
64: throw new \PHPixie\HTTP\Exception("Cookie '$name' is not set");
65: }
66:
67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79:
80: public function set(
81: $name,
82: $value,
83: $lifetime = null,
84: $path = '/',
85: $domain = null,
86: $secure = false,
87: $httpOnly = false
88: )
89: {
90: if($lifetime !== null) {
91: $expires = time() + $lifetime;
92:
93: }else{
94: $expires = null;
95: }
96:
97: if($lifetime < 0) {
98: unset($this->cookies[$name]);
99:
100: }else {
101: $this->cookies[$name] = $value;
102: }
103:
104: $this->updates[$name] = $this->builder->cookiesUpdate(
105: $name,
106: $value,
107: $expires,
108: $path,
109: $domain,
110: $secure,
111: $httpOnly
112: );
113: }
114:
115: 116: 117: 118: 119:
120: public function remove($name)
121: {
122: $this->set($name, null, -3600*24*30);
123: }
124:
125: 126: 127: 128: 129:
130: public function exists($name)
131: {
132: return array_key_exists($name, $this->cookies);
133: }
134:
135: 136: 137: 138:
139: public function updates()
140: {
141: return array_values($this->updates);
142: }
143:
144: 145: 146: 147:
148: public function asArray()
149: {
150: return $this->cookies;
151: }
152: }