Total Complexity | 7 |
Total Lines | 58 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
7 | class ContextManager |
||
8 | { |
||
9 | /** |
||
10 | * The collection of Contexts |
||
11 | * @var array |
||
12 | */ |
||
13 | private $contexts = array(); |
||
14 | |||
15 | /** |
||
16 | * Add a context to the collection |
||
17 | * @param String|array $key |
||
18 | * @param $value |
||
19 | * @return void |
||
20 | */ |
||
21 | public function put($key, $value = null) |
||
22 | { |
||
23 | if (is_array($key)) { |
||
24 | foreach ($key as $arrayKey => $arrayValue) { |
||
25 | $this->bind((string)$arrayKey, $arrayValue); |
||
26 | } |
||
27 | } else { |
||
28 | $this->bind((string)$key, $value); |
||
29 | } |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * Get a value from the contexts |
||
34 | * @param $key |
||
35 | * @throws UnregisteredContextException |
||
36 | * @return mixed |
||
37 | */ |
||
38 | public function get($key) |
||
39 | { |
||
40 | if (array_key_exists($key, $this->contexts)) { |
||
41 | return $this->contexts[$key]; |
||
42 | } |
||
43 | |||
44 | throw new Exception\UnregisteredContextException("No context registered for " . $key); |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * Get all contexts |
||
49 | * @return array |
||
50 | */ |
||
51 | public function all() |
||
52 | { |
||
53 | return $this->contexts; |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * Store a key value pair into the collection |
||
58 | * @param String $key |
||
59 | * @param mixed $value |
||
60 | * @return void |
||
61 | */ |
||
62 | private function bind(String $key, $value) |
||
65 | } |
||
66 | } |
||
67 |