]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/Util/ConfigFileTransformer.php
4eaafe0610d3218843f9fe607128cc1f3baa37a0
[friendica.git] / src / Core / Config / Util / ConfigFileTransformer.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
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\Core\Config\Util;
23
24 /**
25  * Util to transform back the config array into a string
26  */
27 class ConfigFileTransformer
28 {
29         public static function encode(array $data): string
30         {
31                 $dataString = '<?php' . PHP_EOL . PHP_EOL;
32                 $dataString .= 'return [' . PHP_EOL;
33
34                 $categories = array_keys($data);
35
36                 foreach ($categories as $category) {
37
38                         if (is_null($data[$category])) {
39                                 $dataString .= "\t'$category' => null," . PHP_EOL;
40                                 continue;
41                         }
42
43                         $dataString .= "\t'$category' => [" . PHP_EOL;
44
45                         if (is_array($data[$category])) {
46                                 $keys = array_keys($data[$category]);
47
48                                 foreach ($keys as $key) {
49                                         $dataString .= static::mapConfigValue($key, $data[$category][$key]);
50                                 }
51                         }
52                         $dataString .= "\t]," . PHP_EOL;
53                 }
54
55                 $dataString .= "];" . PHP_EOL;
56
57                 return $dataString;
58         }
59
60         protected static function extractArray(array $config, int $level = 0): string
61         {
62                 $string = '';
63
64                 foreach ($config as $configKey => $configValue) {
65                         $string .= static::mapConfigValue($configKey, $configValue, $level);
66                 }
67
68                 return $string;
69         }
70
71         protected static function mapConfigValue(string $key, $value, $level = 0): string
72         {
73                 $string = str_repeat("\t", $level + 2) . "'$key' => ";
74
75                 if (is_null($value)) {
76                         $string .= "null,";
77                 } elseif (is_array($value)) {
78                         $string .= "[" . PHP_EOL;
79                         $string .= static::extractArray($value, ++$level);
80                         $string .= str_repeat("\t", $level + 1) . '],';
81                 } elseif (is_bool($value)) {
82                         $string .= ($value ? 'true' : 'false') . ",";
83                 } elseif (is_numeric($value)) {
84                         $string .= $value . ",";
85                 } else {
86                         $string .= sprintf('\'%s\',', addcslashes($value, '\'\\'));
87                 }
88
89                 $string .= PHP_EOL;
90
91                 return $string;
92         }
93 }