PluginProbe
User Access Manager / 2.3.20
User Access Manager v2.3.20
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.20, at src/Config/Config.php

122 lines 3.0 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\Config\Parameter\ConfigParameter;
9 use UserAccessManager\Wrapper\Wordpress;
10
11 class Config
12 {
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 protected string $key
26 ) {
27 }
28
29 public function getWpOption(string $option): mixed
30 {
31 if (isset($this->wpOptions[$option]) === false) {
32 $this->wpOptions[$option] = $this->wordpress->getOption($option);
33 }
34
35 return $this->wpOptions[$option];
36 }
37
38 /**
39 * @return ConfigParameter[]
40 */
41 protected function getDefaultConfigParameters(): array
42 {
43 return $this->defaultConfigParameters;
44 }
45
46 /**
47 * @param ConfigParameter[] $defaultConfigParameters
48 */
49 public function setDefaultConfigParameters(array $defaultConfigParameters): void
50 {
51 $this->defaultConfigParameters = $defaultConfigParameters;
52 }
53
54 /**
55 * @return ConfigParameter[]
56 */
57 public function getConfigParameters(): array
58 {
59 if ($this->configParameters !== null) {
60 return $this->configParameters;
61 }
62
63 $configParameters = $this->getDefaultConfigParameters();
64
65 foreach ((array) $this->getWpOption($this->key) as $key => $option) {
66 if (isset($configParameters[$key]) === true) {
67 $configParameters[$key]->setValue($option);
68 }
69 }
70
71 return $this->configParameters = $configParameters;
72 }
73
74 public function setConfigParameters(array $rawParameters): void
75 {
76 $configParameters = $this->getConfigParameters();
77
78 foreach ($rawParameters as $key => $value) {
79 if (isset($configParameters[$key]) === true) {
80 $configParameters[$key]->setValue($value);
81 }
82 }
83
84 $persistableValues = [];
85
86 foreach ($configParameters as $parameter) {
87 $persistableValues[$parameter->getId()] = $parameter->getValue();
88 }
89
90 $this->wordpress->updateOption($this->key, $persistableValues);
91 }
92
93 public function flushConfigParameters(): void
94 {
95 $this->defaultConfigParameters = [];
96 $this->configParameters = null;
97 }
98
99 /**
100 * @throws Exception
101 */
102 public function getParameterValueRaw(string $parameterName): mixed
103 {
104 $options = $this->getConfigParameters();
105
106 if (isset($options[$parameterName]) === false) {
107 throw new Exception("Unknown config parameter '$parameterName'.");
108 }
109
110 return $options[$parameterName]->getValue();
111 }
112
113 public function getParameterValue(string $parameterName): mixed
114 {
115 try {
116 return $this->getParameterValueRaw($parameterName);
117 } catch (Exception) {
118 return null;
119 }
120 }
121 }
122