1 | <?php |
||
20 | class GenericCookie implements CookieInterface |
||
21 | { |
||
22 | use CookieFieldsTrait; |
||
23 | |||
24 | /** |
||
25 | * @var string Raw cookie value. |
||
26 | */ |
||
27 | protected $rawValue; |
||
28 | |||
29 | /** |
||
30 | * @var mixed Parsed value of the cookie. |
||
31 | */ |
||
32 | protected $value; |
||
33 | |||
34 | /** |
||
35 | * @var bool |
||
36 | */ |
||
37 | protected $isLoaded = false; |
||
38 | |||
39 | /** |
||
40 | * @var bool |
||
41 | */ |
||
42 | protected $shouldClear = false; |
||
43 | |||
44 | /** |
||
45 | * @var string Hash of the raw value generated once on load. |
||
46 | */ |
||
47 | protected $valueHash; |
||
48 | |||
49 | /** |
||
50 | * Constructor. |
||
51 | * |
||
52 | * @param string $name |
||
53 | */ |
||
54 | public function __construct($name) |
||
58 | |||
59 | /** |
||
60 | * {@inheritdoc} |
||
61 | */ |
||
62 | public function setValue($value) |
||
63 | { |
||
64 | $this->ensureLoaded(); |
||
65 | $this->value = $value; |
||
66 | |||
67 | return $this; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * {@inheritdoc} |
||
72 | */ |
||
73 | public function getValue() |
||
74 | { |
||
75 | $this->ensureLoaded(); |
||
76 | |||
77 | return $this->value; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | public function load($rawValue) |
||
84 | { |
||
85 | if ($this->hasRawValue()) { |
||
86 | return; |
||
87 | } |
||
88 | |||
89 | $this->isLoaded = true; |
||
90 | $this->rawValue = $rawValue; |
||
91 | |||
92 | $defaults = [ |
||
93 | 'domain' => null, |
||
94 | 'path' => '/', |
||
95 | 'http_only' => true, |
||
96 | 'secure' => false, |
||
97 | 'expires_time' => 0, |
||
98 | ]; |
||
99 | $defaults = array_merge($defaults, $this->defaults); |
||
100 | $this->setDomain($defaults['domain']); |
||
101 | |||
102 | $expiresTime = $defaults['expires_time']; |
||
103 | if (isset($this->defaults['expires_interval'])) { |
||
104 | $expiresTime = (new \DateTime()) |
||
105 | ->add(new \DateInterval($this->defaults['expires_interval'])) |
||
106 | ->getTimestamp(); |
||
107 | } |
||
108 | $this->setExpiresTime($expiresTime); |
||
109 | |||
110 | $this->setIsHttpOnly($defaults['http_only']); |
||
111 | $this->setIsSecure($defaults['secure']); |
||
112 | $this->setPath($defaults['path']); |
||
113 | |||
114 | $this->setValue($this->decode($this->rawValue)); |
||
115 | $this->loadHash(); |
||
116 | |||
117 | $this->afterLoad(); |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Override this for the ability to modify value after loading. |
||
122 | * |
||
123 | * E.g. set expiration time dynamically. |
||
124 | */ |
||
125 | protected function afterLoad() |
||
128 | |||
129 | /** |
||
130 | * {@inheritdoc} |
||
131 | */ |
||
132 | public function isLoaded() |
||
136 | |||
137 | /** |
||
138 | * Check if model already has loaded cookie. |
||
139 | * |
||
140 | * @return bool |
||
141 | */ |
||
142 | public function hasRawValue() |
||
146 | |||
147 | /** |
||
148 | * Return value of $rawValue. |
||
149 | * |
||
150 | * @return string |
||
151 | */ |
||
152 | public function getRawValue() |
||
153 | { |
||
154 | $this->ensureLoaded(); |
||
155 | |||
156 | return $this->rawValue; |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * {@inheritdoc} |
||
161 | */ |
||
162 | public function toCookie() |
||
163 | { |
||
164 | $this->rawValue = $this->encode($this->getValue()); |
||
165 | |||
166 | return new Cookie( |
||
167 | $this->getName(), |
||
168 | $this->getRawValue(), |
||
169 | $this->getExpiresTime(), |
||
170 | $this->getPath(), |
||
171 | $this->getDomain(), |
||
172 | $this->isSecure(), |
||
173 | $this->isHttpOnly() |
||
174 | ); |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Build $rawValue from data in $value. |
||
179 | * |
||
180 | * @param mixed $value |
||
181 | * |
||
182 | * @return string |
||
183 | */ |
||
184 | protected function encode($value) |
||
188 | |||
189 | /** |
||
190 | * Decode $value from $rawValue. |
||
191 | * |
||
192 | * @param string $rawValue |
||
193 | * |
||
194 | * @return mixed |
||
195 | */ |
||
196 | protected function decode($rawValue) |
||
200 | |||
201 | /** |
||
202 | * {@inheritdoc} |
||
203 | */ |
||
204 | public function isDirty() |
||
208 | |||
209 | /** |
||
210 | * Calculate hash of cookie data. |
||
211 | * |
||
212 | * @return string |
||
213 | */ |
||
214 | protected function calculateHash() |
||
218 | |||
219 | /** |
||
220 | * {@inheritdoc} |
||
221 | */ |
||
222 | public function getClear() |
||
223 | { |
||
224 | $this->ensureLoaded(); |
||
225 | |||
226 | return $this->shouldClear; |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * {@inheritdoc} |
||
231 | */ |
||
232 | public function setClear($clear) |
||
233 | { |
||
234 | $this->ensureLoaded(); |
||
235 | $this->shouldClear = $clear; |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * Store hash of the cookie value for dirty detection. |
||
240 | */ |
||
241 | protected function loadHash() |
||
245 | |||
246 | /** |
||
247 | * Throw exception, if cookie has not been loaded yet. |
||
248 | * |
||
249 | * @throws CookieNotLoadedException |
||
250 | */ |
||
251 | protected function ensureLoaded() |
||
252 | { |
||
257 | } |
||
258 |