PluginProbe
User Access Manager / 2.3.13
User Access Manager v2.3.13
2.3.20 2.3.19 2.3.18 2.3.17 2.3.16 2.3.15 2.3.14 2.3.13 trunk 0.6 0.6.1 0.6.2 0.7 0.7 Beta 0.7.0.1 0.8 0.8.0.1 0.8.0.2 0.9 0.9.1 0.9.1.1 0.9.1.2 0.9.1.3 0.9.1.4 1.0 All 136 releases
user-access-manager / src / Config / Config.php

Config.php in User Access Manager 2.3.13, at src/Config/Config.php

130 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace UserAccessManager\Config;
6
7 use Exception;
8 use UserAccessManager\Wrapper\Wordpress;
9
10 class Config
11 {
12 protected string $key;
13 protected array $wpOptions = [];
14 /**
15 * @var ConfigParameter[]
16 */
17 protected array $defaultConfigParameters = [];
18 /**
19 * @var null|ConfigParameter[]
20 */
21 protected ?array $configParameters = null;
22
23 public function __construct(
24 private Wordpress $wordpress,
25 string $key
26 ) {
27 $this->key = $key;
28 }
29
30 public function getWpOption(string $option): mixed
31 {
32 if (!isset($this->wpOptions[$option]) === true) {
33 $this->wpOptions[$option] = $this->wordpress->getOption($option);
34 }
35
36 return $this->wpOptions[$option];
37 }
38
39 /**
40 * @return ConfigParameter[]
41 */
42 protected function getDefaultConfigParameters(): array
43 {
44 return $this->defaultConfigParameters;
45 }
46
47 /**
48 * @param ConfigParameter[] $defaultConfigParameters
49 */
50 public function setDefaultConfigParameters(array $defaultConfigParameters): void
51 {
52 $this->defaultConfigParameters = $defaultConfigParameters;
53 }
54
55 /**
56 * @return ConfigParameter[]
57 */
58 public function getConfigParameters(): array
59 {
60 if ($this->configParameters === null) {
61 $configParameters = $this->getDefaultConfigParameters();
62 $currentOptions = (array) $this->getWpOption($this->key);
63
64 foreach ($currentOptions as $key => $option) {
65 if (isset($configParameters[$key])) {
66 $configParameters[$key]->setValue($option);
67 }
68 }
69
70 $this->configParameters = $configParameters;
71 }
72
73 return $this->configParameters;
74 }
75
76 /**
77 * @param array $rawParameters
78 */
79 public function setConfigParameters(array $rawParameters): void
80 {
81 $configParameters = $this->getConfigParameters();
82
83 foreach ($rawParameters as $key => $value) {
84 if (isset($configParameters[$key]) === true) {
85 $configParameters[$key]->setValue($value);
86 }
87 }
88
89 $this->configParameters = $configParameters;
90
91 $simpleConfigParameters = [];
92
93 foreach ($configParameters as $parameter) {
94 $simpleConfigParameters[$parameter->getId()] = $parameter->getValue();
95 }
96
97 $this->wordpress->updateOption($this->key, $simpleConfigParameters);
98 }
99
100 public function flushConfigParameters(): void
101 {
102 $this->defaultConfigParameters = [];
103 $this->configParameters = null;
104 }
105
106 /**
107 * @throws Exception
108 */
109 public function getParameterValueRaw(string $parameterName): mixed
110 {
111 $options = $this->getConfigParameters();
112
113 if (isset($options[$parameterName]) === false) {
114 throw new Exception("Unknown config parameter '$parameterName'.");
115 }
116
117 return $options[$parameterName]->getValue();
118 }
119
120 public function getParameterValue(string $parameterName): mixed
121 {
122 try {
123 return $this->getParameterValueRaw($parameterName);
124 } catch (Exception) {
125 }
126
127 return null;
128 }
129 }
130