| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBooking\Framework\Http; |
| 4 |
|
| 5 |
class Cookie |
| 6 |
{ |
| 7 |
public static function set( |
| 8 |
$name, |
| 9 |
$value, |
| 10 |
$minutes = 0, |
| 11 |
$path = '', |
| 12 |
$domain = '', |
| 13 |
$secure = false, |
| 14 |
$httponly = false |
| 15 |
) |
| 16 |
{ |
| 17 |
$time = ($minutes == 0) ? 0 : static::expiresAt($minutes * 60); |
| 18 |
|
| 19 |
setcookie($name, $value, $time, $path, $domain, $secure, $httponly); |
| 20 |
} |
| 21 |
|
| 22 |
public static function setForever( |
| 23 |
$name, |
| 24 |
$value, |
| 25 |
$path = '', |
| 26 |
$domain = '', |
| 27 |
$secure = false, |
| 28 |
$httponly = false |
| 29 |
) |
| 30 |
{ |
| 31 |
$fiveYears = static::expiresAt((365 * 24 * 60 * 60) * 5); |
| 32 |
|
| 33 |
setcookie($name, $value, $fiveYears, $path, $domain, $secure, $httponly); |
| 34 |
} |
| 35 |
|
| 36 |
public static function get($name, $default = null) |
| 37 |
{ |
| 38 |
if (array_key_exists($name, $_COOKIE)) { |
| 39 |
return $_COOKIE[$name]; |
| 40 |
} |
| 41 |
|
| 42 |
return $default; |
| 43 |
} |
| 44 |
|
| 45 |
public static function delete($name, $path = '', $domain = '') { |
| 46 |
setcookie($name, '', time() - 3600, $path, $domain); |
| 47 |
|
| 48 |
if (array_key_exists($name, $_COOKIE)) { |
| 49 |
unset($_COOKIE[$name]); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
protected static function expiresAt($value = 0) |
| 54 |
{ |
| 55 |
if (!$value instanceof \DateTimeInterface) { |
| 56 |
|
| 57 |
$value = is_numeric($value) ? (int) $value : 0; |
| 58 |
|
| 59 |
$value = new \DateTime('+' . $value . ' seconds'); |
| 60 |
} |
| 61 |
|
| 62 |
return $value->getTimestamp(); |
| 63 |
} |
| 64 |
} |
| 65 |
|