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