-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig.php
More file actions
164 lines (151 loc) · 3.73 KB
/
Config.php
File metadata and controls
164 lines (151 loc) · 3.73 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?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;
use FloatPHP\Kernel\Orm;
/**
* Config class used to store config in database.
*/
class Config extends Orm
{
/**
* @access private
* @var string $keyColumn, Config key column name
* @var string $valueColumn, Config value column name
*/
private $keyColumn;
private $valueColumn;
/**
* @access private
* @var string ID
* @var string KEY
* @var string VALUE
*/
private const ID = 'configId';
private const KEY = 'name';
private const VALUE = 'options';
private const TABLE = 'config';
/**
* Init database.
*
* @param string $key
* @param string $value
* @param string $id
* @param string $table
*/
public function __construct($key = self::KEY, $value = self::VALUE, $id = self::ID, $table = self::TABLE)
{
// Init ORM
parent::__construct();
// Set table
$this->keyColumn = $key;
$this->valueColumn = $value;
$this->key = $id;
$this->table = $table;
}
/**
* Get database config value by key.
*
* @access public
* @param string $key
* @return string
*/
public function getValue(string $key) : string
{
$this->bind(["{$this->keyColumn}" => $key]);
$sql = "SELECT `{$this->valueColumn}` FROM `{$this->table}` ";
$sql .= "WHERE `{$this->keyColumn}` LIKE :{$this->keyColumn};";
return (string)$this->getSingle($sql);
}
/**
* Set database config value.
*
* @access public
* @param string $key
* @param mixed $value
* @return bool
*/
public function setValue(string $key, $value = '') : bool
{
if ( $this->hasValue($key) ) {
return $this->updateValue($key, $value);
}
$this->bind([
"{$this->keyColumn}" => $key,
"{$this->valueColumn}" => $value
]);
$sql = "INSERT INTO `{$this->table}` (`$this->keyColumn`, `{$this->valueColumn}`) ";
$sql .= "VALUES(:{$this->keyColumn}, :{$this->valueColumn});";
return (bool)$this->execute($sql);
}
/**
* Update database config value.
*
* @access public
* @param string $key
* @param mixed $value
* @return bool
*/
public function updateValue(string $key, $value = '') : bool
{
$this->bind([
"{$this->keyColumn}" => $key,
"{$this->valueColumn}" => $value
]);
$sql = "UPDATE `{$this->table}` SET `{$this->valueColumn}` = :{$this->valueColumn} ";
$sql .= "WHERE `{$this->keyColumn}` LIKE :{$this->keyColumn};";
return (bool)$this->execute($sql);
}
/**
* Delete database config by key.
*
* @access public
* @param string $key
* @return bool
*/
public function deleteValue(string $key) : bool
{
$this->bind(["{$this->keyColumn}" => $key]);
$sql = "DELETE FROM `{$this->table}` ";
$sql .= "WHERE `{$this->keyColumn}` LIKE :{$this->keyColumn};";
return (bool)$this->execute($sql);
}
/**
* Check database config exists by key.
*
* @access public
* @param string $key
* @return bool
*/
public function hasValue(string $key) : bool
{
$this->bind(["{$this->keyColumn}" => $key]);
$sql = "SELECT COUNT('{$this->keyColumn}') FROM `{$this->table}` ";
$sql .= "WHERE `{$this->keyColumn}` LIKE :{$this->keyColumn};";
return (bool)$this->getSingle($sql);
}
/**
* Set config Id.
*
* @access public
* @param string $key
* @return int
*/
public function getId(string $key) : int
{
$this->bind(["{$this->keyColumn}" => $key]);
$sql = "SELECT `{$this->key}` FROM `{$this->table}` ";
$sql .= "WHERE `{$this->keyColumn}` LIKE :{$this->keyColumn};";
return (int)$this->getSingle($sql);
}
}