1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Zortje\MVC\Configuration; |
5
|
|
|
|
6
|
|
|
use Zortje\MVC\Configuration\Exception\ConfigurationNonexistentException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Configuration |
10
|
|
|
* |
11
|
|
|
* @package Zortje\MVC\Configuration |
12
|
|
|
*/ |
13
|
|
|
class Configuration |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var array Internal configurations |
18
|
|
|
*/ |
19
|
|
|
protected $configurations = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Configuration constructor. |
23
|
|
|
* |
24
|
|
|
* @param array $config |
25
|
|
|
*/ |
26
|
2 |
|
public function __construct(array $config = []) |
27
|
|
|
{ |
28
|
2 |
|
foreach ($config as $key => $value) { |
29
|
1 |
|
$this->set($key, $value); |
30
|
|
|
} |
31
|
2 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Set configuration |
35
|
|
|
* |
36
|
|
|
* @param string $key Configuration name |
37
|
|
|
* @param mixed $value Configuration value |
38
|
|
|
*/ |
39
|
1 |
|
public function set(string $key, $value) |
40
|
|
|
{ |
41
|
1 |
|
$this->configurations[$key] = $value; |
42
|
1 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Check if configuration exists |
46
|
|
|
* |
47
|
|
|
* @param string $key Configuration name |
48
|
|
|
* |
49
|
|
|
* @return bool TRUE if configuration exists, otherwise FALSE |
50
|
|
|
*/ |
51
|
1 |
|
public function exists(string $key) |
52
|
|
|
{ |
53
|
1 |
|
if (isset($this->configurations[$key])) { |
54
|
1 |
|
$exists = true; |
55
|
|
|
} else { |
56
|
1 |
|
$exists = false; |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
return $exists; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get configuration |
64
|
|
|
* |
65
|
|
|
* @param string $key Configuration name |
66
|
|
|
* |
67
|
|
|
* @return mixed Configuration value |
68
|
|
|
* |
69
|
|
|
* @throws ConfigurationNonexistentException If configuration has not been set |
70
|
|
|
*/ |
71
|
2 |
|
public function get(string $key) |
72
|
|
|
{ |
73
|
2 |
|
if (isset($this->configurations[$key]) === false) { |
74
|
1 |
|
throw new ConfigurationNonexistentException([$key]); |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
return $this->configurations[$key]; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|