-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettings.php
More file actions
148 lines (133 loc) · 2.79 KB
/
Settings.php
File metadata and controls
148 lines (133 loc) · 2.79 KB
1
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/**
* @author : Jakiboy
* @package : FloatPHP
* @subpackage : Helpers Connection Component
* @version : 1.5.x
* @copyright : (c) 2018 - 2025 Jihad Sinnaour <me@jihadsinnaour.com>
* @link : https://floatphp.com
* @license : MIT
*
* This file is a part of FloatPHP Framework.
*/
declare(strict_types=1);
namespace FloatPHP\Helpers\Connection;
/**
* Built-in Settings IO class.
*/
final class Settings
{
use \FloatPHP\Helpers\Framework\tr\TraitConfigurable,
\FloatPHP\Helpers\Framework\tr\TraitFormattable;
/**
* @access private
* @var string $row, Temp row name
* @var string ROW
* @var string TTL
*/
private $row;
private const ROW = 'settings';
/**
* Set settings row group.
*
* @access public
* @param string $row
*/
public function __construct(string $row = self::ROW)
{
// Set settings row name
$this->row = $row;
// Init config
$this->getConfigObject();
}
/**
* Get settings value.
*
* @access public
* @param string $key
* @param mixed $default
* @return mixed
*/
public function get(?string $key = null, $default = null) : mixed
{
$settings = $this->getValues();
if ( !$this->isType('null', $key) ) {
return $settings[$key] ?? $default;
}
return $settings;
}
/**
* Set settings value.
*
* @access public
* @param mixed $key
* @param mixed $value
* @return bool
*/
public function set($key, $value = null) : bool
{
if ( $this->isType('array', $key) ) {
return $this->setValues($key);
}
if ( $this->isType('string', $key) ) {
$settings = $this->getValues();
$settings[$key] = $value;
return $this->setValues($settings);
}
return false;
}
/**
* Delete settings value.
*
* @access public
* @param string $key
* @return bool
*/
public function delete(string $key) : bool
{
$settings = $this->getValues();
if ( isset($settings[$key]) ) {
unset($settings[$key]);
return $this->setValues($settings);
}
return false;
}
/**
* Reset database settings row.
*
* @access public
* @return bool
*/
public function reset() : bool
{
return $this->deleteConfigValue($this->row);
}
/**
* Get settings values from database.
*
* @access private
* @return array
*/
private function getValues() : array
{
$settings = $this->getConfigValue($this->row);
$settings = $this->unserialize($settings);
if ( $this->isType('string', $settings) ) {
$settings = $this->decodeJson($settings, true);
}
return $settings ?: [];
}
/**
* Set settings values in database.
*
* @access private
* @param array $settings
* @return bool
*/
private function setValues(array $settings) : bool
{
$settings = $this->formatJson($settings, 256);
$settings = $this->serialize($settings);
return $this->setConfigValue($this->row, $settings);
}
}