1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Compolomus\IniObject; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
|
9
|
|
|
class Section |
10
|
|
|
{ |
11
|
|
|
private $name; |
12
|
|
|
|
13
|
|
|
private $params; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Section constructor. |
17
|
|
|
* |
18
|
|
|
* @param string $sectionName |
19
|
|
|
* @param array $params |
20
|
|
|
*/ |
21
|
10 |
|
public function __construct(string $sectionName, array $params) |
22
|
|
|
{ |
23
|
10 |
|
$this->name = $sectionName; |
24
|
|
|
|
25
|
10 |
|
$data = []; |
26
|
10 |
|
foreach ($params as $name => $value) { |
27
|
10 |
|
$data[$name] = new Param($name, $value); |
28
|
|
|
} |
29
|
|
|
|
30
|
10 |
|
$this->params = $data; |
31
|
10 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $name |
35
|
|
|
* @param $value |
36
|
|
|
*/ |
37
|
|
View Code Duplication |
public function add(string $name, $value): void |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
if (isset($this->params[$name])) { |
40
|
|
|
throw new InvalidArgumentException('Overwrite parameter denied'); |
41
|
|
|
} |
42
|
|
|
$this->params[$name] = new Param($name, $value); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $name |
47
|
|
|
*/ |
48
|
2 |
View Code Duplication |
public function remove(string $name): void |
|
|
|
|
49
|
|
|
{ |
50
|
2 |
|
if (! isset($this->params[$name])) { |
51
|
2 |
|
throw new InvalidArgumentException('Parameter not found for remove'); |
52
|
|
|
} |
53
|
1 |
|
unset($this->params[$name]); |
54
|
1 |
|
} |
55
|
|
|
|
56
|
1 |
View Code Duplication |
public function update(string $name, $value): void |
|
|
|
|
57
|
|
|
{ |
58
|
1 |
|
if (! isset($this->params[$name])) { |
59
|
1 |
|
throw new InvalidArgumentException('Parameter not found for update'); |
60
|
|
|
} |
61
|
1 |
|
$this->params[$name] = new Param($name, $value); |
62
|
1 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $name |
66
|
|
|
* @return mixed|null |
67
|
|
|
*/ |
68
|
3 |
|
public function getParam(string $name) |
69
|
|
|
{ |
70
|
3 |
|
return $this->params[$name]->getValue(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
1 |
|
public function getName(): string |
77
|
|
|
{ |
78
|
1 |
|
return $this->name; |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
public function __toString(): string |
82
|
|
|
{ |
83
|
1 |
|
$return = '[' . $this->getName() . ']' . PHP_EOL . PHP_EOL; |
84
|
1 |
|
foreach ($this->params as $param) { |
85
|
1 |
|
$return .= $param; |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
return $return . PHP_EOL; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.