1 | <?php |
||
7 | class SetCookie implements Abstracted |
||
8 | { |
||
9 | |||
10 | protected $headerValue = null; |
||
11 | protected $headerName = 'Set-Cookie'; |
||
12 | |||
13 | private $cookieName = null; |
||
14 | private $cookieValue = null; |
||
15 | private $options = []; |
||
16 | |||
17 | private $defaults = [ |
||
18 | 'expires' => null, |
||
19 | 'path' => '/', |
||
20 | 'domain' => '', |
||
21 | 'secure' => false, |
||
22 | 'httpOnly' => true, |
||
23 | ]; |
||
24 | |||
25 | |||
26 | 15 | public function __construct($name, $value, $options = []) |
|
32 | |||
33 | |||
34 | 17 | public function prepare() |
|
35 | { |
||
36 | 17 | if ($this->hasInvalidOptions($this->options)) { |
|
37 | 1 | $message = 'Valid array keys for cookie options are: \'expires\', \'path\', \'domain\', \'secure\' and \'httpOnly\''; |
|
38 | 1 | trigger_error($message, E_NOTICE); |
|
39 | } |
||
40 | 16 | $this->options = $this->cleanOptions($this->options); |
|
41 | 16 | } |
|
42 | |||
43 | |||
44 | 11 | private function hasInvalidOptions($options) |
|
50 | |||
51 | |||
52 | 10 | private function cleanOptions($options) |
|
53 | { |
||
54 | 10 | $options = $options + $this->defaults; |
|
55 | |||
56 | 10 | if ($options['expires'] !== null) { |
|
57 | 3 | $options['expires'] = $this->convertTime($options['expires']); |
|
58 | } |
||
59 | |||
60 | 10 | if ($options['path'] === null) { |
|
61 | 1 | $options['path'] = '/'; |
|
62 | } |
||
63 | |||
64 | 10 | $options['domain'] = strtolower($options['domain']); |
|
65 | 10 | $options['secure'] = (bool) $options['secure']; |
|
66 | 10 | $options['httpOnly'] = (bool) $options['httpOnly']; |
|
67 | |||
68 | 10 | return $options; |
|
69 | } |
||
70 | |||
71 | |||
72 | 6 | private function convertTime($time) { |
|
85 | |||
86 | |||
87 | 6 | private function isDateTime($time) |
|
91 | |||
92 | |||
93 | |||
94 | |||
95 | 1 | public function getName() |
|
99 | |||
100 | |||
101 | 13 | public function getValue() |
|
110 | |||
111 | |||
112 | 10 | private function collectFormatedOptions() |
|
120 | |||
121 | |||
122 | 13 | private function collectExpireTime($options) |
|
123 | { |
||
124 | 13 | $string = ''; |
|
125 | |||
126 | 13 | if ($options['expires'] !== null) { |
|
127 | 6 | $string = $options['expires']->format(\DateTime::RFC1123); |
|
128 | 6 | $string = str_replace('+0000', 'GMT', $string); |
|
129 | 6 | $string = '; Expires=' . $string; |
|
130 | } |
||
131 | |||
132 | 13 | return $string; |
|
133 | } |
||
134 | |||
135 | |||
136 | 10 | private function collectDomainPathValue($options) |
|
137 | { |
||
138 | 10 | $output = ''; |
|
139 | |||
140 | 10 | if ($options['domain'] !== '') { |
|
141 | 2 | $output .= '; Domain=' . $options['domain']; |
|
142 | } |
||
143 | |||
144 | 10 | return $output . '; Path=' . $options['path']; |
|
145 | } |
||
146 | |||
147 | |||
148 | 10 | private function collectBooleanOptions($options) |
|
149 | { |
||
150 | 10 | $result = ''; |
|
151 | |||
152 | 10 | if ($options['secure']) { |
|
153 | 1 | $result .= '; Secure'; |
|
154 | } |
||
155 | |||
156 | 10 | if ($options['httpOnly']) { |
|
157 | 9 | $result .= '; HttpOnly'; |
|
158 | } |
||
159 | |||
160 | 10 | return $result; |
|
161 | } |
||
162 | |||
163 | |||
164 | 1 | public function isFinal() |
|
168 | } |
||
169 |