Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
UCookie.php
Go to the documentation of this file.
1<?php
2
3namespace Ubiquity\utils\http;
4
6
16class UCookie {
17
22 private static $transformer;
23 public static $useTransformer = false;
24
36 public static function set($name, $value, $duration = 60 * 60 * 24, $path = '/', $secure = false, $httpOnly = false): bool {
37 if ($value!=null && self::$useTransformer && isset ( self::$transformer )) {
38 $value = self::$transformer->transform ( $value );
39 }
40 return \setcookie ( $name, $value, $duration ? (\time () + $duration) : 0, $path, $secure, $httpOnly );
41 }
42
50 public static function get($name, $default = null) {
51 $v = $_COOKIE [$name] ?? $default;
52 if ($v!=null && self::$useTransformer && isset ( self::$transformer )) {
53 return self::$transformer->reverse ( rawurldecode($v) );
54 }
55 return $v;
56 }
57
64 public static function delete($name, $path = '/'): bool {
65 if (isset ( $_COOKIE [$name] )) {
66 unset ( $_COOKIE [$name] );
67 }
68 return \setcookie ( $name, '', \time () - 3600, $path );
69 }
70
74 public static function deleteAll($path = '/'): void {
75 foreach ( $_COOKIE as $name => $_ ) {
76 self::delete ( $name, $path );
77 }
78 }
79
87 public static function exists($name): bool {
88 return isset ( $_COOKIE [$name] );
89 }
90
103 public static function setRaw($name, $value, $duration = 60 * 60 * 24, $path = '/', $secure = false, $httpOnly = false): bool {
104 if ($value!=null && self::$useTransformer && isset ( self::$transformer )) {
105 $value = self::$transformer->transform ( $value );
106 }
107 return \setrawcookie ( $name, $value, \time () + $duration, $path, $secure, $httpOnly );
108 }
109
117 public static function filter(string $key,int $filter=\FILTER_DEFAULT,$default=null){
118 return \filter_input(INPUT_COOKIE,$key,$filter)??$default;
119 }
120
121 public static function setTransformer(TransformerInterface $transformer) {
122 self::$transformer = $transformer;
123 self::$useTransformer = true;
124 }
125
126 public static function getTransformerClass(): ?string {
127 if (isset ( self::$transformer )) {
128 return \get_class ( self::$transformer );
129 }
130 return null;
131 }
132}