| @@ -1,34 +1,76 @@ | ||
| 1 | 1 | <?php |
| 2 | - | |
| 3 | -declare(strict_types=1); | |
| 4 | - | |
| 2 | +/** | |
| 3 | + * Config.php | |
| 4 | + * | |
| 5 | + * The Config class file. | |
| 6 | + * | |
| 7 | + * PHP versions 5 | |
| 8 | + * | |
| 9 | + * @author Alexander Schneider <[email protected]> | |
| 10 | + * @copyright 2008-2017 Alexander Schneider | |
| 11 | + * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 | |
| 12 | + * @version SVN: $id$ | |
| 13 | + * @link http://wordpress.org/extend/plugins/user-access-manager/ | |
| 14 | + */ | |
| 5 | 15 | namespace UserAccessManager\Config; |
| 6 | 16 | |
| 7 | -use Exception; | |
| 8 | 17 | use UserAccessManager\Wrapper\Wordpress; |
| 9 | 18 | |
| 19 | +/** | |
| 20 | + * Class Config | |
| 21 | + * | |
| 22 | + * @package UserAccessManager\Config | |
| 23 | + */ | |
| 10 | 24 | class Config |
| 11 | 25 | { |
| 12 | - protected string $key; | |
| 13 | - protected array $wpOptions = []; | |
| 14 | 26 | /** |
| 27 | + * @var Wordpress | |
| 28 | + */ | |
| 29 | + protected $wordpress; | |
| 30 | + | |
| 31 | + /** | |
| 32 | + * @var string | |
| 33 | + */ | |
| 34 | + protected $key; | |
| 35 | + | |
| 36 | + /** | |
| 37 | + * @var array | |
| 38 | + */ | |
| 39 | + protected $wpOptions = []; | |
| 40 | + | |
| 41 | + /** | |
| 15 | 42 | * @var ConfigParameter[] |
| 16 | 43 | */ |
| 17 | - protected array $defaultConfigParameters = []; | |
| 44 | + protected $defaultConfigParameters = []; | |
| 45 | + | |
| 18 | 46 | /** |
| 19 | 47 | * @var null|ConfigParameter[] |
| 20 | 48 | */ |
| 21 | - protected ?array $configParameters = null; | |
| 49 | + protected $configParameters = null; | |
| 22 | 50 | |
| 51 | + /** | |
| 52 | + * Config constructor. | |
| 53 | + * | |
| 54 | + * @param Wordpress $wordpress | |
| 55 | + * @param string $key | |
| 56 | + */ | |
| 23 | 57 | public function __construct( |
| 24 | - private Wordpress $wordpress, | |
| 25 | - string $key | |
| 58 | + Wordpress $wordpress, | |
| 59 | + $key | |
| 26 | 60 | ) { |
| 61 | + $this->wordpress = $wordpress; | |
| 27 | 62 | $this->key = $key; |
| 28 | 63 | } |
| 29 | 64 | |
| 30 | - public function getWpOption(string $option): mixed | |
| 65 | + /** | |
| 66 | + * Returns the WordPress options. | |
| 67 | + * | |
| 68 | + * @param string $option | |
| 69 | + * | |
| 70 | + * @return mixed | |
| 71 | + */ | |
| 72 | + public function getWpOption($option) | |
| 31 | 73 | { |
| 32 | 74 | if (!isset($this->wpOptions[$option]) === true) { |
| 33 | 75 | $this->wpOptions[$option] = $this->wordpress->getOption($option); |
| 34 | 76 | } |
| @@ -36,31 +78,37 @@ | ||
| 36 | 78 | return $this->wpOptions[$option]; |
| 37 | 79 | } |
| 38 | 80 | |
| 39 | 81 | /** |
| 82 | + * Returns the default parameters for the current config. | |
| 83 | + * | |
| 40 | 84 | * @return ConfigParameter[] |
| 41 | 85 | */ |
| 42 | - protected function getDefaultConfigParameters(): array | |
| 86 | + protected function getDefaultConfigParameters() | |
| 43 | 87 | { |
| 44 | 88 | return $this->defaultConfigParameters; |
| 45 | 89 | } |
| 46 | 90 | |
| 47 | 91 | /** |
| 92 | + * Sets the default config parameters | |
| 93 | + * | |
| 48 | 94 | * @param ConfigParameter[] $defaultConfigParameters |
| 49 | 95 | */ |
| 50 | - public function setDefaultConfigParameters(array $defaultConfigParameters): void | |
| 96 | + public function setDefaultConfigParameters(array $defaultConfigParameters) | |
| 51 | 97 | { |
| 52 | 98 | $this->defaultConfigParameters = $defaultConfigParameters; |
| 53 | 99 | } |
| 54 | 100 | |
| 55 | 101 | /** |
| 102 | + * Returns the current settings | |
| 103 | + * | |
| 56 | 104 | * @return ConfigParameter[] |
| 57 | 105 | */ |
| 58 | - public function getConfigParameters(): array | |
| 106 | + public function getConfigParameters() | |
| 59 | 107 | { |
| 60 | 108 | if ($this->configParameters === null) { |
| 61 | 109 | $configParameters = $this->getDefaultConfigParameters(); |
| 62 | - $currentOptions = (array) $this->getWpOption($this->key); | |
| 110 | + $currentOptions = (array)$this->getWpOption($this->key); | |
| 63 | 111 | |
| 64 | 112 | foreach ($currentOptions as $key => $option) { |
| 65 | 113 | if (isset($configParameters[$key])) { |
| 66 | 114 | $configParameters[$key]->setValue($option); |
| @@ -73,11 +121,13 @@ | ||
| 73 | 121 | return $this->configParameters; |
| 74 | 122 | } |
| 75 | 123 | |
| 76 | 124 | /** |
| 125 | + * Sets the new config parameters and saves them to the database. | |
| 126 | + * | |
| 77 | 127 | * @param array $rawParameters |
| 78 | 128 | */ |
| 79 | - public function setConfigParameters(array $rawParameters): void | |
| 129 | + public function setConfigParameters(array $rawParameters) | |
| 80 | 130 | { |
| 81 | 131 | $configParameters = $this->getConfigParameters(); |
| 82 | 132 | |
| 83 | 133 | foreach ($rawParameters as $key => $value) { |
| @@ -96,9 +146,12 @@ | ||
| 96 | 146 | |
| 97 | 147 | $this->wordpress->updateOption($this->key, $simpleConfigParameters); |
| 98 | 148 | } |
| 99 | 149 | |
| 100 | - public function flushConfigParameters(): void | |
| 150 | + /** | |
| 151 | + * Flushes the config parameters. | |
| 152 | + */ | |
| 153 | + public function flushConfigParameters() | |
| 101 | 154 | { |
| 102 | 155 | $this->defaultConfigParameters = []; |
| 103 | 156 | $this->configParameters = null; |
| 104 | 157 | } |
| @@ -103,27 +156,39 @@ | ||
| 103 | 156 | $this->configParameters = null; |
| 104 | 157 | } |
| 105 | 158 | |
| 106 | 159 | /** |
| 107 | - * @throws Exception | |
| 160 | + * Returns the requested parameter value | |
| 161 | + * | |
| 162 | + * @param string $parameterName | |
| 163 | + * | |
| 164 | + * @return mixed | |
| 165 | + * | |
| 166 | + * @throws \Exception | |
| 108 | 167 | */ |
| 109 | - public function getParameterValueRaw(string $parameterName): mixed | |
| 168 | + public function getParameterValueRaw($parameterName) | |
| 110 | 169 | { |
| 111 | 170 | $options = $this->getConfigParameters(); |
| 112 | 171 | |
| 113 | 172 | if (isset($options[$parameterName]) === false) { |
| 114 | - throw new Exception("Unknown config parameter '$parameterName'."); | |
| 173 | + throw new \Exception("Unknown config parameter '{$parameterName}'."); | |
| 115 | 174 | } |
| 116 | 175 | |
| 117 | 176 | return $options[$parameterName]->getValue(); |
| 118 | 177 | } |
| 119 | 178 | |
| 120 | - public function getParameterValue(string $parameterName): mixed | |
| 179 | + /** | |
| 180 | + * Returns the requested parameter value but suppresses exceptions. | |
| 181 | + * | |
| 182 | + * @param string $parameterName | |
| 183 | + * | |
| 184 | + * @return mixed | |
| 185 | + */ | |
| 186 | + public function getParameterValue($parameterName) | |
| 121 | 187 | { |
| 122 | 188 | try { |
| 123 | 189 | return $this->getParameterValueRaw($parameterName); |
| 124 | - } catch (Exception) { | |
| 190 | + } catch (\Exception $exception) { | |
| 191 | + return null; | |
| 125 | 192 | } |
| 126 | - | |
| 127 | - return null; | |
| 128 | 193 | } |
| 129 | 194 | } |