1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Yandex PHP Library |
4
|
|
|
* |
5
|
|
|
* @copyright NIX Solutions Ltd. |
6
|
|
|
* @link https://github.com/nixsolutions/yandex-php-library |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @namespace |
11
|
|
|
*/ |
12
|
|
|
namespace Yandex\Common; |
13
|
|
|
|
14
|
|
|
use Yandex\Common\Exception\InvalidSettingsException; |
15
|
|
|
use Yandex\Common\Exception\RealizationException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Package |
19
|
|
|
* |
20
|
|
|
* @category Yandex |
21
|
|
|
* @package Common |
22
|
|
|
* |
23
|
|
|
* @author Anton Shevchuk |
24
|
|
|
* @created 07.08.13 10:12 |
25
|
|
|
*/ |
26
|
|
|
abstract class AbstractPackage |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* __set |
30
|
|
|
* |
31
|
|
|
* @param string $key |
32
|
|
|
* @param mixed $value |
33
|
|
|
* @throws Exception\RealizationException |
34
|
|
|
* @throws Exception\InvalidSettingsException |
35
|
|
|
* @return self |
36
|
|
|
*/ |
37
|
5 |
View Code Duplication |
public function __set($key, $value) |
|
|
|
|
38
|
|
|
{ |
39
|
5 |
|
$method = 'set' . ucfirst($key); |
40
|
|
|
|
41
|
5 |
|
if (method_exists($this, $method)) { |
42
|
3 |
|
$this->$method($value); |
43
|
2 |
|
} elseif (property_exists($this, $key)) { |
44
|
1 |
|
throw new RealizationException("Property `$key` required realization setter method `$method`"); |
45
|
|
|
} else { |
46
|
1 |
|
throw new InvalidSettingsException("Configuration option `$key`` is undefined"); |
47
|
|
|
} |
48
|
3 |
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* __get |
53
|
|
|
* |
54
|
|
|
* @param string $key |
55
|
|
|
* @throws Exception\RealizationException |
56
|
|
|
* @throws Exception\InvalidSettingsException |
57
|
|
|
* @return self |
58
|
|
|
*/ |
59
|
4 |
View Code Duplication |
public function __get($key) |
|
|
|
|
60
|
|
|
{ |
61
|
4 |
|
$method = 'get' . ucfirst($key); |
62
|
|
|
|
63
|
4 |
|
if (method_exists($this, $method)) { |
64
|
3 |
|
return $this->$method($key); |
65
|
1 |
|
} elseif (property_exists($this, $key)) { |
66
|
1 |
|
throw new RealizationException("Property `$key` required realization getter method `$method`"); |
67
|
|
|
} else { |
68
|
|
|
throw new InvalidSettingsException("Configuration option '$key' is undefined"); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param array $options |
74
|
|
|
* @return void |
75
|
|
|
*/ |
76
|
2 |
|
public function setSettings(array $options) |
77
|
|
|
{ |
78
|
|
|
// apply options |
79
|
2 |
|
foreach ($options as $key => $value) { |
80
|
2 |
|
$key = $this->normalizeKey($key); |
81
|
2 |
|
$this->$key = $value; |
82
|
|
|
} |
83
|
2 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* checkOptions |
87
|
|
|
* |
88
|
|
|
* @throws Exception\InvalidSettingsException |
89
|
|
|
* @return void |
90
|
|
|
*/ |
91
|
8 |
|
public function checkSettings() |
92
|
|
|
{ |
93
|
8 |
|
if (!$this->doCheckSettings()) { |
94
|
2 |
|
throw new InvalidSettingsException("Invalid configuration options of '".get_class($this)."' package"); |
95
|
|
|
} |
96
|
6 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Check package configuration |
100
|
|
|
* |
101
|
|
|
* @return boolean |
102
|
|
|
*/ |
103
|
|
|
abstract protected function doCheckSettings(); |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string $key |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
2 |
|
private function normalizeKey($key) |
110
|
|
|
{ |
111
|
2 |
|
$option = str_replace('_', ' ', strtolower($key)); |
112
|
2 |
|
$option = str_replace(' ', '', ucwords($option)); |
113
|
2 |
|
return $option; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
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.