Completed
Push — master ( 30ad8f...8e43a1 )
by Jean-Christophe
01:44
created

CookieUtils::deleteAll()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
namespace Ubiquity\utils;
4
5
/**
6
 * Cookies utilities
7
 * @author jc
8
 * @version 1.0.0.1
9
 */
10
class CookieUtils {
11
	/**
12
	 * Sends a cookie
13
	 * @param string $name the name of the cookie
14
	 * @param string $value The value of the cookie.
15
	 * @param int $duration default : 1 day
16
	 * @param string $path default : / the cookie will be available within the entire domain
17
	 */
18
	public static function set($name,$value,$duration=60*60*24,$path="/"){
19
		\setcookie($name, $value, \time() + $duration,$path);
20
	}
21
22
	/**
23
	 * Returns the Cookie with the name $name
24
	 * @param string $name
25
	 * @param string $default
26
	 * @return null|string
27
	 */
28
	public static function get($name,$default=null){
29
		return isset($_COOKIE[$name])?$_COOKIE[$name]:$default;
30
	}
31
32
	/**
33
	 * Removes the cookie with the name $name
34
	 * @param string $name
35
	 * @param $path
36
	 */
37
	public static function delete($name,$path="/"){
38
		\setcookie($name, "", \time() - 3600,$path);
39
	}
40
41
	/**
42
	 * Deletes all cookies
43
	 */
44
	public function deleteAll($path="/"){
45
		foreach ($_COOKIE as $name=>$value){
46
			self::delete($name,$path);
47
		}
48
	}
49
}