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