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
|
|
|
public function addParameter($key, $value) |
32
|
|
|
{ |
33
|
|
|
$this->parameters[$key] = $value; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Set all the parameters of this context. |
38
|
|
|
* |
39
|
|
|
* @param array $parameters |
40
|
|
|
*/ |
41
|
|
|
public function setParameters(array $parameters) |
42
|
|
|
{ |
43
|
|
|
$this->parameters = $parameters; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Determine whether a parameter exists. |
48
|
|
|
* |
49
|
|
|
* @param string $key |
50
|
|
|
* |
51
|
|
|
* @return bool |
52
|
|
|
*/ |
53
|
|
|
public function hasParameter($key) |
54
|
|
|
{ |
55
|
|
|
return array_key_exists($key, $this->parameters); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Return all parameters of this context. |
60
|
|
|
* |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
|
|
public function getParameters() |
64
|
|
|
{ |
65
|
|
|
return $this->parameters; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function getIterator() |
69
|
|
|
{ |
70
|
|
|
return new \ArrayIterator($this->parameters); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|