]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/Model/DbaUtils.php
Restructure (P)Config to follow new paradigm
[friendica.git] / src / Core / Config / Model / DbaUtils.php
1 <?php
2
3 namespace Friendica\Core\Config\Model;
4
5 class DbaUtils
6 {
7         /**
8          * Formats a DB value to a config value
9          * - null   = The db-value isn't set
10          * - bool   = The db-value is either '0' or '1'
11          * - array  = The db-value is a serialized array
12          * - string = The db-value is a string
13          *
14          * Keep in mind that there aren't any numeric/integer config values in the database
15          *
16          * @param null|string $value
17          *
18          * @return null|array|string
19          */
20         public static function toConfigValue($value)
21         {
22                 if (!isset($value)) {
23                         return null;
24                 }
25
26                 switch (true) {
27                         // manage array value
28                         case preg_match("|^a:[0-9]+:{.*}$|s", $value):
29                                 return unserialize($value);
30
31                         default:
32                                 return $value;
33                 }
34         }
35
36         /**
37          * Formats a config value to a DB value (string)
38          *
39          * @param mixed $value
40          *
41          * @return string
42          */
43         public static function toDbValue($value): string
44         {
45                 // if not set, save an empty string
46                 if (!isset($value)) {
47                         return '';
48                 }
49
50                 switch (true) {
51                         // manage arrays
52                         case is_array($value):
53                                 return serialize($value);
54
55                         default:
56                                 return (string)$value;
57                 }
58         }
59 }