1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Surfnet\StepupBundle\Value; |
4
|
|
|
|
5
|
|
|
use Surfnet\StepupBundle\Exception\InvalidArgumentException; |
6
|
|
|
use Symfony\Component\HttpFoundation\Cookie; |
7
|
|
|
use Symfony\Component\HttpFoundation\Request; |
8
|
|
|
|
9
|
|
|
final class CookieSettings |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
private $name; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $domain; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $expire; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $path; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var bool |
33
|
|
|
*/ |
34
|
|
|
private $secure; |
35
|
|
|
/** |
36
|
|
|
* @var bool |
37
|
|
|
*/ |
38
|
|
|
private $httpOnly; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $name |
42
|
|
|
* @param string $domain |
43
|
|
|
* @param string $expire |
44
|
|
|
* @param string $path |
45
|
|
|
* @param bool $secure |
46
|
|
|
* @param bool $httpOnly |
47
|
|
|
*/ |
48
|
|
|
public function __construct($name, $domain, $expire, $path, $secure, $httpOnly) |
49
|
|
|
{ |
50
|
|
|
if (!is_string($name)) { |
51
|
|
|
throw InvalidArgumentException::invalidType('string', 'name', $name); |
52
|
|
|
} |
53
|
|
|
if (empty($name)) { |
54
|
|
|
throw new InvalidArgumentException('Empty name provided to ' . __CLASS__); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$this->name = $name; |
58
|
|
|
|
59
|
|
|
if (!is_string($domain)) { |
60
|
|
|
throw InvalidArgumentException::invalidType('string', 'domain', $domain); |
61
|
|
|
} |
62
|
|
|
if (empty($domain)) { |
63
|
|
|
throw new InvalidArgumentException(sprintf('Empty domain provided to ' . __CLASS__)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$this->domain = $domain; |
67
|
|
|
|
68
|
|
|
if (!is_integer($expire)) { |
69
|
|
|
throw InvalidArgumentException::invalidType('integer', 'expire', $expire); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$this->expire = $expire; |
|
|
|
|
73
|
|
|
|
74
|
|
|
if (!is_string($path)) { |
75
|
|
|
throw InvalidArgumentException::invalidType('string', 'path', $path); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$this->path = $path; |
79
|
|
|
|
80
|
|
|
if (!is_bool($secure)) { |
81
|
|
|
throw InvalidArgumentException::invalidType('bool', 'secure', $secure); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$this->secure = $secure; |
85
|
|
|
|
86
|
|
|
if (!is_bool($httpOnly)) { |
87
|
|
|
throw InvalidArgumentException::invalidType('bool', 'httpOnly', $httpOnly); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$this->httpOnly = $httpOnly; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* The the value for this cookie from the request, null if doesn't exist. |
95
|
|
|
* |
96
|
|
|
* @param Request $request |
97
|
|
|
* @return string|null |
98
|
|
|
*/ |
99
|
|
|
public function value(Request $request) |
100
|
|
|
{ |
101
|
|
|
return $request->cookies->get($this->name); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function toCookie($value) |
105
|
|
|
{ |
106
|
|
|
return new Cookie( |
107
|
|
|
$this->name, |
108
|
|
|
$value, |
109
|
|
|
$this->expire, |
110
|
|
|
$this->path, |
111
|
|
|
$this->domain, |
112
|
|
|
$this->secure, |
113
|
|
|
$this->httpOnly |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.