]> git.mxchange.org Git - friendica.git/blob - src/Model/Config/DbaConfig.php
Merge pull request #8269 from MrPetovan/bug/frio-more-actions
[friendica.git] / src / Model / Config / DbaConfig.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Model\Config;
23
24 use Friendica\Database\Database;
25
26 /**
27  * The DB-based model of (P-)Config values
28  * Encapsulates db-calls in case of config queries
29  */
30 abstract class DbaConfig
31 {
32         /** @var Database */
33         protected $dba;
34
35         /**
36          * @param Database $dba The database connection of this model
37          */
38         public function __construct(Database $dba)
39         {
40                 $this->dba = $dba;
41         }
42
43         /**
44          * Checks if the model is currently connected
45          *
46          * @return bool
47          */
48         public function isConnected()
49         {
50                 return $this->dba->isConnected();
51         }
52
53         /**
54          * Formats a DB value to a config value
55          * - null   = The db-value isn't set
56          * - bool   = The db-value is either '0' or '1'
57          * - array  = The db-value is a serialized array
58          * - string = The db-value is a string
59          *
60          * Keep in mind that there aren't any numeric/integer config values in the database
61          *
62          * @param null|string $value
63          *
64          * @return null|array|string
65          */
66         protected function toConfigValue($value)
67         {
68                 if (!isset($value)) {
69                         return null;
70                 }
71
72                 switch (true) {
73                         // manage array value
74                         case preg_match("|^a:[0-9]+:{.*}$|s", $value):
75                                 return unserialize($value);
76
77                         default:
78                                 return $value;
79                 }
80         }
81
82         /**
83          * Formats a config value to a DB value (string)
84          *
85          * @param mixed $value
86          *
87          * @return string
88          */
89         protected function toDbValue($value)
90         {
91                 // if not set, save an empty string
92                 if (!isset($value)) {
93                         return '';
94                 }
95
96                 switch (true) {
97                         // manage arrays
98                         case is_array($value):
99                                 return serialize($value);
100
101                         default:
102                                 return (string)$value;
103                 }
104         }
105 }