| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
namespace Liuch\DmarcSrg\Settings; |
| 33 |
|
| 34 |
use Liuch\DmarcSrg\Core; |
| 35 |
use Liuch\DmarcSrg\Users\User; |
| 36 |
use Liuch\DmarcSrg\Exception\SoftException; |
| 37 |
use Liuch\DmarcSrg\Exception\DatabaseNotFoundException; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
abstract class Setting |
| 46 |
{ |
| 47 |
public const TYPE_STRING = 1; |
| 48 |
public const TYPE_INTEGER = 2; |
| 49 |
public const TYPE_STRING_SELECT = 3; |
| 50 |
|
| 51 |
protected $core = null; |
| 52 |
protected $name = null; |
| 53 |
protected $user = null; |
| 54 |
protected $value = null; |
| 55 |
protected $wignore = false; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
abstract public function type(): int; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
abstract protected function checkValue(): bool; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
abstract protected function stringToValue(string $s): void; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
abstract protected function valueToString(): string; |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public function __construct($data, bool $wignore = false, $core = null) |
| 106 |
{ |
| 107 |
$this->wignore = $wignore; |
| 108 |
$this->core = $core ?? Core::instance(); |
| 109 |
switch (gettype($data)) { |
| 110 |
case 'string': |
| 111 |
$this->name = $data; |
| 112 |
SettingsList::checkName($this->name); |
| 113 |
return; |
| 114 |
case 'array': |
| 115 |
if (!isset($data['name']) || gettype($data['name']) !== 'string') { |
| 116 |
break; |
| 117 |
} |
| 118 |
$this->name = $data['name']; |
| 119 |
SettingsList::checkName($this->name); |
| 120 |
if (isset($data['value'])) { |
| 121 |
$this->value = $data['value']; |
| 122 |
if (!$this->checkValue()) { |
| 123 |
if (!$wignore) { |
| 124 |
break; |
| 125 |
} |
| 126 |
$this->resetToDefault(); |
| 127 |
} |
| 128 |
} |
| 129 |
if (isset($data['user'])) { |
| 130 |
if (!($data['user'] instanceof User)) { |
| 131 |
break; |
| 132 |
} |
| 133 |
$this->user = $data['user']; |
| 134 |
} |
| 135 |
return; |
| 136 |
} |
| 137 |
throw new SoftException('Wrong setting data'); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public function name(): string |
| 146 |
{ |
| 147 |
return $this->name; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public function value() |
| 156 |
{ |
| 157 |
if (is_null($this->value)) { |
| 158 |
$this->fetchData(); |
| 159 |
} |
| 160 |
return $this->value; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
public function setValue($value): void |
| 171 |
{ |
| 172 |
$this->value = $value; |
| 173 |
if (!$this->checkValue()) { |
| 174 |
if (!$this->wignore) { |
| 175 |
throw new SoftException('Wrong setting value'); |
| 176 |
} |
| 177 |
$this->resetToDefault(); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
public function toArray(): array |
| 187 |
{ |
| 188 |
if (is_null($this->value)) { |
| 189 |
$this->fetchData(); |
| 190 |
} |
| 191 |
$sdef = SettingsList::$schema[$this->name]['default']; |
| 192 |
switch ($this->type()) { |
| 193 |
case self::TYPE_STRING: |
| 194 |
$type = 'string'; |
| 195 |
break; |
| 196 |
case self::TYPE_INTEGER: |
| 197 |
$type = 'integer'; |
| 198 |
$sdef = intval($sdef); |
| 199 |
break; |
| 200 |
case self::TYPE_STRING_SELECT: |
| 201 |
$type = 'select'; |
| 202 |
break; |
| 203 |
default: |
| 204 |
$type = ''; |
| 205 |
$sdef = ''; |
| 206 |
break; |
| 207 |
} |
| 208 |
return [ |
| 209 |
'type' => $type, |
| 210 |
'name' => $this->name, |
| 211 |
'value' => $this->value, |
| 212 |
'default' => $sdef |
| 213 |
]; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
public function save(): void |
| 224 |
{ |
| 225 |
$this->core->database()->getMapper('setting')->save( |
| 226 |
$this->name, |
| 227 |
$this->valueToString(), |
| 228 |
($this->user ?? $this->core->getCurrentUser())->id() |
| 229 |
); |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
private function fetchData(): void |
| 238 |
{ |
| 239 |
try { |
| 240 |
$res = $this->core->database()->getMapper('setting')->value( |
| 241 |
$this->name, |
| 242 |
($this->user ?? $this->core->getCurrentUser())->id() |
| 243 |
); |
| 244 |
} catch (DatabaseNotFoundException $e) { |
| 245 |
$this->resetToDefault(); |
| 246 |
return; |
| 247 |
} |
| 248 |
$this->stringToValue($res); |
| 249 |
if (!$this->checkValue()) { |
| 250 |
$this->resetToDefault(); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
private function resetToDefault(): void |
| 260 |
{ |
| 261 |
$this->stringToValue(SettingsList::$schema[$this->name]['default']); |
| 262 |
} |
| 263 |
} |