|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the FOSHttpCache package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace FOS\HttpCache\UserContext; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* A UserContext is a set of parameters which allow to determinate different views for an url. |
|
16
|
|
|
* |
|
17
|
|
|
* For example a menu can be different if a user is authenticated or not, |
|
18
|
|
|
* in this case the UserContext should have a authenticated parameter set |
|
19
|
|
|
* to true if user is logged in or to false otherwise. |
|
20
|
|
|
*/ |
|
21
|
|
|
class UserContext implements \IteratorAggregate |
|
22
|
|
|
{ |
|
23
|
|
|
private $parameters = []; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Set a parameter for this context. |
|
27
|
|
|
* |
|
28
|
|
|
* @param string $key Parameter identifier |
|
29
|
|
|
* @param mixed $value Parameter value (it should be serializable) |
|
30
|
|
|
*/ |
|
31
|
3 |
|
public function addParameter($key, $value) |
|
32
|
|
|
{ |
|
33
|
3 |
|
$this->parameters[$key] = $value; |
|
34
|
3 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Set all the parameters of this context. |
|
38
|
|
|
*/ |
|
39
|
1 |
|
public function setParameters(array $parameters) |
|
40
|
|
|
{ |
|
41
|
1 |
|
$this->parameters = $parameters; |
|
42
|
1 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Determine whether a parameter exists. |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $key |
|
48
|
|
|
* |
|
49
|
|
|
* @return bool |
|
50
|
|
|
*/ |
|
51
|
2 |
|
public function hasParameter($key) |
|
52
|
|
|
{ |
|
53
|
2 |
|
return array_key_exists($key, $this->parameters); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Return all parameters of this context. |
|
58
|
|
|
* |
|
59
|
|
|
* @return array |
|
60
|
|
|
*/ |
|
61
|
2 |
|
public function getParameters() |
|
62
|
|
|
{ |
|
63
|
2 |
|
return $this->parameters; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
1 |
|
public function getIterator(): \Traversable |
|
67
|
|
|
{ |
|
68
|
1 |
|
return new \ArrayIterator($this->parameters); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|