]> git.mxchange.org Git - friendica.git/blob - src/Model/Config/DbaConfig.php
Merge pull request #7540 from vinzv/patch-1
[friendica.git] / src / Model / Config / DbaConfig.php
1 <?php
2
3 namespace Friendica\Model\Config;
4
5 use Friendica\Database\Database;
6
7 /**
8  * The DB-based model of (P-)Config values
9  * Encapsulates db-calls in case of config queries
10  */
11 abstract class DbaConfig
12 {
13         /** @var Database */
14         protected $dba;
15
16         /**
17          * @param Database $dba The database connection of this model
18          */
19         public function __construct(Database $dba)
20         {
21                 $this->dba = $dba;
22         }
23
24         /**
25          * Checks if the model is currently connected
26          *
27          * @return bool
28          */
29         public function isConnected()
30         {
31                 return $this->dba->isConnected();
32         }
33
34         /**
35          * Formats a DB value to a config value
36          * - null   = The db-value isn't set
37          * - bool   = The db-value is either '0' or '1'
38          * - array  = The db-value is a serialized array
39          * - string = The db-value is a string
40          *
41          * Keep in mind that there aren't any numeric/integer config values in the database
42          *
43          * @param null|string $value
44          *
45          * @return null|array|string
46          */
47         protected function toConfigValue($value)
48         {
49                 if (!isset($value)) {
50                         return null;
51                 }
52
53                 switch (true) {
54                         // manage array value
55                         case preg_match("|^a:[0-9]+:{.*}$|s", $value):
56                                 return unserialize($value);
57
58                         default:
59                                 return $value;
60                 }
61         }
62
63         /**
64          * Formats a config value to a DB value (string)
65          *
66          * @param mixed $value
67          *
68          * @return string
69          */
70         protected function toDbValue($value)
71         {
72                 // if not set, save an empty string
73                 if (!isset($value)) {
74                         return '';
75                 }
76
77                 switch (true) {
78                         // manage arrays
79                         case is_array($value):
80                                 return serialize($value);
81
82                         default:
83                                 return (string)$value;
84                 }
85         }
86 }